//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); } } } }
int main(int argc, char *argv[]) { SDLAppInit("Gource", "gource"); ConfFile conf; std::vector<std::string> files; //convert args to a conf file //read the conf file //apply the conf file to settings try { gGourceSettings.parseArgs(argc, argv, conf, &files); if(gGourceSettings.load_config.empty() && !files.empty()) { //see if file looks like a config file for(std::vector<std::string>::iterator fit = files.begin(); fit != files.end(); fit++) { std::string file = *fit; int file_length = file.size(); if( (file.rfind(".conf") == (file_length-5) && file_length > 5) || (file.rfind(".cfg") == (file_length-4) && file_length > 4) || (file.rfind(".ini") == (file_length-4) && file_length > 4) ) { bool is_conf=true; try { ConfFile conftest; conftest.load(file); } catch(ConfFileException& exception) { is_conf = false; } if(is_conf) { gGourceSettings.load_config = file; files.erase(fit); break; } } } } //set log level Logger::getDefault()->setLevel(gGourceSettings.log_level); //load config if(!gGourceSettings.load_config.empty()) { conf.clear(); conf.load(gGourceSettings.load_config); //apply args to loaded conf file gGourceSettings.parseArgs(argc, argv, conf); } //set path if(!files.empty()) { std::string path = files[files.size()-1]; ConfSectionList* sectionlist = conf.getSections("gource"); if(sectionlist!=0) { for(ConfSectionList::iterator sit = sectionlist->begin(); sit != sectionlist->end(); sit++) { (*sit)->setEntry("path", path); } } else { conf.setEntry("gource", "path", path); } } //apply the config / see if its valid gGourceSettings.importDisplaySettings(conf); gGourceSettings.importGourceSettings(conf); //save config if(!gGourceSettings.save_config.empty()) { conf.save(gGourceSettings.save_config); exit(0); } //write custom log file if(gGourceSettings.output_custom_filename.size() > 0 && gGourceSettings.path.size() > 0) { Gource::writeCustomLog(gGourceSettings.path, gGourceSettings.output_custom_filename); exit(0); } } catch(ConfFileException& exception) { SDLAppQuit(exception.what()); } // this causes corruption on some video drivers if(gGourceSettings.multisample) { display.multiSample(4); } //background needs alpha channel if(gGourceSettings.transparent) { display.enableAlpha(true); } //enable vsync display.enableVsync(gGourceSettings.vsync); //allow resizing window if we are not recording if(gGourceSettings.resizable && gGourceSettings.output_ppm_filename.empty()) { display.enableResize(true); } try { display.init("Gource", gGourceSettings.display_width, gGourceSettings.display_height, gGourceSettings.fullscreen); } catch(SDLInitException& exception) { char errormsg[1024]; snprintf(errormsg, 1024, "SDL initialization failed - %s", exception.what()); SDLAppQuit(errormsg); } #ifdef _WIN32 SDLAppAttachToConsole(); #endif //init frame exporter FrameExporter* exporter = 0; if(gGourceSettings.output_ppm_filename.size() > 0) { try { exporter = new PPMExporter(gGourceSettings.output_ppm_filename); } catch(PPMExporterException& exception) { char errormsg[1024]; snprintf(errormsg, 1024, "could not write to '%s'", exception.what()); SDLAppQuit(errormsg); } } if(display.multiSamplingEnabled()) { glEnable(GL_MULTISAMPLE_ARB); } GourceShell* gourcesh = 0; try { gourcesh = gGourceShell = new GourceShell(&conf, exporter); gourcesh->run(); } catch(ResourceException& exception) { char errormsg[1024]; snprintf(errormsg, 1024, "failed to load resource '%s'", exception.what()); SDLAppQuit(errormsg); } catch(SDLAppException& exception) { if(exception.showHelp()) { gGourceSettings.help(); } else { SDLAppQuit(exception.what()); } } gGourceShell = 0; if(gourcesh != 0) delete gourcesh; if(exporter != 0) delete exporter; //free resources display.quit(); return 0; }
int main(int argc, char *argv[]) { ConfFile conf; std::vector<std::string> files; SDLAppInit("Logstalgia", "logstalgia"); #ifdef _WIN32 SDLApp::initConsole(); #endif try { settings.parseArgs(argc, argv, conf, &files); //set log level Logger::getDefault()->setLevel(settings.log_level); #ifdef _WIN32 // hide console if not needed if(settings.log_level == LOG_LEVEL_OFF && !SDLApp::existing_console) { SDLApp::showConsole(false); } #endif //load config if(!settings.load_config.empty()) { conf.clear(); conf.load(settings.load_config); //apply args to loaded conf file settings.parseArgs(argc, argv, conf); } if(!files.empty()) { std::string path = files[files.size()-1]; ConfSectionList* sectionlist = conf.getSections("logstalgia"); if(sectionlist!=0) { for(ConfSectionList::iterator sit = sectionlist->begin(); sit != sectionlist->end(); sit++) { (*sit)->setEntry("path", path); } } else { conf.setEntry("logstalgia", "path", path); } } //apply the config / see if its valid settings.importDisplaySettings(conf); settings.importLogstalgiaSettings(conf); //save config if(!settings.save_config.empty()) { conf.save(settings.save_config); exit(0); } } catch(ConfFileException& exception) { SDLAppQuit(exception.what()); } #ifdef _WIN32 if(settings.path.empty()) { //open file dialog settings.path = win32LogSelector(); //TODO chdir back to original directory if(settings.path.empty()) return 0; } #endif if(settings.path.empty()) SDLAppQuit("no file supplied"); //enable vsync display.enableVsync(settings.vsync); // this causes corruption on some video drivers if(settings.multisample) display.multiSample(4); if(settings.resizable && settings.output_ppm_filename.empty()) { display.enableResize(true); } display.init("Logstalgia", settings.display_width, settings.display_height, settings.fullscreen); // Don't minimize when alt-tabbing so you can fullscreen logstalgia on a second monitor #if SDL_VERSION_ATLEAST(2,0,0) SDL_SetHint(SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS, "0"); #endif //disable OpenGL 2.0 functions if not supported if(!GLEW_VERSION_2_0) settings.ffp = true; //init frame exporter FrameExporter* exporter = 0; if(!settings.output_ppm_filename.empty()) { try { exporter = new PPMExporter(settings.output_ppm_filename); } catch(PPMExporterException& exception) { char errormsg[1024]; snprintf(errormsg, 1024, "could not write to '%s'", exception.what()); SDLAppQuit(errormsg); } } if(settings.multisample) glEnable(GL_MULTISAMPLE_ARB); Logstalgia* ls = 0; try { ls = new Logstalgia(settings.path); if(exporter != 0) { ls->setFrameExporter(exporter); } for(const std::string& group : settings.groups) { ls->addGroup(group); } ls->setBackground(settings.background_colour); ls->run(); } catch(ResourceException& exception) { char errormsg[1024]; snprintf(errormsg, 1024, "failed to load resource '%s'", exception.what()); SDLAppQuit(errormsg); } catch(SDLAppException& exception) { if(exception.showHelp()) { settings.help(); } else { SDLAppQuit(exception.what()); } } if(ls!=0) delete ls; if(exporter!=0) delete exporter; display.quit(); return 0; }