void Block::AppendOption(const std::string& group, const std::string& key, const std::string& value, char seperator) { std::map<std::string, KeyValueMap>::iterator it = m_keyValueGroups.find(group); if (it == m_keyValueGroups.end()) { //Insert new vector as one does not exist already for this platform. std::pair<std::map<std::string, KeyValueMap>::iterator, bool> result = m_keyValueGroups.insert(std::make_pair(group, KeyValueMap())); it = result.first; } KeyValueMap& kvmap = (*it).second; KeyValueMap::iterator kvit = kvmap.find(key); if (kvit == kvmap.end()) { kvmap.insert(std::make_pair(key, value)); } else { const std::string& oldValue = kvit->second; std::stringstream ss; ss << oldValue; ss << seperator; ss << value; kvit->second = ss.str(); } }
KeyValueMap& ConfigFile::findSection(const std::string& sec) { for(unsigned int is=0; is<sections.size(); is++) if(comparestr(sections[is].first, sec)) return sections[is].second; // not found -- add new section sections.push_back(std::pair<std::string, KeyValueMap>(sec, KeyValueMap())); return sections.back().second; }
ConfigFile::ConfigFile(const std::string& _fileName) : fileName(_fileName) { std::ifstream strm(fileName.c_str()); if(!strm) throw std::runtime_error("File does not exist: "+_fileName); std::string buffer; int secIndex = -1; while(std::getline(strm, buffer)) { // remove all comment lines if(buffer.size()>0 && (buffer[0] == '#' || buffer[0] == ';')) continue; std::string::size_type indx = buffer.find('['); std::string::size_type indx1= buffer.find(']'); if(indx!=std::string::npos && indx1!=std::string::npos && indx1>indx) { // section start - parse section name buffer = buffer.substr(indx+1, indx1-indx-1); indx = buffer.find_first_not_of(" \t\n\r"); if(indx!=std::string::npos && indx>0) buffer.erase(0, indx); indx = buffer.find_last_not_of(" \t\n\r"); if(indx!=std::string::npos && indx+1<buffer.size()) buffer.erase(indx+1); if(buffer.empty()) continue; // find if this section already existed in the list secIndex = -1; for(size_t i=0; i<sections.size(); i++) if(comparestr(sections[i].first, buffer)) secIndex = i; if(secIndex<0) { // section not found before, add it sections.push_back(std::pair<std::string, KeyValueMap>(buffer, KeyValueMap())); secIndex = sections.size()-1; } } else { // not a section if(sections.empty()) { // no sections has been created yet sections.push_back(std::pair<std::string, KeyValueMap>("", KeyValueMap())); // add an empty section secIndex = 0; } sections[secIndex].second.add(buffer.c_str()); } } strm.close(); }
void Block::SetOption(const std::string& group_, const std::string& key_, const std::string& value_) { std::string group, key, value; mbExpandMacros(&group, this, group_.c_str()); mbExpandMacros(&key, this, key_.c_str()); mbExpandMacros(&value, this, value_.c_str()); std::map<std::string, KeyValueMap>::iterator it = m_keyValueGroups.find(group); if (it == m_keyValueGroups.end()) { //Insert new vector as one does not exist already for this platform. std::pair<std::map<std::string, KeyValueMap>::iterator, bool> result = m_keyValueGroups.insert(std::make_pair(group, KeyValueMap())); it = result.first; } KeyValueMap& kvmap = (*it).second; kvmap[key] = value; }
Block::Block() { m_parent = NULL; m_keyValueGroups.insert(std::make_pair("__macros", KeyValueMap())); m_macroCacheDirty = true; }