Changeset 7e54a5f in nscp for include/settings


Ignore:
Timestamp:
03/08/11 23:40:04 (2 years ago)
Author:
Michael Medin <michael@…>
Branches:
master, 0.4.0, 0.4.1, 0.4.2
Children:
569a179
Parents:
197b263
Message:

Fixed a bunch of issues with the "old settings reader/importer"

File:
1 edited

Legend:

Unmodified
Added
Removed
  • include/settings/settings_old.hpp

    rc760fc9 r7e54a5f  
    7878    typedef std::map<std::wstring,std::wstring> path_map; 
    7979    typedef std::map<settings_core::key_path_type,settings_core::key_path_type> key_map; 
     80    typedef std::pair<std::wstring,std::wstring> section_key_type; 
     81    typedef std::pair<settings_core::key_path_type,settings_core::key_path_type> keys_key_type; 
     82 
    8083    path_map sections_; 
    8184    key_map keys_; 
     
    97100    settings_core::key_path_type map_key(settings_core::key_path_type new_key) { 
    98101      key_map::iterator it1 = keys_.find(new_key); 
    99       if (it1 != keys_.end()) 
     102      if (it1 != keys_.end()) { 
     103        get_core()->get_logger()->quick_debug(new_key.first + _T(".") + new_key.second + _T(" not found in alias list")); 
    100104        return (*it1).second; 
     105      } 
    101106      path_map::iterator it2 = sections_.find(new_key.first); 
    102107      if (it2 != sections_.end()) 
     
    125130      key = map_key(key); 
    126131      get_core()->get_logger()->quick_debug(key.first + _T("//") + key.second); 
    127       return internal_get_value(key.first, key.second.c_str()); 
     132      return internal_get_value(key.first, key.second); 
    128133    } 
    129134#define UNLIKELY_STRING _T("$$$EMPTY_KEY$$$") 
     
    131136    std::wstring internal_get_value(std::wstring path, std::wstring key, int bufferSize = 1024) { 
    132137      get_core()->get_logger()->quick_debug(path + _T("//") + key); 
     138      if (!has_key_int(path, key)) 
     139        throw KeyNotFoundException(key); 
     140 
    133141      TCHAR* buffer = new TCHAR[bufferSize+2]; 
    134142      if (buffer == NULL) 
    135         throw settings_exception(_T("Out of memmory error!")); 
    136       int retVal = GetPrivateProfileString(path.c_str(), key.c_str(), UNLIKELY_STRING, buffer, bufferSize, get_file_name().c_str()); 
     143        throw settings_exception(_T("Out of memory error!")); 
     144      int retVal = GetPrivateProfileString(path.c_str(), key.c_str(), _T(""), buffer, bufferSize, get_file_name().c_str()); 
    137145      if (retVal == bufferSize-1) { 
    138146        delete [] buffer; 
     
    141149      std::wstring ret = buffer; 
    142150      delete [] buffer; 
    143       if (ret != UNLIKELY_STRING) 
    144         return ret; 
    145       if (has_key_int(path, key)) { 
    146         return _T(""); 
    147       } 
    148       throw KeyNotFoundException(key); 
    149       //return ret; 
     151      return ret; 
    150152    } 
    151153 
     
    183185    /// @author mickem 
    184186    virtual bool has_real_key(settings_core::key_path_type key) { 
    185       return has_key_int(key.first, key.second); 
    186     } 
    187  
    188     bool has_key_int(std::wstring path, std::wstring key, int bufferLength=1024) { 
    189       string_list ret; 
     187      settings_core::key_path_type old = map_key(key); 
     188      return has_key_int(old.first, old.second); 
     189    } 
     190 
     191 
     192    std::set<std::wstring> internal_read_keys_from_section(std::wstring section, int bufferLength = 1024) { 
    190193      TCHAR* buffer = new TCHAR[bufferLength+1]; 
    191194      if (buffer == NULL) 
    192         throw settings_exception(_T("has_key_int:: Failed to allocate memory for buffer!")); 
    193       std::wstring mapped = map_path(path); 
    194       unsigned int count = ::GetPrivateProfileSection(mapped.c_str(), buffer, bufferLength-2, get_file_name().c_str()); 
     195        throw settings_exception(_T("internal_read_keys_from_section:: Failed to allocate memory for buffer!")); 
     196      unsigned int count = ::GetPrivateProfileSection(section.c_str(), buffer, bufferLength-2, get_file_name().c_str()); 
    195197      if (count == bufferLength-2) { 
    196198        delete [] buffer; 
    197         return has_key_int(path, key, bufferLength*10); 
    198       } 
    199  
     199        return internal_read_keys_from_section(section, bufferLength*10); 
     200      } 
     201 
     202      std::set<std::wstring> ret; 
    200203      unsigned int last = 0; 
    201204      for (unsigned int i=0;i<count;i++) { 
     
    203206          std::wstring s = &buffer[last]; 
    204207          std::size_t p = s.find('='); 
    205           if ((p == std::wstring::npos && s == key) || (s.substr(0,p) == key)) { 
    206             delete [] buffer; 
    207             return true; 
    208           } 
     208          ret.insert((p == std::wstring::npos)?s:s.substr(0,p)); 
    209209          last = i+1; 
    210210        } 
    211211      } 
    212212      delete [] buffer; 
    213       return false; 
     213      return ret; 
     214    } 
     215 
     216    typedef std::map<std::wstring,std::set<std::wstring> > section_cache_type; 
     217    section_cache_type section_cache_; 
     218    bool has_key_int(std::wstring path, std::wstring key) { 
     219      section_cache_type::const_iterator it = section_cache_.find(path); 
     220      if (it == section_cache_.end()) { 
     221        std::set<std::wstring> list = internal_read_keys_from_section(path); 
     222        section_cache_[path] = list; 
     223        it = section_cache_.find(path); 
     224      } 
     225      return (*it).second.find(key) != (*it).second.end(); 
    214226    } 
    215227 
     
    248260    virtual void get_real_sections(std::wstring path, string_list &list) { 
    249261      get_core()->get_logger()->debug(__FILE__, __LINE__, std::wstring(_T("Get sections for: ")) + path); 
     262 
     263      unsigned int path_length = path.length(); 
    250264      //string_list lst = get_mapped_sections(path); 
    251265      //list.insert(list.end(), lst.begin(), lst.end()); 
    252       /* 
    253       string_list src = int_read_sections(); 
    254       for (string_list::const_iterator cit = src.begin(); cit != src.end(); ++cit) { 
    255         std::wstring mapped = get_core()->reverse_map_path((*cit)); 
     266      BOOST_FOREACH(section_key_type key, sections_) { 
     267        if (path_length == 0 || path == _T("/")) { 
     268          std::wstring::size_type pos = key.first.find(L'/', 1); 
     269          list.push_back(pos == std::wstring::npos?key.first:key.first.substr(0,pos)); 
     270        } else if (key.first.length() > path_length && path == key.first.substr(0, path_length)) { 
     271          std::wstring::size_type pos = key.first.find(L'/', path_length+1); 
     272          list.push_back(pos == std::wstring::npos?key.first.substr(path_length+1):key.first.substr(path_length+1,pos-path_length-1)); 
     273        } 
     274      } 
     275      BOOST_FOREACH(keys_key_type key, keys_) { 
    256276        if (path.empty() || path == _T("/")) { 
    257           std::wstring::size_type pos = mapped.find(L'/', 1); 
     277          std::wstring::size_type pos = key.first.first.find(L'/', 1); 
    258278          if (pos != std::wstring::npos) 
    259             mapped = mapped.substr(0,pos); 
    260           get_core()->get_logger()->debug(__FILE__, __LINE__, std::wstring(_T("Found: ")) + mapped); 
    261           list.push_back(mapped); 
    262         } else if (mapped.length() > path.length() && mapped == path.substr(0, path.length())) { 
    263           get_core()->get_logger()->debug(__FILE__, __LINE__, std::wstring(_T("Found: FUCKED")) + mapped); 
    264         } 
    265       } 
    266       */ 
    267       //list.insert(list.end(), src.begin(), src.end()); 
    268       /* 
    269       CSimpleIni::TNamesDepend lst; 
    270       ini.GetAllSections(lst); 
    271       if (path.empty()) { 
    272         for (CSimpleIni::TNamesDepend::const_iterator cit = lst.begin(); cit != lst.end(); ++cit) { 
    273           std::wstring mapped = get_core()->reverse_map_path((*cit).pItem); 
    274           if (mapped.length() > 1) { 
    275             std::wstring::size_type pos = mapped.find(L'/', 1); 
    276             if (pos != std::wstring::npos) 
    277               mapped = mapped.substr(0,pos); 
    278           } 
    279           list.push_back(mapped); 
    280         } 
    281       } else { 
    282         for (CSimpleIni::TNamesDepend::const_iterator cit = lst.begin(); cit != lst.end(); ++cit) { 
    283           std::wstring mapped = get_core()->reverse_map_path((*cit).pItem); 
    284           get_core()->get_logger()->debug(__FILE__, __LINE__, std::wstring(_T("Looking for: ")) + mapped + _T(": ") + mapped); 
    285           std::wstring::size_type mapped_len = mapped.length(); 
    286           std::wstring::size_type path_len = path.length(); 
    287           if (mapped_len > path_len+1 && mapped.substr(0,path_len) == path) { 
    288             std::wstring::size_type pos = mapped.find(L'/', path_len+1); 
    289             if (pos == std::wstring::npos) 
    290               mapped = mapped.substr(path_len+1); 
    291             else 
    292               mapped = mapped.substr(path_len+1, pos-path_len-1); 
    293             list.push_back(mapped); 
    294           } 
    295         } 
    296       } 
    297       */ 
     279            key.first.first = key.first.first.substr(0,pos); 
     280          get_core()->get_logger()->debug(__FILE__, __LINE__, std::wstring(_T("Found: ")) + key.first.first); 
     281          list.push_back(key.first.first); 
     282        } else if (key.first.first.length() > path_length && path == key.first.first.substr(0, path_length)) { 
     283          std::wstring::size_type pos = key.first.first.find(L'/', path_length+1); 
     284          list.push_back(pos == std::wstring::npos?key.first.first.substr(path_length+1):key.first.first.substr(path_length+1,pos-path_length-1)); 
     285        } 
     286      } 
     287      list.unique(); 
    298288    } 
    299289 
     
    336326    /// @author mickem 
    337327    virtual void get_real_keys(std::wstring path, string_list &list) { 
    338       std::wstring mapped_path = map_path(path); 
    339       int_read_section(mapped_path, list); 
    340       /* 
    341       settings::settings_core::mapped_key_list_type mapped_keys = get_core()->find_maped_keys(path); 
    342       for (settings::settings_core::mapped_key_list_type::const_iterator cit = mapped_keys.begin(); cit != mapped_keys.end(); ++cit) { 
    343         if (has_key((*cit).dst.first, (*cit).dst.second)) 
    344           list.push_back((*cit).src.second); 
    345       } 
    346       */ 
    347     } 
    348 //    virtual settings_core::key_type get_key_type(std::wstring path, std::wstring key) { 
    349 //      return settings_core::key_string; 
    350 //    } 
     328      if (path.empty() || path == _T("/")) { 
     329        get_core()->get_logger()->debug(__FILE__, __LINE__, std::wstring(_T("Loose leaves not supported: TODO"))); 
     330        return; 
     331      } 
     332      // @todo: this will NOT work for "nodes in paths" 
     333      BOOST_FOREACH(keys_key_type key, keys_) { 
     334        if (path == key.first.first) { 
     335          if (has_key_int(key.second.first, key.second.second)) 
     336            list.push_back(key.first.second); 
     337        } else { 
     338          //get_core()->get_logger()->debug(__FILE__, __LINE__, std::wstring(_T("Found: TODO FOO fix sub sections")) + key.first.first); 
     339        } 
     340      } 
     341 
     342      BOOST_FOREACH(section_key_type key, sections_) { 
     343        if (key.first == path) { 
     344          section_cache_type::const_iterator it = section_cache_.find(key.second); 
     345          get_core()->get_logger()->debug(__FILE__, __LINE__, std::wstring(_T("=============>>>>>>>>>>>")) + key.first + _T(" >>>> ") + key.second); 
     346          if (it == section_cache_.end()) { 
     347            std::set<std::wstring> list = internal_read_keys_from_section(path); 
     348            section_cache_[path] = list; 
     349            it = section_cache_.find(path); 
     350          } 
     351          list.insert(list.end(), (*it).second.begin(), (*it).second.end()); 
     352        } 
     353      } 
     354 
     355    } 
    351356  private: 
    352     bool has_key(std::wstring section, std::wstring key) { 
    353       TCHAR* buffer = new TCHAR[1024]; 
    354       GetPrivateProfileString(section.c_str(), key.c_str(), UNLIKELY_STRING, buffer, 1023, get_file_name().c_str()); 
    355       std::wstring ret = buffer; 
    356       delete [] buffer; 
    357       return ret != UNLIKELY_STRING; 
    358     } 
     357 
    359358    void int_read_section(std::wstring section, string_list &list, unsigned int bufferLength = BUFF_LEN) { 
    360359      //get_core()->get_logger()->debug(__FILE__, __LINE__, _T("Reading (OLD) section: ") + section); 
     
    384383    } 
    385384 
     385    string_list int_read_section_from_inifile(std::wstring section, unsigned int bufferLength = BUFF_LEN) { 
     386      TCHAR* buffer = new TCHAR[bufferLength+1]; 
     387      if (buffer == NULL) 
     388        throw settings_exception(_T("getSections:: Failed to allocate memory for buffer!")); 
     389      unsigned int count = GetPrivateProfileSection(section.c_str(), buffer, bufferLength, get_file_name().c_str()); 
     390      if (count == bufferLength-2) { 
     391        delete [] buffer; 
     392        return int_read_section_from_inifile(section, bufferLength*10); 
     393      } 
     394      unsigned int last = 0; 
     395      string_list list; 
     396      for (unsigned int i=0;i<count;i++) { 
     397        if (buffer[i] == '\0') { 
     398          std::wstring s = &buffer[last]; 
     399          std::size_t p = s.find('='); 
     400          if (p == std::wstring::npos) 
     401            list.push_back(s); 
     402          else 
     403            list.push_back(s.substr(0,p)); 
     404          last = i+1; 
     405        } 
     406      } 
     407      delete [] buffer; 
     408      return list; 
     409    } 
     410 
     411 
    386412    inline std::wstring get_file_name() { 
    387413      if (filename_.empty()) { 
Note: See TracChangeset for help on using the changeset viewer.