[6383] | 1 | /* |
---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
| 10 | |
---|
| 11 | ### File Specific: |
---|
| 12 | main-programmer: Christoph Renner |
---|
| 13 | co-programmer: ... |
---|
| 14 | */ |
---|
| 15 | |
---|
| 16 | //#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_ |
---|
| 17 | |
---|
| 18 | #include "cmd_line_prefs_reader.h" |
---|
| 19 | |
---|
[7243] | 20 | #include "preferences.h" |
---|
| 21 | |
---|
[6383] | 22 | using namespace std; |
---|
| 23 | |
---|
[7251] | 24 | RegistredArgs CmdLinePrefsReader::regArgs; |
---|
| 25 | |
---|
[6383] | 26 | /** |
---|
| 27 | * standard constructor |
---|
| 28 | */ |
---|
[7241] | 29 | CmdLinePrefsReader::CmdLinePrefsReader( ) |
---|
[6383] | 30 | { |
---|
[6392] | 31 | |
---|
[6383] | 32 | } |
---|
| 33 | |
---|
| 34 | |
---|
| 35 | /** |
---|
| 36 | * standard deconstructor |
---|
| 37 | */ |
---|
| 38 | CmdLinePrefsReader::~CmdLinePrefsReader () |
---|
| 39 | { |
---|
| 40 | } |
---|
| 41 | |
---|
[7243] | 42 | bool CmdLinePrefsReader::callBack( ArgTableEntry entry, void * data, const std::string & arg, const std::vector<MultiType> & argArgs ) |
---|
[7241] | 43 | { |
---|
[7250] | 44 | CallbackData * cbd = (CallbackData *)data; |
---|
[8316] | 45 | |
---|
[7243] | 46 | switch ( entry.id ) |
---|
| 47 | { |
---|
| 48 | case ID_SET_INI: |
---|
| 49 | { |
---|
| 50 | std::string section = arg; |
---|
| 51 | section.erase( 0, entry.longOption.length()+1 ); |
---|
| 52 | std::string key = section; |
---|
| 53 | section.erase( section.find(".") ); |
---|
| 54 | key.erase( 0, key.find(".")+1 ); |
---|
| 55 | //PRINTF(0)("SECTION '%s', KEY '%s'\n", section.c_str(), key.c_str()); |
---|
[8316] | 56 | |
---|
[7243] | 57 | if ( key == "" || section == "" || argArgs.size() != 1 ) |
---|
| 58 | { |
---|
| 59 | PRINTF(1)("usage: --set-section.key=value\n"); |
---|
| 60 | return false; |
---|
| 61 | } |
---|
[8316] | 62 | |
---|
[7250] | 63 | //Preferences::getInstance()->setMultiType( section, key, argArgs[0], true ); |
---|
| 64 | cbd->iniEntries.push_back( IniEntry() ); |
---|
| 65 | cbd->iniEntries.back().section = section; |
---|
| 66 | cbd->iniEntries.back().key = key; |
---|
| 67 | cbd->iniEntries.back().value = argArgs[0].getString(); |
---|
[7251] | 68 | return true; |
---|
[7243] | 69 | } |
---|
[7250] | 70 | case ID_HELP: |
---|
| 71 | cbd->parser->showHelp(); |
---|
[7251] | 72 | return true; |
---|
[7243] | 73 | } |
---|
[8316] | 74 | |
---|
| 75 | if ( entry.id >= ID_PREFS && entry.id < regArgs.size() + ID_PREFS) |
---|
[7251] | 76 | { |
---|
[7254] | 77 | if ( regArgs[entry.id - ID_PREFS].value == "%arg%" ) |
---|
[7251] | 78 | { |
---|
| 79 | assert( argArgs.size() == 1 ); |
---|
[8316] | 80 | |
---|
[7251] | 81 | cbd->iniEntries.push_back( IniEntry() ); |
---|
[7254] | 82 | cbd->iniEntries.back().section = regArgs[entry.id - ID_PREFS].section; |
---|
| 83 | cbd->iniEntries.back().key = regArgs[entry.id - ID_PREFS].key; |
---|
[7251] | 84 | cbd->iniEntries.back().value = argArgs[0].getString(); |
---|
| 85 | } |
---|
| 86 | else |
---|
| 87 | { |
---|
| 88 | cbd->iniEntries.push_back( IniEntry() ); |
---|
[7254] | 89 | cbd->iniEntries.back().section = regArgs[entry.id - ID_PREFS].section; |
---|
| 90 | cbd->iniEntries.back().key = regArgs[entry.id - ID_PREFS].key; |
---|
| 91 | cbd->iniEntries.back().value = regArgs[entry.id - ID_PREFS].value; |
---|
[7251] | 92 | } |
---|
| 93 | } |
---|
| 94 | else |
---|
| 95 | { |
---|
| 96 | assert(false); |
---|
| 97 | } |
---|
[8316] | 98 | |
---|
[7243] | 99 | return true; |
---|
[7241] | 100 | } |
---|
[6388] | 101 | |
---|
[7250] | 102 | |
---|
[7241] | 103 | bool CmdLinePrefsReader::parse( int argc, char ** argv ) |
---|
| 104 | { |
---|
| 105 | CmdLineParser parser; |
---|
[8316] | 106 | |
---|
[7258] | 107 | parser.add( ID_HELP, "help", 'h', 0, "", "Shows this help and exits"); |
---|
[8316] | 108 | |
---|
| 109 | for (unsigned int i = 0; i<regArgs.size(); i++ ) |
---|
[7251] | 110 | { |
---|
| 111 | if ( regArgs[i].value == "%arg%" ) |
---|
| 112 | { |
---|
[7254] | 113 | parser.add( ID_PREFS + i, regArgs[i].longOption, regArgs[i].shortOption, 1, regArgs[i].argName, regArgs[i].help ); |
---|
[7251] | 114 | } |
---|
| 115 | else |
---|
| 116 | { |
---|
[7254] | 117 | parser.add( ID_PREFS + i, regArgs[i].longOption, regArgs[i].shortOption, 0, "", regArgs[i].help ); |
---|
[7251] | 118 | } |
---|
| 119 | } |
---|
[8316] | 120 | |
---|
[7250] | 121 | parser.add( ID_SET_INI, "set-%", '\0', 1, "value", "Override a configuration element." ); |
---|
[8316] | 122 | |
---|
[7250] | 123 | CallbackData cbd; |
---|
[8316] | 124 | |
---|
[7250] | 125 | cbd.parser = &parser; |
---|
[8316] | 126 | |
---|
[7251] | 127 | if ( parser.parse( &callBack, &cbd, argc, argv ) ) |
---|
| 128 | { |
---|
| 129 | std::list<IniEntry>::const_iterator it; |
---|
| 130 | for ( it = cbd.iniEntries.begin(); it != cbd.iniEntries.end(); it++ ) |
---|
| 131 | { |
---|
| 132 | Preferences::getInstance()->setString( it->section, it->key, it->value, true); |
---|
| 133 | } |
---|
| 134 | } |
---|
| 135 | else |
---|
| 136 | { |
---|
| 137 | exit(EXIT_FAILURE); |
---|
| 138 | } |
---|
[8316] | 139 | |
---|
[7251] | 140 | return true; |
---|
[7241] | 141 | } |
---|
| 142 | |
---|
[7251] | 143 | bool CmdLinePrefsReader::registerArgument( const char shortOption, const std::string & longOption, const std::string & section, const std::string & key, const std::string & help, const std::string & argName, const std::string & value ) |
---|
| 144 | { |
---|
| 145 | RegistredArgument arg; |
---|
[8316] | 146 | |
---|
[7251] | 147 | arg.longOption = longOption; |
---|
[7261] | 148 | while ( arg.longOption.find("_") != std::string::npos ) |
---|
| 149 | { |
---|
| 150 | arg.longOption.replace( arg.longOption.find("_"), 1, "-" ); |
---|
| 151 | } |
---|
[7251] | 152 | arg.shortOption = shortOption; |
---|
[7261] | 153 | if ( arg.shortOption == '_' ) |
---|
| 154 | arg.shortOption = '\0'; |
---|
[7251] | 155 | arg.value = value; |
---|
| 156 | arg.help = help; |
---|
| 157 | arg.argName = argName; |
---|
| 158 | arg.section = section; |
---|
| 159 | arg.key = key; |
---|
[8316] | 160 | |
---|
[7251] | 161 | regArgs.push_back( arg ); |
---|
[8316] | 162 | return true; |
---|
[7251] | 163 | } |
---|
[7241] | 164 | |
---|
[7251] | 165 | |
---|