Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/parser/preferences/cmd_line_prefs_reader.cc @ 9152

Last change on this file since 9152 was 8316, checked in by bensch, 18 years ago

trunk: fixed most -Wall warnings… but there are still many missing :/

File size: 4.1 KB
Line 
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
20#include "preferences.h"
21
22using namespace std;
23
24RegistredArgs CmdLinePrefsReader::regArgs;
25
26/**
27 * standard constructor
28*/
29CmdLinePrefsReader::CmdLinePrefsReader( )
30{
31
32}
33
34
35/**
36 * standard deconstructor
37*/
38CmdLinePrefsReader::~CmdLinePrefsReader ()
39{
40}
41
42bool CmdLinePrefsReader::callBack( ArgTableEntry entry, void * data, const std::string & arg, const std::vector<MultiType> & argArgs )
43{
44  CallbackData * cbd = (CallbackData *)data;
45
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());
56
57      if ( key == "" || section == "" || argArgs.size() != 1 )
58      {
59        PRINTF(1)("usage: --set-section.key=value\n");
60        return false;
61      }
62
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();
68      return true;
69    }
70    case ID_HELP:
71      cbd->parser->showHelp();
72      return true;
73  }
74
75  if ( entry.id >= ID_PREFS && entry.id  < regArgs.size() + ID_PREFS)
76  {
77    if ( regArgs[entry.id - ID_PREFS].value == "%arg%" )
78    {
79      assert( argArgs.size() == 1 );
80
81      cbd->iniEntries.push_back( IniEntry() );
82      cbd->iniEntries.back().section = regArgs[entry.id - ID_PREFS].section;
83      cbd->iniEntries.back().key = regArgs[entry.id - ID_PREFS].key;
84      cbd->iniEntries.back().value = argArgs[0].getString();
85    }
86    else
87    {
88      cbd->iniEntries.push_back( IniEntry() );
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;
92    }
93  }
94  else
95  {
96    assert(false);
97  }
98
99  return true;
100}
101
102
103bool CmdLinePrefsReader::parse( int argc, char ** argv )
104{
105  CmdLineParser parser;
106
107  parser.add( ID_HELP, "help", 'h', 0, "", "Shows this help and exits");
108
109  for (unsigned int i = 0; i<regArgs.size(); i++ )
110  {
111    if ( regArgs[i].value == "%arg%" )
112    {
113      parser.add( ID_PREFS + i, regArgs[i].longOption, regArgs[i].shortOption, 1, regArgs[i].argName, regArgs[i].help );
114    }
115    else
116    {
117      parser.add( ID_PREFS + i, regArgs[i].longOption, regArgs[i].shortOption, 0, "", regArgs[i].help );
118    }
119  }
120
121  parser.add( ID_SET_INI, "set-%", '\0', 1, "value", "Override a configuration element." );
122
123  CallbackData cbd;
124
125  cbd.parser = &parser;
126
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  }
139
140  return true;
141}
142
143bool 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;
146
147  arg.longOption = longOption;
148  while ( arg.longOption.find("_") != std::string::npos )
149  {
150    arg.longOption.replace( arg.longOption.find("_"), 1, "-" );
151  }
152  arg.shortOption = shortOption;
153  if ( arg.shortOption == '_' )
154    arg.shortOption = '\0';
155  arg.value = value;
156  arg.help = help;
157  arg.argName = argName;
158  arg.section = section;
159  arg.key = key;
160
161  regArgs.push_back( arg );
162  return true;
163}
164
165
Note: See TracBrowser for help on using the repository browser.