Main Page | Class Hierarchy | Compound List | File List | Compound Members | File Members | Related Pages

argumentparser.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2003, Frederik Holljen
00003  * All Rights Reserved.
00004  *
00005  * See COPYING for licensing.
00006  */
00007 
00008 
00009 /*
00010  * This file was copied from the open source project IRCan.
00011  *
00012  * Copyright (C) 2003, Frederik Holljen <larson@users.sourceforge.net>
00013  *
00014  * This program is free software; you can redistribute it and/or modify
00015  * it under the terms of the GNU General Public License as published by
00016  * the Free Software Foundation; either version 2 of the License, or
00017  * (at your option) any later version.
00018  *
00019  * This program is distributed in the hope that it will be useful,
00020  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00022  * GNU General Public License for more details.
00023  *
00024  * You should have received a copy of the GNU General Public License
00025  * along with this program; if not, write to the Free Software
00026  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00027  *
00028  * $Id: argumentparser.cpp,v 1.2 2003/11/28 15:41:27 bosman Exp $
00029  */
00030 
00031 #include "argumentparser.hpp"
00032 #include <iostream>
00033 #include <string.h> // for strlen
00034 
00051 ArgumentParser::ArgumentParser()
00052 {
00053     NoParamArgs = "";
00054 }
00055 
00060 ArgumentParser::~ArgumentParser()
00061 {
00062 }
00063 
00073 bool ArgumentParser::parseArguments( int argc, char **argv )
00074 {
00075     if( argc == 1 ) // nothing to do
00076         return true;
00077 
00078     string pendingArg;
00079     for( int i = 1; i < argc; i++ )
00080     {
00081         uint len = strlen( argv[i] );
00082         if( argv[i][0] == '-' && len > 1) // if it's only the '-' ignore it.
00083         {
00084             if( !pendingArg.empty() )
00085             {
00086                 NoParamArgs += pendingArg;
00087                 pendingArg = "";
00088             }
00089 
00090             uint j = 1;
00091             for( ; j < len - 1; j++ )
00092             {
00093                 NoParamArgs += argv[i][j];
00094             }
00095             pendingArg = argv[i][j];
00096         }
00097         else if( !pendingArg.empty() )
00098         {
00099             ArgsMap[pendingArg] = argv[i];
00100             pendingArg = "";
00101         }
00102         else // this could be a mode..for now it's an error
00103         {
00104             return false;
00105         }
00106     }
00107 
00108     if( !pendingArg.empty() )
00109     {
00110         NoParamArgs += pendingArg;
00111     }
00112     return true;
00113 }
00114 
00118 uint ArgumentParser::numArguments() const
00119 {
00120     return ArgsMap.size() + NoParamArgs.length();
00121 }
00122 
00127 typedef map<string, string>::const_iterator mapIt;
00128 string ArgumentParser::argValue( const string &arg ) const
00129 {
00130     mapIt it = ArgsMap.find( arg );
00131     if( it != ArgsMap.end() )
00132     {
00133         return (*it).second;
00134     }
00135 
00136     return "";
00137 }
00138 
00144 bool ArgumentParser::boolArgValue( const string &arg ) const
00145 {
00146     mapIt it = ArgsMap.find( arg );
00147     if( it != ArgsMap.end() )
00148     {
00149         if( (*it).second == "true" ||
00150             (*it).second == "1" ||
00151             (*it).second == "on" )
00152         {
00153             return true;
00154         }
00155     }
00156 
00157     return false;
00158 }
00159 
00163 bool ArgumentParser::singleArgSet( char arg ) const
00164 {
00165     if( NoParamArgs.find( arg ) != string::npos )
00166         return true;
00167     return false;
00168 }

Generated on Mon Dec 1 14:26:27 2003 for Ck by doxygen 1.3.3