| eRPC Generator (erpcgen) Rev. 1.8.1 NXP Semiconductors

- Main Page
- Classes
- Files

- File List

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 <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) = 0;
85
86 virtual void next(void) = 0;
87
88 virtual const char *operator()(void) = 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 OptArgvIter(const char *const argv[])
106 : ndx(0)
107 , ac(-1)
108 , av(argv)
109 {
110 }
111
112 OptArgvIter(int argc, const char *const argv[])
113 : ndx(0)
114 , ac(argc)
115 , av(argv)
116 {
117 }
118
119 virtual ~OptArgvIter(void);
120
121 virtual const char *curr(void);
122
123 virtual void next(void);
124
125 virtual const char *operator()(void);
126
127 virtual void rewind(void);
128
130 int index(void) { return ndx; }
131 };
132
135 class OptStrTokIter : public OptIterRwd
136 {
137 private:
138 unsigned len; // length of token-string
139 const char *str; // the token-string
140 const char *seps; // delimiter-set (separator-characters)
141 const char *cur; // current token
142 char *tokstr; // our copy of the token-string
143
144 static const char *default_delims; // default delimiters = whitespace
145
146 public:
147 OptStrTokIter(const char *tokens, const char *delimiters = 0);
148
149 virtual ~OptStrTokIter(void);
150
151 virtual const char *curr(void);
152
153 virtual void next(void);
154
155 virtual const char *operator()(void);
156
157 virtual void rewind(void);
158
161 const char *delimiters(void) { return seps; }
162
163 void delimiters(const char *delims) { seps = (delims) ? delims : default_delims; }
164 };
165
181 class OptIstreamIter : public OptIter
182 {
183 private:
184 std::istream &is;
185 OptStrTokIter *tok_iter;
186
187 void fill(void);
188
189 public:
190 static const unsigned MAX_LINE_LEN;
191
192 OptIstreamIter(std::istream &input);
193
194 virtual ~OptIstreamIter(void);
195
196 virtual const char *curr(void);
197
198 virtual void next(void);
199
200 virtual const char *operator()(void);
201 };
202
365 class Options
366 {
367 private:
368 unsigned explicit_end : 1;
369 unsigned optctrls : 7;
370 const char *const *optvec;
371 const char *nextchar;
372 const char *listopt;
373 const char *cmdname;
374
375 void check_syntax(void) const;
376
377 const char *match_opt(char opt, int ignore_case = 0) const;
378
379 const char *match_longopt(const char *opt, int len, int &ambiguous) const;
380
381 int parse_opt(OptIter &iter, const char *&optarg);
382
383 int parse_longopt(OptIter &iter, const char *&optarg);
384
385 public:
386 enum OptCtrl
387 {
388 DEFAULT = 0x00,
389 ANYCASE = 0x01,
390 QUIET = 0x02,
391 PLUS = 0x04,
392 SHORT_ONLY = 0x08,
393 LONG_ONLY = 0x10,
394 NOGUESSING = 0x20,
396 PARSE_POS = 0x40
402 };
410
413 enum OptRC
414 {
415 ENDOPTS = 0,
416 BADCHAR = -1,
417 BADKWD = -2,
418 AMBIGUOUS = -3,
419 POSITIONAL = -4
420 };
421
422 Options(const char *name, const char *const optv[]);
423
424 virtual ~Options(void);
425
427 const char *name(void) const { return cmdname; }
428
430 unsigned ctrls(void) const { return optctrls; }
431
433 void ctrls(unsigned newctrls) { optctrls = newctrls; }
434
436 void reset(void) { nextchar = listopt = NULL; }
437
440 void usage(std::ostream &os, const char *positionals) const;
441
470 int operator()(OptIter &iter, const char *&optarg);
471
476 int explicit_endopts() const { return explicit_end; }
477 };
478
479 #endif /* _options_h */
OptStrTokIter
Definition: options.h:135
Options
parse command-line options
Definition: options.h:365
OptArgvIter
Definition: options.h:97
OptStrTokIter::delimiters
const char * delimiters(void)
Definition: options.h:161
OptIstreamIter
Definition: options.h:181
Options::name
const char * name(void) const
name() returns the command name
Definition: options.h:427
OptArgvIter::index
int index(void)
index returns the current index to use for argv[]
Definition: options.h:130
OptIter::operator()
virtual const char * operator()(void)
Definition: options.cpp:119
OptIter::next
virtual void next(void)=0
next() advances to the next item.
Options::OptRC
OptRC
Definition: options.h:413
OptIterRwd
Definition: options.h:77
Options::explicit_endopts
int explicit_endopts() const
Definition: options.h:476
Options::ctrls
unsigned ctrls(void) const
ctrls() (with no arguments) returns the existing control settings
Definition: options.h:430
Options::ctrls
void ctrls(unsigned newctrls)
ctrls() (with 1 argument) sets new control settings
Definition: options.h:433
OptIter::curr
virtual const char * curr(void)=0
Options::OptCtrl
OptCtrl
Definition: options.h:386
Options::reset
void reset(void)
reset for another pass to parse for options
Definition: options.h:436
OptIter
Definition: options.h:54