args.cc

Go to the documentation of this file.
00001 /*
00002  *  Copyright (C) 2000-2002  The Exult Team
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017  */
00018 
00019 #ifdef HAVE_CONFIG_H
00020 #  include <config.h>
00021 #endif
00022 
00023 #ifndef ALPHA_LINUX_CXX
00024 #  include <iostream>
00025 #endif
00026 
00027 #include "args.h"
00028 
00029 #ifndef UNDER_CE
00030 using std::cerr;
00031 using std::endl;
00032 using std::string;
00033 using std::strtol;
00034 using std::strtoul;
00035 #endif
00036 
00037 void  Args::declare(const char *s,bool *b,bool defval)
00038 {
00039   string  ss;
00040 
00041   ss=s;
00042 
00043   Opts o;
00044   o.option=ss;
00045   o.bval=b;
00046   o.dbval=defval;
00047   o.valuetype=Opts::type_bool;
00048   options.push_back(o);
00049 }
00050 
00051 void  Args::declare(const char *s,string *b,const char *defval)
00052 {
00053   string  ss;
00054 
00055   ss=s;
00056 
00057   Opts o;
00058   o.option=ss;
00059   o.sval=b;
00060   o.dsval=defval?defval:"";
00061   *o.sval=defval?defval:"";
00062   o.valuetype=Opts::type_string;
00063   options.push_back(o);
00064 }
00065 
00066 void  Args::declare(const char *s,int *b,int defval)
00067 {
00068   string  ss;
00069 
00070   ss=s;
00071 
00072   Opts o;
00073   o.option=ss;
00074   o.ival=b;
00075   o.dival=defval;
00076   *o.ival=defval;
00077   o.valuetype=Opts::type_int;
00078   options.push_back(o);
00079 }
00080 
00081 void  Args::declare(const char *s,uint32 *b,uint32 defval)
00082 {
00083   string  ss;
00084 
00085   ss=s;
00086 
00087   Opts o;
00088   o.option=ss;
00089   o.uval=b;
00090   o.duval=defval;
00091   *o.uval=defval;
00092   o.valuetype=Opts::type_unsigned;
00093   options.push_back(o);
00094 }
00095 
00096 void  Args::process(int argc,char **argv)
00097 {
00098   for(int i=1;i<argc;i++)
00099   {
00100     for(unsigned int j=0;j<options.size() && i<argc;j++)
00101     {
00102       switch(options[j].valuetype)
00103       {
00104         case Opts::no_type:
00105           continue;
00106         case Opts::type_bool:
00107           if(options[j].option==argv[i])
00108             *(options[j].bval)=options[j].dbval;
00109           break;
00110         case Opts::type_string:
00111         {
00112           if(options[j].option==argv[i])
00113           {
00114             // We want the _next_ argument
00115             if(++i>=argc)
00116             {
00117               cerr << "Data not specified for argument '" << options[j].option <<"'. Using default." << endl;
00118               break;
00119             }
00120             *(options[j].sval)=argv[i];
00121           }
00122           break;
00123         }
00124         case Opts::type_int:
00125         {
00126 //          char buf[64];
00127           if(options[j].option==argv[i])
00128           {
00129             // We want the _next_ argument
00130             if(++i>=argc)
00131             {
00132               cerr << "Data not specified for argument '" << options[j].option <<"'. Using default." << endl;
00133               break;
00134             }
00135             *(options[j].ival)=strtol(argv[i],0,10);
00136           }
00137           break;
00138         }
00139         case Opts::type_unsigned:
00140         {
00141 //          char buf[64];
00142           if(options[j].option==argv[i])
00143           {
00144             // We want the _next_ argument
00145             if(++i>=argc)
00146             {
00147               cerr << "Data not specified for argument '" << options[j].option <<"'. Using default." << endl;
00148               break;
00149             }
00150             *(options[j].uval)=strtoul(argv[i],0,10);
00151           }
00152           break;
00153         }
00154       }
00155     }
00156   }
00157 
00158 }

Generated on Mon Jul 9 14:42:40 2007 for ExultEngine by  doxygen 1.5.1