From: hachti Date: Mon, 20 Nov 2006 07:31:13 +0000 (+0000) Subject: *** empty log message *** X-Git-Url: http://gitweb.hachti.de/?a=commitdiff_plain;h=632a71a231a9bdd73a670ec686e13dd049921182;p=h316.git *** empty log message *** --- diff --git a/pc-tools/ldc2/src/argread.cpp b/pc-tools/ldc2/src/argread.cpp deleted file mode 100644 index 116dbb8..0000000 --- a/pc-tools/ldc2/src/argread.cpp +++ /dev/null @@ -1,136 +0,0 @@ -#include "argread.hh" -#include - -argreader::argreader(string n_name){ - progname=n_name; -} - -void argreader::add_param (string shortname, string longname, - string description, int * status, - string * target, string placeholder){ - if (target!=NULL) if(status!=NULL) - arguments.insert(arguments.end(),parameter(shortname,longname,description,status,target,placeholder)); -} - -void argreader::add_free_param(string placeholder, string description, int * status, string * target){ - if (target!=NULL) if(status!=NULL) - free_arguments.insert(free_arguments.end(),free_parameter(placeholder,description,status,target)); -} - -vector argreader::read_args(int argc, char ** args){ - vector result; - vector argv; - for (char ** akt=args; *akt ;akt++) argv.insert(argv.end(),string(*akt)); - - unsigned int free_parms_count=0; - for (vector::iterator akt_p=argv.begin()+1;akt_psubstr(0,2)=="--")&&(free_parms_count==0)){ - int found=0; - for (vector::iterator parm_p=arguments.begin();parm_psubstr(2,parm_p->longname.length())==parm_p->longname){ - found=1; - *(parm_p->status)=1; - if (parm_p->target){ - if (akt_p->length()>2+parm_p->longname.length()){ - *(parm_p->target)=akt_p->substr(2+parm_p->longname.length()); - - } else // Word not long enough - if (akt_p+1status)=1; - *(parm_p->target)=*(++akt_p); - } else { // No next word :-( - result.insert(result.end(), - "Parameter --"+parm_p->longname+parm_p->placeholder+" needs an Argument!"); - } - } // arg needed - } - } // search for loop - if (!found) result.insert(result.end(),"Unknown parameter: "+*akt_p); - } else { // No -- param, now look for switches - if (((*akt_p)[0]=='-')&&(free_parms_count==0)){ - int stop_char_loop=0; - for (unsigned int pos=1; poslength()&& !stop_char_loop ;pos++){ - int found=0; - for (vector::iterator parm_p=arguments.begin();parm_pshortname[0]==(*akt_p)[pos]){ - found=1; - (*parm_p->status)=1; - if (parm_p->target){ // Need argument - if (akt_p->length()>pos+1){ - *(parm_p->target)=akt_p->substr(pos+1); - stop_char_loop=1; - } else { // Word not long enough - printf("s word next\n"); - if (akt_p+1target)=*(++akt_p); - stop_char_loop=1; - } else { // No next word :-( - result.insert(result.end(), - "Parameter --"+parm_p->longname+parm_p->placeholder+" needs an Argument!"); - } - } - } // arg needed - } //if match - } //args loop - if (!found) result.insert(result.end(),"Unknown switch: "+akt_p->substr(pos,1)); - } // char loop - }// switch found - else{ // no switch found - if (free_parms_countfree_arguments.size()) result.insert(result.begin(),"Too many arguments!"); - if (!result.empty()) get_help(result); - return result; -} - - -void argreader::get_help(vector & result){ - result.insert(result.end(),"Usage:"); - string line=" "+progname+" "; - for (vector::iterator parm_p=arguments.begin();parm_pshortname; - if (parm_p->target!=0) line+=parm_p->placeholder; - line+="] "; - } - result.insert(result.end(),line); -} - -vector argreader::get_help(){ - vector result; - get_help(result); - return result; -} - - -/**************************************************/ - -argreader::parameter::parameter(string n_shortname, string n_longname,string n_description, int * n_status, - string * n_target, string n_placeholder){ - shortname=n_shortname; - longname=n_longname; - description=n_description; - status=n_status; - target=n_target; - placeholder=n_placeholder; - if (status) *status=0; -} - -argreader::free_parameter::free_parameter( string n_placeholder, string n_description, - int * n_status, string * n_target){ - description=n_description; - status=n_status; - target=n_target; - placeholder=n_placeholder; - if (status) *status=0; -} - - - - diff --git a/pc-tools/ldc2/src/argread.hh b/pc-tools/ldc2/src/argread.hh deleted file mode 100644 index 7cd3aff..0000000 --- a/pc-tools/ldc2/src/argread.hh +++ /dev/null @@ -1,76 +0,0 @@ -#ifndef ARGREAD_H -#define ARGREAD_H - -#include -#include - -using namespace std; - -#ifndef NULL -#define NULL (void*)0 -#endif - -class argreader{ -public: - argreader(string n_name); - - /*! - *\brief add a parameter to be looked for - *\param shortname A character for the short form. For example the 'h' in -h - *\param longname The double dash longname. For example 'input_file=' in --input_file= or - * 'ignore_errors' in --ignore--errors - *\param description A parameter description. - *\param status Pointer to an integer. Will be set to 1 if arg found. - *\target Pointer to a value should be read. If no value is needed, pass NULL. - *\placeholder A placeholder for the documentation. For example "" in -f - */ - void add_param (string shortname, string longname, string description, int * status, - string * target=NULL, string placeholder=string("")); - - /*! - * Read in the args passed to main(). - *\returns empty vector on success or the error messages to be output. - */ - vector read_args(int argc, char ** args); - - - vector & get_free_args(); - - void get_help(vector & ); - vector get_help(); - - void add_free_param(string placeholder, string description, int * status, string * target); - - - -private: - class parameter{ - public: - parameter (string shortname, string longname,string description, int * status, - string * target=NULL, string placeholder=string("")); - string shortname; - string longname; - int * status; - string * target; - string description; - string placeholder; - }; - - class free_parameter{ - public: - free_parameter (string placeholder,string description, - int * status, string * target); - int * status; - string * target; - string description; - string placeholder; - }; - - vectorarguments; - vectorfree_arguments; - - string progname; -}; - - -#endif diff --git a/pc-tools/ldc2/src/argument_reader.cpp b/pc-tools/ldc2/src/argument_reader.cpp new file mode 100644 index 0000000..e927a37 --- /dev/null +++ b/pc-tools/ldc2/src/argument_reader.cpp @@ -0,0 +1,268 @@ +#include "argument_reader.hh" +#include + +/*! + *\brief Constructor. + * + * This constructor makes a new argument_reader ready to use. + *\arg app_name Name of the application as mentioned in the + * "Use: ..." help message. + */ +argument_reader::argument_reader(string app_name){ + progname=app_name; +} + + +/*! + *\brief Add a new parameter to be searched for. + *\param shortname A character for the short form. + * For example the 'h' in -h + *\param longname The double dash longname. For example + * "input_file=" in --input_file= or + * "ignore_errors" in --ignore--errors + *\param description A detailed parameter description. + *\param status Pointer to an integer. Will be set to 1 if arg found. + *\target Pointer to a value should be read. + * If no value is needed, pass NULL. + *\placeholder A placeholder for the documentation. + * For example "" in -f + */ +void argument_reader::add_param (string shortname, string longname, + string description, int * status, + string * target, string placeholder){ + + if(status!=NULL) arguments.insert(arguments.end(), + parameter(shortname,longname, description, + status,target,placeholder) + ); +} + + +/*! + *\brief Add an accepted argument to the argument reader. + *\param placeholder Something like "". + *\param description Text describing the argument. + *\param status A pointer to a status variable. Will be set to 0 immediately. + * If the argument is filled in, it will be set to 1. + *\param target A pointer to a c++ string where the argument goes to. + * + *\note Arguments are filled in the order of adding them to the + * argument reader. + * There would be no other way to determine the order. + */ +void argument_reader::add_argument(string placeholder, string description, int * status, string * target){ + if (target!=NULL) if(status!=NULL) + free_arguments.insert(free_arguments.end(),free_parameter(placeholder,description,status,target)); +} + + +/*! + * Read in the args passed to main(). + *\returns empty vector on success or the error messages to be output. + */ +vector argument_reader::read_args(int argc, char ** args){ + vector result; + vector argv; + for (char ** akt=args; *akt ;akt++) argv.insert(argv.end(),string(*akt)); + + unsigned int free_parms_count=0; + for (vector::iterator akt_p=argv.begin()+1;akt_psubstr(0,2)=="--")&&(free_parms_count==0)){ + int found=0; + for (vector::iterator parm_p=arguments.begin();parm_psubstr(2,parm_p->longname.length())==parm_p->longname){ + found=1; + *(parm_p->status)=1; + if (parm_p->target){ + if (akt_p->length()>2+parm_p->longname.length()){ + *(parm_p->target)=akt_p->substr(2+parm_p->longname.length()); + + } else // Word not long enough + if (akt_p+1status)=1; + *(parm_p->target)=*(++akt_p); + } else { // No next word :-( + result.insert(result.end(), + "Parameter --"+parm_p->longname+parm_p->placeholder+" needs an Argument!"); + } + } // arg needed + } + } // search for loop + if (!found) result.insert(result.end(),"Unknown parameter: "+*akt_p); + } else { // No -- param, now look for switches + if (((*akt_p)[0]=='-')&&(free_parms_count==0)){ + int stop_char_loop=0; + for (unsigned int pos=1; poslength()&& !stop_char_loop ;pos++){ + int found=0; + for (vector::iterator parm_p=arguments.begin();parm_pshortname[0]==(*akt_p)[pos]){ + found=1; + (*parm_p->status)=1; + if (parm_p->target){ // Need argument + if (akt_p->length()>pos+1){ + *(parm_p->target)=akt_p->substr(pos+1); + stop_char_loop=1; + } else { // Word not long enough + if (akt_p+1target)=*(++akt_p); + stop_char_loop=1; + } else { // No next word :-( + result.insert(result.end(), + "Parameter --"+parm_p->longname+parm_p->placeholder+" needs an Argument!"); + } + } + } // arg needed + } //if match + } //args loop + if (!found) result.insert(result.end(),"Unknown switch: "+akt_p->substr(pos,1)); + } // char loop + }// switch found + else{ // no switch found + if (free_parms_countfree_arguments.size()) result.insert(result.begin(),"Too many arguments!"); + if (!result.empty()){ + result.insert(result.begin(),"Error!"); + result.insert(result.begin(),""); + } +// if (!result.empty()){ +// get_help(result); +// } + return result; +} + +/*! + *\brief Generate help. + *\arg target Reference to a vector to which lots of helpful + * strings are appended. + */ +void argument_reader::get_help(vector & result){ + + result.insert(result.end(),""); + string line="Usage: "+progname; + for (vector::iterator parm_p=arguments.begin();parm_pshortname; + if (parm_p->target!=0) addstr+=parm_p->placeholder; + addstr+="]"; + if (line.length()+addstr.length()>79){ + result.insert(result.end(),line); + line=string(7+progname.length(),' '); + } + line+=addstr; + } + + for (vector::iterator parm_p=free_arguments.begin();parm_pplaceholder.length()>79){ + result.insert(result.end(),line); + line=string(7+progname.length(),' '); + } + line+=" ["+parm_p->placeholder+"]"; + } + result.insert(result.end(),line); + + /*******************************/ + + vector left,right; + for (vector::iterator parm_p=arguments.begin();parm_pdescription; + string st2=" -"+parm_p->shortname; + if (parm_p->target)st2+=" "+parm_p->placeholder; + st2+=", --"+parm_p->longname; + if (parm_p->target)st2+=parm_p->placeholder; + left.insert(left.end(),st2); + right.insert(right.end(),line); + } + + for (vector::iterator parm_p=free_arguments.begin();parm_pdescription; + st2+=" "+parm_p->placeholder; + left.insert(left.end(),st2); + right.insert(right.end(),line); + } + + if (arguments.size()){ + result.insert(result.end(),""); + result.insert(result.end(),"Options:"); + } + + unsigned int max_width=0; + for (unsigned int c=0; cmax_width) max_width=left[c].length(); + for (unsigned int c=0; c80){ // Too long??? + int limit=nl.find_last_of(' ',80); + printf("limit:%i\n",limit); + result.insert(result.end(),nl.substr(0,limit)); + nl=string(max_width+2,' ')+nl.substr(limit+1); + } + result.insert(result.end(),nl); + } + + if (free_arguments.size()){ + result.insert(result.end(),""); + result.insert(result.end(),"Arguments:"); + } + + max_width=0; + for (unsigned int c=arguments.size(); cmax_width) max_width=left[c].length(); + for (unsigned int c=arguments.size(); c80){ // Too long??? + int limit=nl.find_last_of(' ',80); + printf("limit:%i\n",limit); + result.insert(result.end(),nl.substr(0,limit)); + nl=string(max_width+2,' ')+nl.substr(limit+1); + } + + result.insert(result.end(),nl); + } + result.insert(result.end(),""); +} + +/*! + *\brief Generate help. + *\return A vector containing many helpful strings for the user. + */ +vector argument_reader::get_help(){ + vector result; + get_help(result); + return result; +} + + +/**************************************************/ + +argument_reader::parameter::parameter(string n_shortname, string n_longname,string n_description, int * n_status, + string * n_target, string n_placeholder){ + shortname=n_shortname; + longname=n_longname; + description=n_description; + status=n_status; + target=n_target; + placeholder=n_placeholder; + if (status) *status=0; +} + +argument_reader::free_parameter::free_parameter( string n_placeholder, string n_description, + int * n_status, string * n_target){ + description=n_description; + status=n_status; + target=n_target; + placeholder=n_placeholder; + if (status) *status=0; +} diff --git a/pc-tools/ldc2/src/argument_reader.hh b/pc-tools/ldc2/src/argument_reader.hh new file mode 100644 index 0000000..1222176 --- /dev/null +++ b/pc-tools/ldc2/src/argument_reader.hh @@ -0,0 +1,75 @@ +#ifndef ARGUMENT_READER_H +#define ARGUMENT_READER_H + +#include +#include + +using namespace std; + +/*! + *\brief Hachti's wonderful commandline parser. + * + * This class is designed to do all the work with the parameters passed to + * the main() routine of a program.\n + * Usage:\n + * - Create an instance. + * - Add Parameters/switches with add_param(). + * - Add Arguments with add_argument(). + * - Call read_args() with your main routine's arguments.\ + * The vector returned by read_args() contains all error \ + * messages. When it's empty, everything is fine! + * - Call get_help() if you want nice formatted help output. \ + * (You want!!!) + * Sould be easy to use..... Enjoy. + */ +class argument_reader{ + +public: + argument_reader(string n_name); + + void add_param (string shortname, + string longname, + string description, + int * status, + string * target=NULL, + string placeholder=string("") + ); + + vector read_args(int argc, char ** args); + + void get_help(vector & target); + vector get_help(); + + void add_argument(string placeholder, string description, int * status, string * target); + +private: + class parameter{ + public: + parameter (string shortname, string longname,string description, int * status, + string * target=NULL, string placeholder=string("")); + string shortname; + string longname; + int * status; + string * target; + string description; + string placeholder; + }; + + class free_parameter{ + public: + free_parameter (string placeholder,string description, + int * status, string * target); + int * status; + string * target; + string description; + string placeholder; + }; + + vectorarguments; + vectorfree_arguments; + + string progname; +}; + + +#endif diff --git a/pc-tools/ldc2/src/main.cpp b/pc-tools/ldc2/src/main.cpp index f827410..237d997 100644 --- a/pc-tools/ldc2/src/main.cpp +++ b/pc-tools/ldc2/src/main.cpp @@ -21,9 +21,6 @@ void tape_stop(void* m){ printf("tape_stop\n"); } - - - void dump_vector(vector arguments){ for (vector::iterator iter=arguments.begin();iter"); ar.add_param("h","help","Give help",&help_needed); - ar.add_free_param("","File to read data from",&file_set,&filename); - - - + ar.add_param("n","name=","Enter other name",&name_set,&name,""); + ar.add_param("n","name=","Enter other name",&name_set,&name,""); + ar.add_param("n","name=","Enter other name",&name_set,&name,""); + ar.add_param("n","name=","Enter other name Gelaber Gelaber Gelaber Gelaber M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M M",&name_set,&name,""); + ar.add_param("n","name=","Enter other name",&name_set,&name,""); + ar.add_param("n","name=","Enter other name",&name_set,&name,""); + ar.add_param("n","name=","Enter other name",&name_set,&name,""); + ar.add_param("n","name=","Enter other name",&name_set,&name,""); + ar.add_param("n","name=","Enter other name",&name_set,&name,""); + ar.add_param("n","name=","Enter other name",&name_set,&name,""); + ar.add_param("n","name=","Enter other name",&name_set,&name,""); + ar.add_param("n","name=","Enter other name",&name_set,&name,""); + ar.add_param("n","name=","Enter other name",&name_set,&name,""); + ar.add_param("n","name=","Enter other name",&name_set,&name,""); + ar.add_param("n","name=","Enter other name",&name_set,&name,""); + ar.add_param("n","name=","Enter other name",&name_set,&name,""); + ar.add_param("n","name=","Enter other name",&name_set,&name,""); + ar.add_param("n","name=","Enter other name",&name_set,&name,""); + ar.add_param("n","name=","Enter other name",&name_set,&name,""); + ar.add_param("n","name=","Enter other name",&name_set,&name,""); + ar.add_param("n","name=","Enter other name",&name_set,&name,""); + ar.add_free_param("","File to read data from",&file_set,&filename); + dump_vector(ar.read_args(argc,args)); - if (help_needed==1) dump_vector(ar.get_help()); + if (help_needed){ + dump_vector(ar.get_help()); + exit(1); + } - printf("Hallo %s!\n",name.c_str()); + printf("\n\n\nHallo %s!\n",name.c_str()); exit(0); tape_block * myblock=0; do{