Пример #1
0
//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);
            }
        }
    }
}
Пример #2
0
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();
}