settings.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #ifndef SETTINGS_HEADER
  17. #define SETTINGS_HEADER
  18. #include "irrlichttypes_bloated.h"
  19. #include "util/string.h"
  20. #include "threading/mutex.h"
  21. #include <string>
  22. #include <map>
  23. #include <list>
  24. #include <set>
  25. class Settings;
  26. struct NoiseParams;
  27. // Global objects
  28. extern Settings *g_settings;
  29. extern std::string g_settings_path;
  30. /** function type to register a changed callback */
  31. typedef void (*setting_changed_callback)(const std::string &name, void *data);
  32. enum ValueType {
  33. VALUETYPE_STRING,
  34. VALUETYPE_FLAG // Doesn't take any arguments
  35. };
  36. enum SettingsParseEvent {
  37. SPE_NONE,
  38. SPE_INVALID,
  39. SPE_COMMENT,
  40. SPE_KVPAIR,
  41. SPE_END,
  42. SPE_GROUP,
  43. SPE_MULTILINE,
  44. };
  45. struct ValueSpec {
  46. ValueSpec(ValueType a_type, const char *a_help=NULL)
  47. {
  48. type = a_type;
  49. help = a_help;
  50. }
  51. ValueType type;
  52. const char *help;
  53. };
  54. struct SettingsEntry {
  55. SettingsEntry()
  56. {
  57. group = NULL;
  58. is_group = false;
  59. }
  60. SettingsEntry(const std::string &value_)
  61. {
  62. value = value_;
  63. group = NULL;
  64. is_group = false;
  65. }
  66. SettingsEntry(Settings *group_)
  67. {
  68. group = group_;
  69. is_group = true;
  70. }
  71. std::string value;
  72. Settings *group;
  73. bool is_group;
  74. };
  75. class Settings {
  76. public:
  77. Settings() {}
  78. ~Settings();
  79. Settings & operator += (const Settings &other);
  80. Settings & operator = (const Settings &other);
  81. /***********************
  82. * Reading and writing *
  83. ***********************/
  84. // Read configuration file. Returns success.
  85. bool readConfigFile(const char *filename);
  86. //Updates configuration file. Returns success.
  87. bool updateConfigFile(const char *filename);
  88. // NOTE: Types of allowed_options are ignored. Returns success.
  89. bool parseCommandLine(int argc, char *argv[],
  90. std::map<std::string, ValueSpec> &allowed_options);
  91. bool parseConfigLines(std::istream &is, const std::string &end = "");
  92. void writeLines(std::ostream &os, u32 tab_depth=0) const;
  93. SettingsParseEvent parseConfigObject(const std::string &line,
  94. const std::string &end, std::string &name, std::string &value);
  95. bool updateConfigObject(std::istream &is, std::ostream &os,
  96. const std::string &end, u32 tab_depth=0);
  97. static bool checkNameValid(const std::string &name);
  98. static bool checkValueValid(const std::string &value);
  99. static std::string sanitizeName(const std::string &name);
  100. static std::string sanitizeValue(const std::string &value);
  101. static std::string getMultiline(std::istream &is, size_t *num_lines=NULL);
  102. static void printEntry(std::ostream &os, const std::string &name,
  103. const SettingsEntry &entry, u32 tab_depth=0);
  104. /***********
  105. * Getters *
  106. ***********/
  107. const SettingsEntry &getEntry(const std::string &name) const;
  108. Settings *getGroup(const std::string &name) const;
  109. std::string get(const std::string &name) const;
  110. bool getBool(const std::string &name) const;
  111. u16 getU16(const std::string &name) const;
  112. s16 getS16(const std::string &name) const;
  113. s32 getS32(const std::string &name) const;
  114. u64 getU64(const std::string &name) const;
  115. float getFloat(const std::string &name) const;
  116. v2f getV2F(const std::string &name) const;
  117. v3f getV3F(const std::string &name) const;
  118. u32 getFlagStr(const std::string &name, const FlagDesc *flagdesc,
  119. u32 *flagmask) const;
  120. // N.B. if getStruct() is used to read a non-POD aggregate type,
  121. // the behavior is undefined.
  122. bool getStruct(const std::string &name, const std::string &format,
  123. void *out, size_t olen) const;
  124. bool getNoiseParams(const std::string &name, NoiseParams &np) const;
  125. bool getNoiseParamsFromValue(const std::string &name, NoiseParams &np) const;
  126. bool getNoiseParamsFromGroup(const std::string &name, NoiseParams &np) const;
  127. // return all keys used
  128. std::vector<std::string> getNames() const;
  129. bool exists(const std::string &name) const;
  130. /***************************************
  131. * Getters that don't throw exceptions *
  132. ***************************************/
  133. bool getEntryNoEx(const std::string &name, SettingsEntry &val) const;
  134. bool getGroupNoEx(const std::string &name, Settings *&val) const;
  135. bool getNoEx(const std::string &name, std::string &val) const;
  136. bool getFlag(const std::string &name) const;
  137. bool getU16NoEx(const std::string &name, u16 &val) const;
  138. bool getS16NoEx(const std::string &name, s16 &val) const;
  139. bool getS32NoEx(const std::string &name, s32 &val) const;
  140. bool getU64NoEx(const std::string &name, u64 &val) const;
  141. bool getFloatNoEx(const std::string &name, float &val) const;
  142. bool getV2FNoEx(const std::string &name, v2f &val) const;
  143. bool getV3FNoEx(const std::string &name, v3f &val) const;
  144. // N.B. getFlagStrNoEx() does not set val, but merely modifies it. Thus,
  145. // val must be initialized before using getFlagStrNoEx(). The intention of
  146. // this is to simplify modifying a flags field from a default value.
  147. bool getFlagStrNoEx(const std::string &name, u32 &val, FlagDesc *flagdesc) const;
  148. /***********
  149. * Setters *
  150. ***********/
  151. // N.B. Groups not allocated with new must be set to NULL in the settings
  152. // tree before object destruction.
  153. bool setEntry(const std::string &name, const void *entry,
  154. bool set_group, bool set_default);
  155. bool set(const std::string &name, const std::string &value);
  156. bool setDefault(const std::string &name, const std::string &value);
  157. bool setGroup(const std::string &name, Settings *group);
  158. bool setGroupDefault(const std::string &name, Settings *group);
  159. bool setBool(const std::string &name, bool value);
  160. bool setS16(const std::string &name, s16 value);
  161. bool setU16(const std::string &name, u16 value);
  162. bool setS32(const std::string &name, s32 value);
  163. bool setU64(const std::string &name, u64 value);
  164. bool setFloat(const std::string &name, float value);
  165. bool setV2F(const std::string &name, v2f value);
  166. bool setV3F(const std::string &name, v3f value);
  167. bool setFlagStr(const std::string &name, u32 flags,
  168. const FlagDesc *flagdesc, u32 flagmask);
  169. bool setNoiseParams(const std::string &name, const NoiseParams &np,
  170. bool set_default=false);
  171. // N.B. if setStruct() is used to write a non-POD aggregate type,
  172. // the behavior is undefined.
  173. bool setStruct(const std::string &name, const std::string &format, void *value);
  174. // remove a setting
  175. bool remove(const std::string &name);
  176. void clear();
  177. void clearDefaults();
  178. void updateValue(const Settings &other, const std::string &name);
  179. void update(const Settings &other);
  180. void registerChangedCallback(std::string name, setting_changed_callback cbf, void *userdata = NULL);
  181. void deregisterChangedCallback(std::string name, setting_changed_callback cbf, void *userdata = NULL);
  182. private:
  183. void updateNoLock(const Settings &other);
  184. void clearNoLock();
  185. void clearDefaultsNoLock();
  186. void doCallbacks(std::string name);
  187. std::map<std::string, SettingsEntry> m_settings;
  188. std::map<std::string, SettingsEntry> m_defaults;
  189. std::map<std::string, std::vector<std::pair<setting_changed_callback,void*> > > m_callbacks;
  190. mutable Mutex m_callbackMutex;
  191. mutable Mutex m_mutex; // All methods that access m_settings/m_defaults directly should lock this.
  192. };
  193. #endif