Kinetis Bootloader Host  2.0.0
Host Tools for Kinetis devices
apps/elftosb/common/options.h
1 // ****************************************************************************
2 // ^FILE: options.h - option parsing classes
3 //
4 // ^DESCRIPTION:
5 // This file defines classes used to parse command-line options.
6 // Options may be parsed from an array of strings, or from any structure
7 // for which a corresponding option-iterator exists.
8 //
9 // ^HISTORY:
10 // 03/06/92 Brad Appleton <bradapp@enteract.com> Created
11 //
12 // 03/23/93 Brad Appleton <bradapp@enteract.com>
13 // - Added OptIstreamIter class
14 //
15 // 03/08/94 Brad Appleton <bradapp@enteract.com>
16 // - Added Options::reset() member function
17 //
18 // 07/31/97 Brad Appleton <bradapp@enteract.com>
19 // - Added PARSE_POS control flag and POSITIONAL return value
20 //
21 // 04/30/06 Chris Reed
22 // - Updated to modern C++ and STL
23 // - Converted comments to doxygen style
24 // ^^**************************************************************************
25 
26 #ifndef _options_h
27 #define _options_h
28 
29 #ifdef USE_STDIO
30 #include <stdio.h>
31 #else
32 #include <iostream>
33 #endif
34 
37 class OptIter
38 {
39 public:
40  OptIter(void) {}
41  virtual ~OptIter(void);
42 
46  virtual const char *curr(void) = 0;
47 
49  virtual void next(void) = 0;
50 
54  virtual const char *operator()(void);
55 };
56 
59 class OptIterRwd : public OptIter
60 {
61 public:
62  OptIterRwd(void);
63 
64  virtual ~OptIterRwd(void);
65 
66  virtual const char *curr(void) = 0;
67 
68  virtual void next(void) = 0;
69 
70  virtual const char *operator()(void) = 0;
71 
73  virtual void rewind(void) = 0;
74 };
75 
79 class OptArgvIter : public OptIterRwd
80 {
81 private:
82  int ndx; // index of current arg
83  int ac; // arg count
84  const char *const *av; // arg vector
85 
86 public:
87  OptArgvIter(const char *const argv[])
88  : av(argv)
89  , ac(-1)
90  , ndx(0)
91  {
92  }
93 
94  OptArgvIter(int argc, const char *const argv[])
95  : av(argv)
96  , ac(argc)
97  , ndx(0)
98  {
99  }
100 
101  virtual ~OptArgvIter(void);
102 
103  virtual const char *curr(void);
104 
105  virtual void next(void);
106 
107  virtual const char *operator()(void);
108 
109  virtual void rewind(void);
110 
112  int index(void) { return ndx; }
113 };
114 
117 class OptStrTokIter : public OptIterRwd
118 {
119 private:
120  unsigned len; // length of token-string
121  const char *str; // the token-string
122  const char *seps; // delimiter-set (separator-characters)
123  const char *cur; // current token
124  char *tokstr; // our copy of the token-string
125 
126  static const char *default_delims; // default delimiters = whitespace
127 
128 public:
129  OptStrTokIter(const char *tokens, const char *delimiters = 0);
130 
131  virtual ~OptStrTokIter(void);
132 
133  virtual const char *curr(void);
134 
135  virtual void next(void);
136 
137  virtual const char *operator()(void);
138 
139  virtual void rewind(void);
140 
143  const char *delimiters(void) { return seps; }
144  void delimiters(const char *delims) { seps = (delims) ? delims : default_delims; }
145 };
146 
162 class OptIstreamIter : public OptIter
163 {
164 private:
165  std::istream &is;
166  OptStrTokIter *tok_iter;
167 
168  void fill(void);
169 
170 public:
171  static const unsigned MAX_LINE_LEN;
172 
173  OptIstreamIter(std::istream &input);
174 
175  virtual ~OptIstreamIter(void);
176 
177  virtual const char *curr(void);
178 
179  virtual void next(void);
180 
181  virtual const char *operator()(void);
182 };
183 
346 class Options
347 {
348 private:
349  unsigned explicit_end : 1;
350  unsigned optctrls : 7;
351  const char *const *optvec;
352  const char *nextchar;
353  const char *listopt;
354  const char *cmdname;
355 
356  void check_syntax(void) const;
357 
358  const char *match_opt(char opt, int ignore_case = 0) const;
359 
360  const char *match_longopt(const char *opt, int len, int &ambiguous) const;
361 
362  int parse_opt(OptIter &iter, const char *&optarg);
363 
364  int parse_longopt(OptIter &iter, const char *&optarg);
365 
366 public:
367  enum OptCtrl
368  {
369  DEFAULT = 0x00,
370  ANYCASE = 0x01,
371  QUIET = 0x02,
372  PLUS = 0x04,
373  SHORT_ONLY = 0x08,
374  LONG_ONLY = 0x10,
375  NOGUESSING = 0x20,
377  PARSE_POS = 0x40
383  };
391 
394  enum OptRC
395  {
396  ENDOPTS = 0,
397  BADCHAR = -1,
398  BADKWD = -2,
399  AMBIGUOUS = -3,
400  POSITIONAL = -4
401  };
402 
403  Options(const char *name, const char *const optv[]);
404 
405  virtual ~Options(void);
406 
408  const char *name(void) const { return cmdname; }
410  unsigned ctrls(void) const { return optctrls; }
412  void ctrls(unsigned newctrls) { optctrls = newctrls; }
414  void reset(void) { nextchar = listopt = NULL; }
417  void usage(std::ostream &os, const char *positionals) const;
418 
447  int operator()(OptIter &iter, const char *&optarg);
448 
453  int explicit_endopts() const { return explicit_end; }
454 };
455 
456 #endif /* _options_h */
Definition: apps/elftosb/common/options.h:117
parse command-line options
Definition: apps/elftosb/common/options.h:346
Definition: apps/elftosb/common/options.h:79
const char * delimiters(void)
Definition: apps/elftosb/common/options.h:143
Definition: apps/elftosb/common/options.h:162
const char * name(void) const
name() returns the command name
Definition: apps/elftosb/common/options.h:408
int index(void)
index returns the current index to use for argv[]
Definition: apps/elftosb/common/options.h:112
virtual const char * operator()(void)
Definition: apps/elftosb/common/options.cpp:108
virtual void next(void)=0
next() advances to the next item.
OptRC
Definition: apps/elftosb/common/options.h:394
Definition: apps/elftosb/common/options.h:59
int explicit_endopts() const
Definition: apps/elftosb/common/options.h:453
unsigned ctrls(void) const
ctrls() (with no arguments) returns the existing control settings
Definition: apps/elftosb/common/options.h:410
void ctrls(unsigned newctrls)
ctrls() (with 1 argument) sets new control settings
Definition: apps/elftosb/common/options.h:412
virtual const char * curr(void)=0
OptCtrl
Definition: apps/elftosb/common/options.h:367
void reset(void)
reset for another pass to parse for options
Definition: apps/elftosb/common/options.h:414
Definition: apps/elftosb/common/options.h:37