00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 #ifndef         TCOMMANDLINE_H_INCLUDED
00031 #include        "tCommandLine.h"
00032 #endif
00033 
00034 #include    "tLocale.h"
00035 #include    "tConfiguration.h"
00036 #include    "tException.h"
00037 
00038 #ifdef WIN32
00039 #include    <windows.h>
00040 #include    <direct.h>
00041 #endif
00042 
00043 #undef  INLINE_DEF
00044 #define INLINE_DEF
00045 
00046 static tCommandLineAnalyzer * s_commandLineAnalyzerAnchor;
00047 
00048 static void quitWithMessagePrepare( const char* message )
00049 {
00050 #ifdef WIN32
00051 #ifndef DEDICATED
00052 #define USEBOX
00053 #endif
00054 #endif
00055 
00056 #ifdef USEBOX
00057     int result = MessageBox (NULL, message , "Message", MB_OK);
00058 #else
00059     std::cout << message;
00060 #endif
00061 
00062     tLocale::Clear();
00063 }
00064 
00065 static void quitWithMessage( const char* message )
00066 {
00067     tLocale::Clear();
00068     quitWithMessagePrepare( message );
00069     throw 1;
00070 
00071     
00072     
00073 }
00074 
00075 
00076 
00077 #define QUIT(x) { std::ostringstream s; s << x; quitWithMessage(s.str().c_str()); name_.Clear();}
00078 
00079 bool tCommandLineData::Analyse(int argc,char **argv)
00080 {
00081 #ifdef DEBUG_X
00082 #ifdef WIN32
00083 #define getcwd _getcwd
00084 #endif
00085 
00086     char * cwd = getcwd(0,0);
00087     tERR_MESSAGE( "Executable: " << argv[0] << ", CWD: " << cwd );
00088     free( cwd );
00089 #endif
00090 
00091     tCommandLineParser parser( argc, argv );
00092     {
00093         tASSERT( programVersion_ );
00094 
00095         char const * run = parser.Current();
00096         while (*run)
00097         {
00098             if (*run == '\\' || *run == '/')
00099                 name_ = run+1;
00100             run++;
00101         }
00102 
00103         if ( name_.Len() <= 3 )
00104         {
00105             name_ = "Armagetron";
00106         }
00107     }
00108 
00109 
00110     
00111     {
00112         tCommandLineAnalyzer * commandLineAnalyzer = s_commandLineAnalyzerAnchor;
00113         while ( commandLineAnalyzer )
00114         {
00115             commandLineAnalyzer->Initialize( parser );
00116             commandLineAnalyzer = commandLineAnalyzer->Next();
00117         }
00118     }
00119 
00120     parser.Advance();
00121 
00122     
00123 
00124 #ifndef WIN32   
00125 #define HELPAVAIL
00126 #endif
00127 #ifdef DEDICATED  
00128 #define HELPAVAIL
00129 #endif
00130 
00131     while ( !parser.End() )
00132     {
00133         if ( parser.GetSwitch( "--help", "-h" ) )
00134         {
00135             {
00136                 std::ostringstream s;
00137                 s << "\n\nUsage: " << name_ << " [Arguments]\n\n"
00138                 << "Possible arguments:\n\n";
00139                 s << "-h, --help                   : print this message\n";
00140 #ifdef HELPAVAIL
00141                 s << "--doc                        : print documentation for all console commands\n";
00142 #endif
00143                 s << "-v, --version                : print version number\n\n";
00144 
00145                 
00146                 tCommandLineAnalyzer * commandLineAnalyzer = s_commandLineAnalyzerAnchor;
00147                 while ( commandLineAnalyzer )
00148                 {
00149                     commandLineAnalyzer->Help( s );
00150                     commandLineAnalyzer = commandLineAnalyzer->Next();
00151                     s << "\n";
00152                 }
00153 
00154                 s << "\n";
00155 
00156                 name_.Clear();
00157                 quitWithMessagePrepare( s.str().c_str() );
00158             }
00159 
00160             return false;
00161             
00162         }
00163 #ifdef HELPAVAIL
00164         else if ( parser.GetSwitch( "--doc") )
00165         {
00166             doc_ = true;
00167         }
00168 #endif
00169         else if ( parser.GetSwitch( "--version", "-v") )
00170         {
00171             QUIT( "This is " << name_ << " version " << *programVersion_ << ".\n" );
00172         }
00173         else
00174         {
00175             
00176             tCommandLineAnalyzer * commandLineAnalyzer = s_commandLineAnalyzerAnchor;
00177             bool success = false;
00178             while ( commandLineAnalyzer )
00179             {
00180                 if ( success = commandLineAnalyzer->Analyze( parser ) )
00181                     break;
00182                 commandLineAnalyzer = commandLineAnalyzer->Next();
00183             }
00184             if ( success )
00185                 continue;
00186 
00187             QUIT( "\n\nUnknown command line option " << parser.Current() << ". Type " << name_ << " -h to get a list of possible options.\n\n" );
00188         }
00189     }
00190 
00191     return true;
00192 }
00193 
00194 bool tCommandLineData::Execute()
00195 {
00196     tString name;
00197 
00198     if ( doc_ )
00199     {
00200         std::cout << "Available console commands/config file settings:\n\n";
00201         tConfItemBase::DocAll( std::cout );
00202 
00203         return false;
00204         
00205     }
00206 
00207     return true;
00208 }
00209 
00210 
00211 
00212 
00213 
00214 
00220 
00221 
00222 bool tCommandLineParser::GetSwitch( char const * option, char const * option_short )
00223 {
00224     if ( End() )
00225         return false;
00226 
00227     char * argument = argv[index];
00228     tASSERT( argument );
00229     if ( !strcmp(argument,option) || ( option_short && !strcmp(argument,option_short ) ) )
00230     {
00231         index++;
00232         return true;
00233     }
00234 
00235     return false;
00236 }
00237 
00238 
00239 
00240 
00241 
00242 
00249 
00250 
00251 bool tCommandLineParser::GetOption( tString & target, char const * option, char const * option_short )
00252 {
00253     if ( End() )
00254         return false;
00255 
00256     char * argument = argv[index];
00257     tASSERT( argument );
00258     if ( GetSwitch( option, option_short ) )
00259     {
00260         if ( !End() )
00261         {
00262             target = argv[index];
00263             index++;
00264             return true;
00265         }
00266         else
00267         {
00268             index--;
00269             tString name_;
00270             QUIT( "  " << argument << " needs another argument.\n" );
00271         }
00272     }
00273 
00274     return false;
00275 }
00276 
00277 
00278 
00279 
00280 
00281 
00285 
00286 
00287 bool tCommandLineParser::End( void ) const
00288 {
00289     return ( index >= argc );
00290 }
00291 
00292 
00293 
00294 
00295 
00296 
00301 
00302 
00303 tCommandLineParser::tCommandLineParser( int a_argc, char * * a_argv )
00304         : argc( a_argc ), argv( a_argv ), index( 0 )
00305 {
00306 }
00307 
00308 
00309 
00310 
00311 
00312 
00315 
00316 
00317 
00318 
00319 
00320 
00321 
00322 
00323 
00324 
00325 
00326 
00327 
00331 
00332 
00333 const char * tCommandLineParser::Executable( void ) const
00334 {
00335     return argv[ 0 ];
00336 }
00337 
00338 
00339 
00340 
00341 
00342 
00346 
00347 
00348 const char * tCommandLineParser::Current( void ) const
00349 {
00350     return argv[ index ];
00351 }
00352 
00353 
00354 
00355 
00356 
00357 
00360 
00361 
00362 void tCommandLineParser::Advance( void )
00363 {
00364     tASSERT( !End() );
00365     index ++;
00366 }
00367 
00368 
00369 
00370 
00371 
00372 
00373 
00376 
00377 
00378 tCommandLineAnalyzer::tCommandLineAnalyzer( void )
00379         : tListItem< tCommandLineAnalyzer >( s_commandLineAnalyzerAnchor )
00380 {
00381 }
00382 
00383 
00384 
00385 
00386 
00387 
00390 
00391 
00392 tCommandLineAnalyzer::~tCommandLineAnalyzer( void )
00393 {
00394 }
00395 
00396 
00397 
00398 
00399 
00400 
00404 
00405 
00406 void tCommandLineAnalyzer::DoInitialize( tCommandLineParser & parser )
00407 {
00408 }
00409 
00410 
00411 
00412 
00413 
00414 
00419 
00420 
00421 bool tCommandLineAnalyzer::DoAnalyze( tCommandLineParser & parser )
00422 {
00423     return false;
00424 }
00425 
00426 
00427 
00428 
00429 
00430 
00434 
00435 
00436 void tCommandLineAnalyzer::DoHelp( std::ostream & s )
00437 {
00438 }
00439