00001 #ifndef EDG_WORKLOAD_COMMON_UTILITIES_LINEPARSER_H
00002 #define EDG_WORKLOAD_COMMON_UTILITIES_LINEPARSER_H
00003
00004 #include <map>
00005 #include <vector>
00006 #include <string>
00007
00008 #include <unistd.h>
00009 #include <getopt.h>
00010
00011 #include "mixed.h"
00012
00013 COMMON_NAMESPACE_BEGIN {
00014
00015 namespace utilities {
00016
00017 typedef struct option option_s;
00018
00019 class LineOption {
00020 public:
00021 inline char get_value( void ) const { return this->o_value; }
00022 inline int has_arguments( void ) const { return this->o_has_arguments; }
00023
00024 inline const std::string &get_name( void ) const { return this->o_name; }
00025 inline const std::string &get_help( void ) const { return this->o_help; }
00026
00027 inline option_s get_struct( void ) const;
00028
00029 char o_value;
00030 int o_has_arguments;
00031 std::string o_name, o_help;
00032 };
00033
00034 class LineParser;
00035
00036 class ParserData {
00037 friend class LineParser;
00038
00039 public:
00040 enum par_num_t { one_or_more = -2, zero_or_more, zero_args };
00041
00042 inline int arguments( void ) const { return this->pd_paramnumber; }
00043 inline const std::string &program( void ) { return this->pd_progname; }
00044 inline const std::string &program( void ) const { return this->pd_progname; }
00045
00046 void usage( std::ostream &os ) const;
00047
00048 private:
00049 ParserData( const std::vector<LineOption> &options, int paramnumber );
00050 ~ParserData( void );
00051
00052 int pd_paramnumber;
00053 std::string pd_optstring, pd_progname;
00054 std::vector<option_s> pd_options;
00055 std::map<char, int> pd_argmap;
00056 std::map<char, std::string> pd_help;
00057 };
00058
00059 class LineParser {
00060 public:
00061 LineParser( std::vector<LineOption> &options, int paramnumber = 0 );
00062 ~LineParser( void );
00063
00064 inline bool is_present( char val ) const { return( this->lp_map.count(val) != 0 ); }
00065 inline const Mixed &operator[]( char val ) const { return( this->lp_map.count(val) ? this->lp_map[val] : Mixed::zero() ); }
00066 inline const std::string &operator[]( int num ) const
00067 { return( ((num >= 0) && (num < (int) this->lp_arguments.size())) ? this->lp_arguments[num] : lp_s_empty ); }
00068
00069 inline const std::string &get_optstring( void ) const { return this->lp_data.pd_optstring; }
00070 inline const std::map<char, Mixed> &get_map( void ) const { return this->lp_map; }
00071 inline const std::vector<std::string> &get_arguments( void ) const { return this->lp_arguments; }
00072
00073 inline LineParser &usage( std::ostream &os ) { this->lp_data.usage( os ); return *this; }
00074 inline const LineParser &usage( std::ostream &os ) const { this->lp_data.usage( os ); return *this; }
00075
00076 LineParser &parse( int argn, char *const *argv );
00077 const LineParser &print( std::ostream &os ) const;
00078
00079 private:
00080 mutable std::map<char, Mixed> lp_map;
00081 std::vector<std::string> lp_arguments;
00082 ParserData lp_data;
00083
00084 static const std::string lp_s_empty;
00085 };
00086
00087 };
00088
00089 } COMMON_NAMESPACE_END;
00090
00091 #endif
00092
00093
00094
00095