Settings.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. Copyright (c) 2009-2010 Christopher A. Taylor. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions are met:
  5. * Redistributions of source code must retain the above copyright notice,
  6. this list of conditions and the following disclaimer.
  7. * Redistributions in binary form must reproduce the above copyright notice,
  8. this list of conditions and the following disclaimer in the documentation
  9. and/or other materials provided with the distribution.
  10. * Neither the name of LibCat nor the names of its contributors may be used
  11. to endorse or promote products derived from this software without
  12. specific prior written permission.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  14. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  16. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  17. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  23. POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef CAT_SETTINGS_HPP
  26. #define CAT_SETTINGS_HPP
  27. #include <cat/Singleton.hpp>
  28. #include <cat/threads/Mutex.hpp>
  29. #include <fstream>
  30. namespace cat {
  31. enum SettingsValueFlags
  32. {
  33. CAT_SETTINGS_FILLED = 1, // s[] array has been set
  34. CAT_SETTINGS_INT = 2, // value has been promoted to int 'i'
  35. };
  36. struct SettingsValue
  37. {
  38. u8 flags; // sum of SettingsValueFlags
  39. char s[256]; // always nul-terminated
  40. int i;
  41. };
  42. class SettingsKey
  43. {
  44. public:
  45. SettingsKey(SettingsKey *lnode, SettingsKey *gnode, const char *name);
  46. ~SettingsKey();
  47. public:
  48. SettingsKey *lnode, *gnode;
  49. char name[64]; // not necessarily nul-terminated
  50. SettingsValue value;
  51. public:
  52. void write(std::ofstream &file);
  53. };
  54. // User settings manager
  55. class Settings : public Singleton<Settings>
  56. {
  57. CAT_SINGLETON(Settings);
  58. Mutex _lock;
  59. protected:
  60. static const u32 KEY_HASH_SALT = 0xbaddecaf;
  61. static const int SETTINGS_HASH_BINS = 256;
  62. SettingsKey *hbtrees[SETTINGS_HASH_BINS]; // hash table of binary trees
  63. bool readSettings; // Flag set when settings have been read from disk
  64. bool modified; // Flag set when settings have been modified since last write
  65. std::string _settings_file;
  66. protected:
  67. SettingsKey *addKey(const char *name);
  68. SettingsKey *getKey(const char *name);
  69. SettingsKey *initInt(const char *name, int n, bool overwrite);
  70. SettingsKey *initStr(const char *name, const char *value, bool overwrite);
  71. void clear();
  72. public:
  73. void readSettingsFromFile(const char *file_path = "settings.txt", const char *override_file = "override.txt");
  74. void readSettingsFromBuffer(const char *data, int len);
  75. void write();
  76. public:
  77. int getInt(const char *name);
  78. const char *getStr(const char *name);
  79. int getInt(const char *name, int init);
  80. const char *getStr(const char *name, const char *init);
  81. void setInt(const char *name, int n);
  82. void setStr(const char *name, const char *value);
  83. };
  84. } // namespace cat
  85. #endif // CAT_SETTINGS_HPP
粤ICP备19079148号