Changeset 99bb030 in nscp for trunk/include/Settings.h


Ignore:
Timestamp:
03/20/06 16:50:33 (7 years ago)
Author:
Michael Medin <michael@…>
Children:
8b89aba
Parents:
c3579b8
Message:

* empty log message *

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/Settings.h

    r9bc31a8 r99bb030  
    55#include <map> 
    66#include <windows.h> 
     7#include <INISettings.h> 
    78#define BUFF_LEN 4096 
    8  
    9 class Section { 
    10 }; 
    119 
    1210class SettingsT 
    1311{ 
    1412private: 
    15   typedef std::map<std::string,std::string> saveKeyList; 
     13  typedef struct { 
     14    typedef enum { sType, iType} typeEnum; 
     15    typeEnum type; 
     16    std::string sVal; 
     17    int iVal; 
     18  } valueStruct; 
     19  typedef std::map<std::string,valueStruct> saveKeyList; 
    1620  typedef std::map<std::string,saveKeyList> saveSectionList; 
    17   std::string file_; 
    1821  saveSectionList data_; 
    1922  bool bHasInternalData; 
     23  INISettings iniManager; 
    2024public: 
    2125  typedef std::list<std::string> sectionList; 
     
    3337   */ 
    3438  void setFile(std::string file) { 
    35     file_ = file; 
     39    iniManager.setFile(file); 
    3640  } 
    3741 
    38   sectionList getSections() { 
    39     sectionList ret; 
    40     char* buffer = new char[BUFF_LEN+1]; 
    41     unsigned int count = ::GetPrivateProfileSectionNames(buffer, BUFF_LEN, file_.c_str()); 
    42     if (count == BUFF_LEN-2) 
    43       throw "Fuck..."; 
    44     unsigned int last = 0; 
    45     for (unsigned int i=0;i<count;i++) { 
    46       if (buffer[i] == '\0') { 
    47         std::string s = &buffer[last]; 
    48         ret.push_back(s); 
    49         last = i+1; 
     42#define UNLIKELY_VALUE -1234 
     43  void read() { 
     44    sectionList sections = getSections(); 
     45    for (sectionList::const_iterator it=sections.begin();it!=sections.end();++it) { 
     46      sectionList section = getSection(*it); 
     47      for (sectionList::const_iterator it2=section.begin();it2!=section.end();++it2) { 
     48        int i = getInt((*it), (*it2), UNLIKELY_VALUE); 
     49        if (i == UNLIKELY_VALUE) { 
     50          getString((*it), (*it2)); 
     51        } 
    5052      } 
    5153    } 
    52     delete [] buffer; 
     54  } 
     55 
     56  sectionList getSections(unsigned int bufferLength = BUFF_LEN) { 
     57    sectionList ret; 
     58    ret = iniManager.getSections(); 
    5359    if (bHasInternalData) { 
    54       for (saveSectionList::const_iterator it = data_.begin(); it != data_.end(); ++it) { 
     60      for (saveSectionList::const_iterator kit = data_.begin(); kit != data_.end(); ++kit) { 
     61        ret.push_back(kit->first); 
    5562      } 
    5663    } 
     
    6572   * @return A list with all keys from the section 
    6673   */ 
    67   sectionList getSection(std::string section) { 
     74  sectionList getSection(std::string section, unsigned int bufferLength = BUFF_LEN) { 
    6875    sectionList ret; 
    69     char* buffer = new char[BUFF_LEN+1]; 
    70     unsigned int count = GetPrivateProfileSection(section.c_str(), buffer, BUFF_LEN, file_.c_str()); 
    71     if (count == BUFF_LEN-2) 
    72       throw "Fuck..."; 
    73     unsigned int last = 0; 
    74     for (unsigned int i=0;i<count;i++) { 
    75       if (buffer[i] == '\0') { 
    76         std::string s = &buffer[last]; 
    77         ret.push_back(s); 
    78         last = i+1; 
    79       } 
    80     } 
    81     delete [] buffer; 
     76    ret = iniManager.getSection(section); 
    8277    if (bHasInternalData) { 
    8378      saveSectionList::const_iterator it = data_.find(section); 
     
    105100        saveKeyList::const_iterator kit = it->second.find(key); 
    106101        if (kit != it->second.end()) { 
    107           return kit->second; 
     102          if (kit->second.type == valueStruct::sType) 
     103            return kit->second.sVal; 
     104          else 
     105            throw "whoops"; 
    108106        } 
    109107      } 
    110108    } 
    111     char* buffer = new char[1024]; 
    112     GetPrivateProfileString(section.c_str(), key.c_str(), defaultValue.c_str(), buffer, 1023, file_.c_str()); 
    113     std::string ret = buffer; 
    114     delete [] buffer; 
     109    std::string ret = iniManager.getString(section, key, defaultValue); 
    115110    return ret; 
    116111  } 
     
    118113  void setString(std::string section, std::string key, std::string value) { 
    119114    bHasInternalData = true; 
    120     (data_[section])[key] = value; 
     115    (data_[section])[key].sVal = value; 
     116    (data_[section])[key].type = valueStruct::sType; 
    121117  } 
    122118 
     
    129125   */ 
    130126  int getInt(std::string section, std::string key, int defaultValue = 0) { 
    131     return GetPrivateProfileInt(section.c_str(), key.c_str(), defaultValue, file_.c_str()); 
     127    return iniManager.getInt(section, key, defaultValue); 
     128  } 
     129  void setInt(std::string section, std::string key, int value) { 
     130    bHasInternalData = true; 
     131    (data_[section])[key].iVal = value; 
     132    (data_[section])[key].type = valueStruct::iType; 
    132133  } 
    133134}; 
Note: See TracChangeset for help on using the changeset viewer.