💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
2.9 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. /*
  2. * Copyright (C) 2017 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper Source Code.
  5. *
  6. * Antkeeper Source Code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper Source Code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper Source Code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef SETTINGS_HPP
  20. #define SETTINGS_HPP
  21. #include <codecvt>
  22. #include <locale>
  23. #include <map>
  24. #include <string>
  25. #include <sstream>
  26. class ParameterDict
  27. {
  28. public:
  29. template <typename T>
  30. bool get(const std::string& name, T* value) const;
  31. template <typename T>
  32. void set(const std::string& name, const T& value);
  33. bool load(const std::string& filename);
  34. bool save(const std::string& filename);
  35. void clear();
  36. const std::map<std::string, std::string>* getParameters() const;
  37. private:
  38. std::map<std::string, std::string> parameters;
  39. };
  40. template <typename T>
  41. bool ParameterDict::get(const std::string& name, T* value) const
  42. {
  43. auto it = parameters.find(name);
  44. if (it == parameters.end())
  45. return false;
  46. std::stringstream stream(it->second);
  47. stream >> (*value);
  48. if (stream.fail())
  49. return false;
  50. return true;
  51. }
  52. template <>
  53. inline bool ParameterDict::get<std::string>(const std::string& name, std::string* value) const
  54. {
  55. auto it = parameters.find(name);
  56. if (it == parameters.end())
  57. return false;
  58. *value = it->second;
  59. return true;
  60. }
  61. template <>
  62. inline bool ParameterDict::get<std::u32string>(const std::string& name, std::u32string* value) const
  63. {
  64. auto it = parameters.find(name);
  65. if (it == parameters.end())
  66. return false;
  67. // Convert UTF-8 string to UTF-32 string
  68. #if _MSC_VER >= 1900
  69. //std::wstring_convert<std::codecvt_utf8<uint32_t>, uint32_t> convert;
  70. //*value = convert.from_bytes(it->second);
  71. std::wstring_convert<std::codecvt_utf8<uint32_t>, uint32_t> convert;
  72. auto uint32string = convert.from_bytes(it->second);
  73. value->resize(uint32string.size());
  74. for (std::size_t i = 0; i < uint32string.size(); ++i)
  75. {
  76. (*value)[i] = static_cast<char32_t>(uint32string[i]);
  77. }
  78. #else
  79. std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> convert;
  80. *value = convert.from_bytes(it->second);
  81. #endif
  82. return true;
  83. }
  84. template <typename T>
  85. void ParameterDict::set(const std::string& name, const T& value)
  86. {
  87. std::stringstream stream;
  88. stream << value;
  89. parameters[name] = stream.str();
  90. }
  91. inline const std::map<std::string, std::string>*ParameterDict:: getParameters() const
  92. {
  93. return &parameters;
  94. }
  95. #endif