示例#1
0
文件: conffile.cpp 项目: JickLee/Core
//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);
}
示例#2
0
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);
}
示例#3
0
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);
}
示例#4
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);
            }
        }
    }
}