| 1 | #pragma once |
|---|
| 2 | |
|---|
| 3 | #include <Singleton.h> |
|---|
| 4 | #include <string> |
|---|
| 5 | #include <map> |
|---|
| 6 | #include <windows.h> |
|---|
| 7 | #include <TSettings.h> |
|---|
| 8 | #include <config.h> |
|---|
| 9 | |
|---|
| 10 | #define BUFF_LEN 4096 |
|---|
| 11 | |
|---|
| 12 | class INISettings : public TSettings |
|---|
| 13 | { |
|---|
| 14 | private: |
|---|
| 15 | // typedef std::map<std::string,std::string> saveKeyList; |
|---|
| 16 | // typedef std::map<std::string,saveKeyList> saveSectionList; |
|---|
| 17 | std::string file_; |
|---|
| 18 | public: |
|---|
| 19 | typedef std::list<std::string> sectionList; |
|---|
| 20 | INISettings(std::string file) : file_(file) |
|---|
| 21 | { |
|---|
| 22 | } |
|---|
| 23 | |
|---|
| 24 | virtual ~INISettings(void) |
|---|
| 25 | { |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | static bool hasSettings(std::string file) { |
|---|
| 29 | std::cout << GetPrivateProfileInt(MAIN_SECTION_TITLE, MAIN_USEFILE, MAIN_USEFILE_DEFAULT, file.c_str()) << ":" << file << std::endl; |
|---|
| 30 | return GetPrivateProfileInt(MAIN_SECTION_TITLE, MAIN_USEFILE, MAIN_USEFILE_DEFAULT, file.c_str()) == 1; |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | sectionList getSections(unsigned int bufferLength = BUFF_LEN) { |
|---|
| 34 | sectionList ret; |
|---|
| 35 | char* buffer = new char[bufferLength+1]; |
|---|
| 36 | unsigned int count = ::GetPrivateProfileSectionNames(buffer, BUFF_LEN, file_.c_str()); |
|---|
| 37 | if (count == bufferLength-2) { |
|---|
| 38 | delete [] buffer; |
|---|
| 39 | return getSections(bufferLength*10); |
|---|
| 40 | } |
|---|
| 41 | unsigned int last = 0; |
|---|
| 42 | for (unsigned int i=0;i<count;i++) { |
|---|
| 43 | if (buffer[i] == '\0') { |
|---|
| 44 | std::string s = &buffer[last]; |
|---|
| 45 | ret.push_back(s); |
|---|
| 46 | last = i+1; |
|---|
| 47 | } |
|---|
| 48 | } |
|---|
| 49 | delete [] buffer; |
|---|
| 50 | return ret; |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | /** |
|---|
| 54 | * Get all keys from a section as a list<string> |
|---|
| 55 | * @param section The section to return all keys from |
|---|
| 56 | * @return A list with all keys from the section |
|---|
| 57 | */ |
|---|
| 58 | sectionList getSection(std::string section, unsigned int bufferLength = BUFF_LEN) { |
|---|
| 59 | sectionList ret; |
|---|
| 60 | char* buffer = new char[bufferLength+1]; |
|---|
| 61 | unsigned int count = GetPrivateProfileSection(section.c_str(), buffer, bufferLength, file_.c_str()); |
|---|
| 62 | if (count == bufferLength-2) { |
|---|
| 63 | delete [] buffer; |
|---|
| 64 | return getSection(section, bufferLength*10); |
|---|
| 65 | } |
|---|
| 66 | unsigned int last = 0; |
|---|
| 67 | for (unsigned int i=0;i<count;i++) { |
|---|
| 68 | if (buffer[i] == '\0') { |
|---|
| 69 | std::string s = &buffer[last]; |
|---|
| 70 | ret.push_back(s); |
|---|
| 71 | last = i+1; |
|---|
| 72 | } |
|---|
| 73 | } |
|---|
| 74 | delete [] buffer; |
|---|
| 75 | return ret; |
|---|
| 76 | } |
|---|
| 77 | /** |
|---|
| 78 | * Get a string from the settings file |
|---|
| 79 | * @param section Section to read from |
|---|
| 80 | * @param key Key to retrieve |
|---|
| 81 | * @param defaultValue Default value to return if key is not found |
|---|
| 82 | * @return The value or defaultValue if the key is not found |
|---|
| 83 | */ |
|---|
| 84 | std::string getString(std::string section, std::string key, std::string defaultValue = "") const { |
|---|
| 85 | char* buffer = new char[1024]; |
|---|
| 86 | GetPrivateProfileString(section.c_str(), key.c_str(), defaultValue.c_str(), buffer, 1023, file_.c_str()); |
|---|
| 87 | std::string ret = buffer; |
|---|
| 88 | delete [] buffer; |
|---|
| 89 | return ret; |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | void setString(std::string section, std::string key, std::string value) { |
|---|
| 93 | WritePrivateProfileString(section.c_str(), key.c_str(), value.c_str(), file_.c_str()); |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | /** |
|---|
| 97 | * Get an integer from the settings file |
|---|
| 98 | * @param section Section to read from |
|---|
| 99 | * @param key Key to retrieve |
|---|
| 100 | * @param defaultValue Default value to return if key is not found |
|---|
| 101 | * @return The value or defaultValue if the key is not found |
|---|
| 102 | */ |
|---|
| 103 | int getInt(std::string section, std::string key, int defaultValue = 0) { |
|---|
| 104 | return GetPrivateProfileInt(section.c_str(), key.c_str(), defaultValue, file_.c_str()); |
|---|
| 105 | } |
|---|
| 106 | void setInt(std::string section, std::string key, int value) { |
|---|
| 107 | WritePrivateProfileString(section.c_str(), key.c_str(), strEx::itos(value).c_str(), file_.c_str()); |
|---|
| 108 | } |
|---|
| 109 | }; |
|---|