eRPC Generator (erpcgen)  Rev. 1.12.0
NXP Semiconductors
options.hpp
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 <cstdio>
48 #else
49 #include <iostream>
50 #endif
51 
54 class OptIter
55 {
56 public:
57  OptIter(void) {}
58 
59  virtual ~OptIter(void);
60 
64  virtual const char *curr(void) = 0;
65 
67  virtual void next(void) = 0;
68 
72  virtual const char *operator()(void);
73 };
74 
77 class OptIterRwd : public OptIter
78 {
79 public:
80  OptIterRwd(void);
81 
82  virtual ~OptIterRwd(void);
83 
84  virtual const char *curr(void) override = 0;
85 
86  virtual void next(void) override = 0;
87 
88  virtual const char *operator()(void) override = 0;
89 
91  virtual void rewind(void) = 0;
92 };
93 
97 class OptArgvIter : public OptIterRwd
98 {
99 private:
100  int ndx; // index of current arg
101  int ac; // arg count
102  const char *const *av; // arg vector
103 
104 public:
105  explicit OptArgvIter(const char *const argv[]) : ndx(0), ac(-1), av(argv) {}
106 
107  OptArgvIter(int argc, const char *const argv[]) : ndx(0), ac(argc), av(argv) {}
108 
109  virtual ~OptArgvIter(void);
110 
111  virtual const char *curr(void) override;
112 
113  virtual void next(void) override;
114 
115  virtual const char *operator()(void) override;
116 
117  virtual void rewind(void) override;
118 
120  int index(void) { return ndx; }
121 };
122 
125 class OptStrTokIter : public OptIterRwd
126 {
127 private:
128  unsigned len; // length of token-string
129  const char *str; // the token-string
130  const char *seps; // delimiter-set (separator-characters)
131  const char *cur; // current token
132  char *tokstr; // our copy of the token-string
133 
134  static const char *default_delims; // default delimiters = whitespace
135 
136 public:
137  explicit OptStrTokIter(const char *tokens, const char *delimiters = 0);
138 
139  virtual ~OptStrTokIter(void);
140 
141  virtual const char *curr(void) override;
142 
143  virtual void next(void) override;
144 
145  virtual const char *operator()(void) override;
146 
147  virtual void rewind(void) override;
148 
151  const char *delimiters(void) { return seps; }
152 
153  void delimiters(const char *delims) { seps = (delims) ? delims : default_delims; }
154 };
155 
171 class OptIstreamIter : public OptIter
172 {
173 private:
174  std::istream &is;
175  OptStrTokIter *tok_iter;
176 
177  void fill(void);
178 
179 public:
180  static const unsigned MAX_LINE_LEN;
181 
182  explicit OptIstreamIter(std::istream &input);
183 
184  virtual ~OptIstreamIter(void);
185 
186  virtual const char *curr(void) override;
187 
188  virtual void next(void) override;
189 
190  virtual const char *operator()(void) override;
191 };
192 
355 class Options
356 {
357 private:
358  unsigned explicit_end : 1;
359  unsigned optctrls : 7;
360  const char *const *optvec;
361  const char *nextchar;
362  const char *listopt;
363  const char *cmdname;
364 
365  void check_syntax(void) const;
366 
367  const char *match_opt(char opt, int ignore_case = 0) const;
368 
369  const char *match_longopt(const char *opt, int len, int &ambiguous) const;
370 
371  int parse_opt(OptIter &iter, const char *&optarg);
372 
373  int parse_longopt(OptIter &iter, const char *&optarg);
374 
375 public:
376  enum class OptCtrl
377  {
378  DEFAULT = 0x00,
379  ANYCASE = 0x01,
380  QUIET = 0x02,
381  PLUS = 0x04,
382  SHORT_ONLY = 0x08,
383  LONG_ONLY = 0x10,
384  NOGUESSING = 0x20,
386  PARSE_POS = 0x40
392  };
400 
403  enum class OptRC
404  {
405  ENDOPTS = 0,
406  BADCHAR = -1,
407  BADKWD = -2,
408  AMBIGUOUS = -3,
409  POSITIONAL = -4
410  };
411 
412  Options(const char *name, const char *const optv[]);
413 
414  virtual ~Options(void);
415 
417  const char *name(void) const { return cmdname; }
418 
420  unsigned ctrls(void) const { return optctrls; }
421 
423  void ctrls(unsigned newctrls) { optctrls = newctrls; }
424 
426  void reset(void) { nextchar = listopt = NULL; }
427 
430  void usage(std::ostream &os, const char *positionals) const;
431 
460  int operator()(OptIter &iter, const char *&optarg);
461 
466  int explicit_endopts() const { return explicit_end; }
467 };
468 
469 #endif /* _options_h */
Definition: options.hpp:125
parse command-line options
Definition: options.hpp:355
Definition: options.hpp:97
const char * delimiters(void)
Definition: options.hpp:151
Definition: options.hpp:171
const char * name(void) const
name() returns the command name
Definition: options.hpp:417
int index(void)
index returns the current index to use for argv[]
Definition: options.hpp:120
virtual const char * operator()(void)
Definition: options.cpp:116
virtual void next(void)=0
next() advances to the next item.
OptRC
Definition: options.hpp:403
Definition: options.hpp:77
int explicit_endopts() const
Definition: options.hpp:466
unsigned ctrls(void) const
ctrls() (with no arguments) returns the existing control settings
Definition: options.hpp:420
void ctrls(unsigned newctrls)
ctrls() (with 1 argument) sets new control settings
Definition: options.hpp:423
virtual const char * curr(void)=0
OptCtrl
Definition: options.hpp:376
void reset(void)
reset for another pass to parse for options
Definition: options.hpp:426
Definition: options.hpp:54