anyoption.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. #ifndef _ANYOPTION_H
  2. #define _ANYOPTION_H
  3. #include <iostream>
  4. #include <fstream>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #define COMMON_OPT 1
  8. #define COMMAND_OPT 2
  9. #define FILE_OPT 3
  10. #define COMMON_FLAG 4
  11. #define COMMAND_FLAG 5
  12. #define FILE_FLAG 6
  13. #define COMMAND_OPTION_TYPE 1
  14. #define COMMAND_FLAG_TYPE 2
  15. #define FILE_OPTION_TYPE 3
  16. #define FILE_FLAG_TYPE 4
  17. #define UNKNOWN_TYPE 5
  18. #define DEFAULT_MAXOPTS 10
  19. #define MAX_LONG_PREFIX_LENGTH 2
  20. #define DEFAULT_MAXUSAGE 3
  21. #define DEFAULT_MAXHELP 10
  22. #define TRUE_FLAG "true"
  23. class AnyOption
  24. {
  25. public: /* the public interface */
  26. AnyOption();
  27. AnyOption(int maxoptions );
  28. AnyOption(int maxoptions , int maxcharoptions);
  29. ~AnyOption();
  30. /*
  31. * following set methods specifies the
  32. * special characters and delimiters
  33. * if not set traditional defaults will be used
  34. */
  35. void setCommandPrefixChar( char _prefix ); /* '-' in "-w" */
  36. void setCommandLongPrefix( char *_prefix ); /* '--' in "--width" */
  37. void setFileCommentChar( char _comment ); /* '#' in shellscripts */
  38. void setFileDelimiterChar( char _delimiter );/* ':' in "width : 100" */
  39. /*
  40. * provide the input for the options
  41. * like argv[] for commndline and the
  42. * option file name to use;
  43. */
  44. void useCommandArgs( int _argc, char **_argv );
  45. void useFileName( const char *_filename );
  46. /*
  47. * turn off the POSIX style options
  48. * this means anything starting with a '-' or "--"
  49. * will be considered a valid option
  50. * which alo means you cannot add a bunch of
  51. * POIX options chars together like "-lr" for "-l -r"
  52. *
  53. */
  54. void noPOSIX();
  55. /*
  56. * prints warning verbose if you set anything wrong
  57. */
  58. void setVerbose();
  59. /*
  60. * there are two types of options
  61. *
  62. * Option - has an associated value ( -w 100 )
  63. * Flag - no value, just a boolean flag ( -nogui )
  64. *
  65. * the options can be either a string ( GNU style )
  66. * or a character ( traditional POSIX style )
  67. * or both ( --width, -w )
  68. *
  69. * the options can be common to the commandline and
  70. * the optionfile, or can belong only to either of
  71. * commandline and optionfile
  72. *
  73. * following set methods, handle all the aboove
  74. * cases of options.
  75. */
  76. /* options comman to command line and option file */
  77. void setOption( const char *opt_string );
  78. void setOption( char opt_char );
  79. void setOption( const char *opt_string , char opt_char );
  80. void setFlag( const char *opt_string );
  81. void setFlag( char opt_char );
  82. void setFlag( const char *opt_string , char opt_char );
  83. /* options read from commandline only */
  84. void setCommandOption( const char *opt_string );
  85. void setCommandOption( char opt_char );
  86. void setCommandOption( const char *opt_string , char opt_char );
  87. void setCommandFlag( const char *opt_string );
  88. void setCommandFlag( char opt_char );
  89. void setCommandFlag( const char *opt_string , char opt_char );
  90. /* options read from an option file only */
  91. void setFileOption( const char *opt_string );
  92. void setFileOption( char opt_char );
  93. void setFileOption( const char *opt_string , char opt_char );
  94. void setFileFlag( const char *opt_string );
  95. void setFileFlag( char opt_char );
  96. void setFileFlag( const char *opt_string , char opt_char );
  97. /*
  98. * process the options, registerd using
  99. * useCommandArgs() and useFileName();
  100. */
  101. void processOptions();
  102. void processCommandArgs();
  103. void processCommandArgs( int max_args );
  104. bool processFile();
  105. /*
  106. * process the specified options
  107. */
  108. void processCommandArgs( int _argc, char **_argv );
  109. void processCommandArgs( int _argc, char **_argv, int max_args );
  110. bool processFile( const char *_filename );
  111. /*
  112. * get the value of the options
  113. * will return NULL if no value is set
  114. */
  115. char *getValue( const char *_option );
  116. bool getFlag( const char *_option );
  117. char *getValue( char _optchar );
  118. bool getFlag( char _optchar );
  119. /*
  120. * Print Usage
  121. */
  122. void printUsage();
  123. void addUsage( const char *line );
  124. void printHelp();
  125. void noUsage();
  126. /*
  127. * get the argument count and arguments sans the options
  128. */
  129. int getArgc();
  130. char* getArgv( int index );
  131. private: /* the hidden data structure */
  132. int argc; /* commandline arg count */
  133. char **argv; /* commndline args */
  134. const char* filename; /* the option file */
  135. char* appname; /* the application name from argv[0] */
  136. int *new_argv; /* arguments sans options (index to argv) */
  137. int new_argc; /* argument count sans the options */
  138. int max_legal_args; /* ignore extra arguments */
  139. /* option strings storage + indexing */
  140. int max_options; /* maximum number of options */
  141. const char **options; /* storage */
  142. int *optiontype; /* type - common, command, file */
  143. int *optionindex; /* index into value storage */
  144. int option_counter; /* counter for added options */
  145. /* option chars storage + indexing */
  146. int max_char_options; /* maximum number options */
  147. char *optionchars; /* storage */
  148. int *optchartype; /* type - common, command, file */
  149. int *optcharindex; /* index into value storage */
  150. int optchar_counter; /* counter for added options */
  151. /* values */
  152. char **values; /* common value storage */
  153. int g_value_counter; /* globally updated value index LAME! */
  154. /* help and usage */
  155. const char **usage; /* usage */
  156. int max_usage_lines; /* max usage lines reseverd */
  157. int usage_lines; /* number of usage lines */
  158. bool command_set; /* if argc/argv were provided */
  159. bool file_set; /* if a filename was provided */
  160. bool mem_allocated; /* if memory allocated in init() */
  161. bool posix_style; /* enables to turn off POSIX style options */
  162. bool verbose; /* silent|verbose */
  163. bool print_usage; /* usage verbose */
  164. bool print_help; /* help verbose */
  165. char opt_prefix_char; /* '-' in "-w" */
  166. char long_opt_prefix[MAX_LONG_PREFIX_LENGTH]; /* '--' in "--width" */
  167. char file_delimiter_char; /* ':' in width : 100 */
  168. char file_comment_char; /* '#' in "#this is a comment" */
  169. char equalsign;
  170. char comment;
  171. char delimiter;
  172. char endofline;
  173. char whitespace;
  174. char nullterminate;
  175. bool set; //was static member
  176. bool once; //was static member
  177. private: /* the hidden utils */
  178. void init();
  179. void init(int maxopt, int maxcharopt );
  180. bool alloc();
  181. void cleanup();
  182. bool valueStoreOK();
  183. /* grow storage arrays as required */
  184. bool doubleOptStorage();
  185. bool doubleCharStorage();
  186. bool doubleUsageStorage();
  187. bool setValue( const char *option , char *value );
  188. bool setFlagOn( const char *option );
  189. bool setValue( char optchar , char *value);
  190. bool setFlagOn( char optchar );
  191. void addOption( const char* option , int type );
  192. void addOption( char optchar , int type );
  193. void addOptionError( const char *opt);
  194. void addOptionError( char opt);
  195. bool findFlag( char* value );
  196. void addUsageError( const char *line );
  197. bool CommandSet();
  198. bool FileSet();
  199. bool POSIX();
  200. bool USAGE();
  201. char parsePOSIX( char* arg );
  202. int parseGNU( char *arg );
  203. bool matchChar( char c );
  204. int matchOpt( char *opt );
  205. /* dot file methods */
  206. char *readFile();
  207. char *readFile( const char* fname );
  208. bool consumeFile( char *buffer );
  209. void processLine( char *theline, int length );
  210. char *chomp( char *str );
  211. void valuePairs( char *type, char *value );
  212. void justValue( char *value );
  213. void printVerbose( const char *msg );
  214. void printVerbose( char *msg );
  215. void printVerbose( char ch );
  216. void printVerbose( );
  217. };
  218. #endif /* ! _ANYOPTION_H */