Kinetis Bootloader Host  2.0.0
Host Tools for Kinetis devices
src/blfwk/options.h
1 // COPY/REUSE POLICY
2 // =================
3 // Permission is hereby granted to freely copy and redistribute this
4 // software, provided that the author is clearly credited in all copies
5 // and derivations. Neither the names of the authors nor that of their
6 // employers may be used to endorse or promote products derived from this
7 // software without specific written permission.
8 //
9 //
10 // DISCLAIMER
11 // ==========
12 // This software is provided ``As Is'' and without any express or implied
13 // warranties. Neither the authors nor any of their employers (including
14 // any of their subsidiaries and subdivisions) are responsible for maintaining
15 // or supporting this software or for any consequences resulting from the
16 // use of this software, no matter how awful, even if they arise from flaws
17 // in the software.
18 // ****************************************************************************
19 // ^FILE: options.h - option parsing classes
20 //
21 // ^DESCRIPTION:
22 // This file defines classes used to parse command-line options.
23 // Options may be parsed from an array of strings, or from any structure
24 // for which a corresponding option-iterator exists.
25 //
26 // ^HISTORY:
27 // 03/06/92 Brad Appleton <bradapp@enteract.com> Created
28 //
29 // 03/23/93 Brad Appleton <bradapp@enteract.com>
30 // - Added OptIstreamIter class
31 //
32 // 03/08/94 Brad Appleton <bradapp@enteract.com>
33 // - Added Options::reset() member function
34 //
35 // 07/31/97 Brad Appleton <bradapp@enteract.com>
36 // - Added PARSE_POS control flag and POSITIONAL return value
37 //
38 // 04/30/06 Chris Reed
39 // - Updated to modern C++ and STL
40 // - Converted comments to doxygen style
41 // ^^**************************************************************************
42 
43 #ifndef _options_h
44 #define _options_h
45 
46 #ifdef USE_STDIO
47 #include <stdio.h>
48 #else
49 #include <iostream>
50 #endif
51 
54 class OptIter
55 {
56 public:
57  OptIter(void) {}
58  virtual ~OptIter(void);
59 
63  virtual const char *curr(void) = 0;
64 
66  virtual void next(void) = 0;
67 
71  virtual const char *operator()(void);
72 };
73 
76 class OptIterRwd : public OptIter
77 {
78 public:
79  OptIterRwd(void);
80 
81  virtual ~OptIterRwd(void);
82 
83  virtual const char *curr(void) = 0;
84 
85  virtual void next(void) = 0;
86 
87  virtual const char *operator()(void) = 0;
88 
90  virtual void rewind(void) = 0;
91 };
92 
96 class OptArgvIter : public OptIterRwd
97 {
98 private:
99  int ndx; // index of current arg
100  int ac; // arg count
101  const char *const *av; // arg vector
102 
103 public:
104  OptArgvIter(const char *const argv[])
105  : av(argv)
106  , ac(-1)
107  , ndx(0)
108  {
109  }
110 
111  OptArgvIter(int argc, const char *const argv[])
112  : av(argv)
113  , ac(argc)
114  , ndx(0)
115  {
116  }
117 
118  virtual ~OptArgvIter(void);
119 
120  virtual const char *curr(void);
121 
122  virtual void next(void);
123 
124  virtual const char *operator()(void);
125 
126  virtual void rewind(void);
127 
129  int index(void) { return ndx; }
130 };
131 
134 class OptStrTokIter : public OptIterRwd
135 {
136 private:
137  unsigned len; // length of token-string
138  const char *str; // the token-string
139  const char *seps; // delimiter-set (separator-characters)
140  const char *cur; // current token
141  char *tokstr; // our copy of the token-string
142 
143  static const char *default_delims; // default delimiters = whitespace
144 
145 public:
146  OptStrTokIter(const char *tokens, const char *delimiters = 0);
147 
148  virtual ~OptStrTokIter(void);
149 
150  virtual const char *curr(void);
151 
152  virtual void next(void);
153 
154  virtual const char *operator()(void);
155 
156  virtual void rewind(void);
157 
160  const char *delimiters(void) { return seps; }
161  void delimiters(const char *delims) { seps = (delims) ? delims : default_delims; }
162 };
163 
179 class OptIstreamIter : public OptIter
180 {
181 private:
182  std::istream &is;
183  OptStrTokIter *tok_iter;
184 
185  void fill(void);
186 
187 public:
188  static const unsigned MAX_LINE_LEN;
189 
190  OptIstreamIter(std::istream &input);
191 
192  virtual ~OptIstreamIter(void);
193 
194  virtual const char *curr(void);
195 
196  virtual void next(void);
197 
198  virtual const char *operator()(void);
199 };
200 
363 class Options
364 {
365 private:
366  unsigned explicit_end : 1;
367  unsigned optctrls : 7;
368  const char *const *optvec;
369  const char *nextchar;
370  const char *listopt;
371  const char *cmdname;
372 
373  void check_syntax(void) const;
374 
375  const char *match_opt(char opt, int ignore_case = 0) const;
376 
377  const char *match_longopt(const char *opt, int len, int &ambiguous) const;
378 
379  int parse_opt(OptIter &iter, const char *&optarg);
380 
381  int parse_longopt(OptIter &iter, const char *&optarg);
382 
383 public:
384  enum OptCtrl
385  {
386  DEFAULT = 0x00,
387  ANYCASE = 0x01,
388  QUIET = 0x02,
389  PLUS = 0x04,
390  SHORT_ONLY = 0x08,
391  LONG_ONLY = 0x10,
392  NOGUESSING = 0x20,
394  PARSE_POS = 0x40
400  };
408 
411  enum OptRC
412  {
413  ENDOPTS = 0,
414  BADCHAR = -1,
415  BADKWD = -2,
416  AMBIGUOUS = -3,
417  POSITIONAL = -4
418  };
419 
420  Options(const char *name, const char *const optv[]);
421 
422  virtual ~Options(void);
423 
425  const char *name(void) const { return cmdname; }
427  unsigned ctrls(void) const { return optctrls; }
429  void ctrls(unsigned newctrls) { optctrls = newctrls; }
431  void reset(void) { nextchar = listopt = NULL; }
434  void usage(std::ostream &os, const char *positionals) const;
435 
464  int operator()(OptIter &iter, const char *&optarg);
465 
470  int explicit_endopts() const { return explicit_end; }
471 };
472 
473 #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: src/blfwk/options.h:160
Definition: apps/elftosb/common/options.h:162
const char * name(void) const
name() returns the command name
Definition: src/blfwk/options.h:425
int index(void)
index returns the current index to use for argv[]
Definition: src/blfwk/options.h:129
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: src/blfwk/options.h:470
unsigned ctrls(void) const
ctrls() (with no arguments) returns the existing control settings
Definition: src/blfwk/options.h:427
void ctrls(unsigned newctrls)
ctrls() (with 1 argument) sets new control settings
Definition: src/blfwk/options.h:429
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: src/blfwk/options.h:431
Definition: apps/elftosb/common/options.h:37