void ConfFile::save() { if(conffile.size()==0) { throw ConfFileException("filename not set", conffile.c_str(), 0); } //save conf file std::ofstream out; out.open(conffile.c_str()); if(!out.is_open()) { std::string write_error = std::string("failed to write config to ") + conffile; throw ConfFileException(write_error, conffile.c_str(), 0); } for(std::map<std::string, ConfSectionList*>::iterator it = sectionmap.begin(); it!= sectionmap.end(); it++) { ConfSectionList* sectionlist = it->second; for(ConfSectionList::iterator sit = sectionlist->begin(); sit != sectionlist->end(); sit++) { ConfSection* s = *sit; s->print(out); } } out.close(); }
//get first entry in a section with a particular name ConfEntry* ConfFile::getEntry(const std::string& section, const std::string& key) { ConfSection* sec = getSection(section); if(sec==0) return 0; ConfEntry* entry = sec->getEntry(key); return entry; }
//returns a list of all entries in a section with a particular name ConfEntryList* ConfFile::getEntries(const std::string& section, const std::string& key) { ConfSection* sec = getSection(section); if(sec==0) return 0; ConfEntryList* entryList = sec->getEntries(key); return entryList; }
//returns the first section with a particular name void ConfFile::setEntry(const std::string& section, const std::string& key, const std::string& value) { ConfSection* sec = getSection(section); if(sec==0) { sec = addSection(section); } sec->setEntry(key, value); }
void SDLAppSettings::exportDisplaySettings(ConfFile& conf) { ConfSection* section = new ConfSection("display"); char viewportbuff[256]; snprintf(viewportbuff, 256, "%dx%d%s", display_width, display_height, resizable ? "" : "!" ); std::string viewport = std::string(viewportbuff); section->setEntry(new ConfEntry("viewport", viewport)); if(fullscreen) section->setEntry(new ConfEntry("fullscreen", fullscreen)); else { if(frameless) section->setEntry(new ConfEntry("frameless", frameless)); if(window_x >= 0 && window_y >= 0) { char windowbuff[256]; snprintf(windowbuff, 256, "%dx%d", window_x, window_y); section->setEntry(new ConfEntry("window-position", std::string(windowbuff))); } } if(multisample) section->setEntry(new ConfEntry("multi-sampling", multisample)); if(!vsync) { section->setEntry(new ConfEntry("no-vsync", true)); } conf.setSection(section); }
void SDLAppSettings::exportDisplaySettings(ConfFile& conf) { ConfSection* section = new ConfSection("display"); char viewportbuff[256]; snprintf(viewportbuff, 256, "%dx%d", display_width, display_height); std::string viewport = std::string(viewportbuff); section->setEntry(new ConfEntry("viewport", viewport)); if(fullscreen) section->setEntry(new ConfEntry("fullscreen", fullscreen)); if(multisample) section->setEntry(new ConfEntry("multi-sampling", multisample)); conf.setSection(section); }
//apply args to a conf file void SDLAppSettings::parseArgs(const std::vector<std::string>& arguments, ConfFile& conffile, std::vector<std::string>* files) { std::map<std::string, std::string>::iterator findit; for(int i=0;i<arguments.size();i++) { std::string args = arguments[i]; //remove leading hyphens bool is_option = false; while(args.size()>1 && args[0] == '-') { args = args.substr(1, args.size()-1); is_option = true; } if(args.size()==0) continue; if(!is_option) { if(files!=0) { files->push_back(args); } continue; } //translate args with aliases if((findit = arg_aliases.find(args)) != arg_aliases.end()) { args = findit->second; } //NUMBERxNUMBER is a magic alias for viewport if(args.size()>1 && args.rfind("x") != std::string::npos) { std::string displayarg = args; int width = 0; int height = 0; bool no_resize = false; if(parseViewport(displayarg, width, height, no_resize)) { if(width>0 && height>0) { ConfSection* display_settings = conffile.getSection("display"); if(!display_settings) { display_settings = conffile.addSection("display"); } display_settings->setEntry("viewport", displayarg); continue; } } } //get type std::string arg_type; if((findit = arg_types.find(args)) != arg_types.end()) { arg_type = findit->second; } else { std::string unknown_option = std::string("unknown option ") + args; throw ConfFileException(unknown_option, "", 0); } //get value (or set to true for booleans) std::string argvalue; if(arg_type == "bool") argvalue = "true"; else if((i+1)<arguments.size()) argvalue = arguments[++i]; //determine section std::string section_name = default_section_name; if((findit = conf_sections.find(args)) != conf_sections.end()) { section_name = findit->second; } //command line options dont go into the conf file if(section_name == "command-line") { commandLineOption(args, argvalue); continue; } //get section(s) of this type ConfSectionList* sections = conffile.getSections(section_name); if(sections == 0) { conffile.addSection(section_name); sections = conffile.getSections(section_name); } //apply to section for(ConfSectionList::iterator it = sections->begin(); it != sections->end(); it++) { ConfSection* section = *it; if(arg_type == "multi-value") { section->addEntry(args, argvalue); } else { section->setEntry(args, argvalue); } } } }
void ConfFile::load() { debugLog("ConfFile::load(%s)", conffile.c_str()); clear(); char buff[1024]; int lineno = 0; ConfSection* sec = 0; std::ifstream in(conffile.c_str()); if(!in.is_open()) { sprintf(buff, "failed to open config file %s", conffile.c_str()); std::string conf_error = std::string(buff); throw ConfFileException(conf_error, conffile, 0); } std::string line; while(std::getline(in, line)) { lineno++; std::vector<std::string> matches; // blank line or commented out lines if(line.size() == 0 || (line.size() > 0 && line[0] == '#')) { continue; // sections } else if(ConfFile_section.match(line, &matches)) { if(sec != 0) addSection(sec); sec = new ConfSection(matches[0], lineno); // key value pairs } else if(ConfFile_key_value.match(line, &matches)) { std::string key = matches[0]; std::string value = (matches.size()>1) ? matches[1] : ""; //trim whitespace ConfFile::trim(value); if(sec==0) sec = new ConfSection("", lineno); sec->addEntry(key, value, lineno); debugLog("%s: [%s] %s => %s", conffile.c_str(), sec->getName().c_str(), key.c_str(), value.c_str()); } else { sprintf(buff, "%s, line %d: could not parse line", conffile.c_str(), lineno); std::string conf_error = std::string(buff); throw ConfFileException(conf_error, conffile, lineno); } } if(sec != 0) addSection(sec); in.close(); }