| 1 | #pragma once |
|---|
| 2 | |
|---|
| 3 | #include <Singleton.h> |
|---|
| 4 | #define BUFF_LEN 4096 |
|---|
| 5 | |
|---|
| 6 | class Section { |
|---|
| 7 | }; |
|---|
| 8 | |
|---|
| 9 | class SettingsT |
|---|
| 10 | { |
|---|
| 11 | private: |
|---|
| 12 | std::string file_; |
|---|
| 13 | public: |
|---|
| 14 | typedef std::list<std::string> sectionList; |
|---|
| 15 | |
|---|
| 16 | SettingsT(void) |
|---|
| 17 | { |
|---|
| 18 | } |
|---|
| 19 | |
|---|
| 20 | virtual ~SettingsT(void) |
|---|
| 21 | { |
|---|
| 22 | } |
|---|
| 23 | |
|---|
| 24 | /** |
|---|
| 25 | * Set the file to read from |
|---|
| 26 | * @param file A INI-file to use as settings repository |
|---|
| 27 | */ |
|---|
| 28 | void setFile(std::string file) { |
|---|
| 29 | file_ = file; |
|---|
| 30 | } |
|---|
| 31 | /** |
|---|
| 32 | * Get all keys from a section as a list<string> |
|---|
| 33 | * @param section The section to return all keys from |
|---|
| 34 | * @return A list with all keys from the section |
|---|
| 35 | */ |
|---|
| 36 | std::list<std::string> getSection(std::string section) { |
|---|
| 37 | std::list<std::string> ret; |
|---|
| 38 | char* buffer = new char[BUFF_LEN+1]; |
|---|
| 39 | unsigned int count = GetPrivateProfileSection(section.c_str(), buffer, BUFF_LEN, file_.c_str()); |
|---|
| 40 | if (count == BUFF_LEN-2) |
|---|
| 41 | throw "Fuck..."; |
|---|
| 42 | unsigned int last = 0; |
|---|
| 43 | for (unsigned int i=0;i<count;i++) { |
|---|
| 44 | if (buffer[i] == '\0') { |
|---|
| 45 | std::string s = &buffer[last]; |
|---|
| 46 | ret.push_back(s); |
|---|
| 47 | last = i+1; |
|---|
| 48 | } |
|---|
| 49 | } |
|---|
| 50 | delete [] buffer; |
|---|
| 51 | return ret; |
|---|
| 52 | } |
|---|
| 53 | /** |
|---|
| 54 | * Get a string from the settings file |
|---|
| 55 | * @param section Section to read from |
|---|
| 56 | * @param key Key to retrieve |
|---|
| 57 | * @param defaultValue Default value to return if key is not found |
|---|
| 58 | * @return The value or defaultValue if the key is not found |
|---|
| 59 | */ |
|---|
| 60 | std::string getString(std::string section, std::string key, std::string defaultValue = "") { |
|---|
| 61 | char* buffer = new char[1024]; |
|---|
| 62 | GetPrivateProfileString(section.c_str(), key.c_str(), defaultValue.c_str(), buffer, 1023, file_.c_str()); |
|---|
| 63 | std::string ret = buffer; |
|---|
| 64 | delete [] buffer; |
|---|
| 65 | return ret; |
|---|
| 66 | } |
|---|
| 67 | /** |
|---|
| 68 | * Get an integer from the settings file |
|---|
| 69 | * @param section Section to read from |
|---|
| 70 | * @param key Key to retrieve |
|---|
| 71 | * @param defaultValue Default value to return if key is not found |
|---|
| 72 | * @return The value or defaultValue if the key is not found |
|---|
| 73 | */ |
|---|
| 74 | int getInt(std::string section, std::string key, int defaultValue = 0) { |
|---|
| 75 | return GetPrivateProfileInt(section.c_str(), key.c_str(), defaultValue, file_.c_str()); |
|---|
| 76 | } |
|---|
| 77 | }; |
|---|
| 78 | |
|---|
| 79 | typedef Singleton<SettingsT> Settings; // Implement the settings manager as a singleton |
|---|