~holger-seelig/titania/3.0

« back to all changes in this revision

Viewing changes to libtitania-anyoptions/anyoption.h

  • Committer: Holger Seelig
  • Date: 2017-01-31 05:07:29 UTC
  • Revision ID: holger.seelig@yahoo.de-20170131050729-mcb9l290ezhn7x7o
Incorporate x3dtidy and titania-info into Titania.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- Mode: C++; coding: utf-8; tab-width: 3; indent-tabs-mode: tab; c-basic-offset: 3 -*-
2
 
 *******************************************************************************
3
 
 *
4
 
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5
 
 *
6
 
 * Copyright create3000, Scheffelstraße 31a, Leipzig, Germany 2011.
7
 
 *
8
 
 * All rights reserved. Holger Seelig <holger.seelig@yahoo.de>.
9
 
 *
10
 
 * THIS IS UNPUBLISHED SOURCE CODE OF create3000.
11
 
 *
12
 
 * The copyright notice above does not evidence any actual of intended
13
 
 * publication of such source code, and is an unpublished work by create3000.
14
 
 * This material contains CONFIDENTIAL INFORMATION that is the property of
15
 
 * create3000.
16
 
 *
17
 
 * No permission is granted to copy, distribute, or create derivative works from
18
 
 * the contents of this software, in whole or in part, without the prior written
19
 
 * permission of create3000.
20
 
 *
21
 
 * NON-MILITARY USE ONLY
22
 
 *
23
 
 * All create3000 software are effectively free software with a non-military use
24
 
 * restriction. It is free. Well commented source is provided. You may reuse the
25
 
 * source in any way you please with the exception anything that uses it must be
26
 
 * marked to indicate is contains 'non-military use only' components.
27
 
 *
28
 
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
29
 
 *
30
 
 * Copyright 1999, 2016 Holger Seelig <holger.seelig@yahoo.de>.
31
 
 *
32
 
 * This file is part of the Titania Project.
33
 
 *
34
 
 * Titania is free software: you can redistribute it and/or modify it under the
35
 
 * terms of the GNU General Public License version 3 only, as published by the
36
 
 * Free Software Foundation.
37
 
 *
38
 
 * Titania is distributed in the hope that it will be useful, but WITHOUT ANY
39
 
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
40
 
 * A PARTICULAR PURPOSE. See the GNU General Public License version 3 for more
41
 
 * details (a copy is included in the LICENSE file that accompanied this code).
42
 
 *
43
 
 * You should have received a copy of the GNU General Public License version 3
44
 
 * along with Titania.  If not, see <http://www.gnu.org/licenses/gpl.html> for a
45
 
 * copy of the GPLv3 License.
46
 
 *
47
 
 * For Silvio, Joy and Adi.
48
 
 *
49
 
 ******************************************************************************/
50
 
 
51
 
#ifndef __ANYOPTION_H__
52
 
#define __ANYOPTION_H__
53
 
 
54
 
#include <cstdlib>
55
 
#include <cstring>
56
 
#include <fstream>
57
 
#include <iostream>
58
 
#include <string>
59
 
 
60
 
#define COMMON_OPT   1
61
 
#define COMMAND_OPT  2
62
 
#define FILE_OPT  3
63
 
#define COMMON_FLAG  4
64
 
#define COMMAND_FLAG    5
65
 
#define FILE_FLAG    6
66
 
 
67
 
#define COMMAND_OPTION_TYPE   1
68
 
#define COMMAND_FLAG_TYPE  2
69
 
#define FILE_OPTION_TYPE   3
70
 
#define FILE_FLAG_TYPE     4
71
 
#define UNKNOWN_TYPE       5
72
 
 
73
 
#define DEFAULT_MAXOPTS    10
74
 
#define MAX_LONG_PREFIX_LENGTH   2
75
 
 
76
 
#define DEFAULT_MAXUSAGE   3
77
 
#define DEFAULT_MAXHELP         10
78
 
 
79
 
#define TRUE_FLAG "true"
80
 
 
81
 
using namespace std;
82
 
 
83
 
class AnyOption
84
 
{
85
 
 
86
 
public:
87
 
 
88
 
        /* the public interface */
89
 
 
90
 
        AnyOption ();
91
 
        AnyOption (int maxoptions);
92
 
        AnyOption (int maxoptions, int maxcharoptions);
93
 
        ~AnyOption ();
94
 
 
95
 
        /*
96
 
         * following set methods specifies the
97
 
         * special characters and delimiters
98
 
         * if not set traditional defaults will be used
99
 
         */
100
 
 
101
 
        void
102
 
        setCommandPrefixChar (char _prefix);    /* '-' in "-w" */
103
 
 
104
 
        void
105
 
        setCommandLongPrefix (char* _prefix);   /* '--' in "--width" */
106
 
 
107
 
        void
108
 
        setFileCommentChar (char _comment);     /* '#' in shellscripts */
109
 
 
110
 
        void
111
 
        setFileDelimiterChar (char _delimiter); /* ':' in "width : 100" */
112
 
 
113
 
        /*
114
 
         * provide the input for the options
115
 
         * like argv[ ] for commndline and the
116
 
         * option file name  to use;
117
 
         */
118
 
 
119
 
        void
120
 
        useCommandArgs (int _argc, char** _argv);
121
 
 
122
 
        void
123
 
        useFiileName (const char* _filename);
124
 
 
125
 
        /*
126
 
         * turn off the POSIX style options
127
 
         * this means anything starting with a '-' or "--"
128
 
         * will be considered a valid option
129
 
         * which alo means you cannot add a bunch of
130
 
         * POIX options chars together like "-lr"  for "-l -r"
131
 
         *
132
 
         */
133
 
 
134
 
        void
135
 
        noPOSIX ();
136
 
 
137
 
        /*
138
 
         * prints warning verbose if you set anything wrong
139
 
         */
140
 
        void
141
 
        setVerbose ();
142
 
 
143
 
        /*
144
 
         * there are two types of options
145
 
         *
146
 
         * Option - has an associated value ( -w 100 )
147
 
         * Flag  - no value, just a boolean flag  ( -nogui )
148
 
         *
149
 
         * the options can be either a string ( GNU style )
150
 
         * or a character ( traditional POSIX style )
151
 
         * or both ( --width, -w )
152
 
         *
153
 
         * the options can be common to the commandline and
154
 
         * the optionfile, or can belong only to either of
155
 
         * commandline and optionfile
156
 
         *
157
 
         * following set methods, handle all the aboove
158
 
         * cases of options.
159
 
         */
160
 
 
161
 
        /* options comman to command line and option file */
162
 
        void
163
 
        setOption (const char* opt_string);
164
 
 
165
 
        void
166
 
        setOption (char opt_char);
167
 
 
168
 
        void
169
 
        setOption (const char* opt_string, char opt_char);
170
 
 
171
 
        void
172
 
        setFlag (const char* opt_string);
173
 
 
174
 
        void
175
 
        setFlag (char opt_char);
176
 
 
177
 
        void
178
 
        setFlag (const char* opt_string, char opt_char);
179
 
 
180
 
        /* options read from commandline only */
181
 
        void
182
 
        setCommandOption (const char* opt_string);
183
 
 
184
 
        void
185
 
        setCommandOption (char opt_char);
186
 
 
187
 
        void
188
 
        setCommandOption (const char* opt_string, char opt_char);
189
 
 
190
 
        void
191
 
        setCommandFlag (const char* opt_string);
192
 
 
193
 
        void
194
 
        setCommandFlag (char opt_char);
195
 
 
196
 
        void
197
 
        setCommandFlag (const char* opt_string, char opt_char);
198
 
 
199
 
        /* options read from an option file only  */
200
 
        void
201
 
        setFileOption (const char* opt_string);
202
 
 
203
 
        void
204
 
        setFileOption (char opt_char);
205
 
 
206
 
        void
207
 
        setFileOption (const char* opt_string, char opt_char);
208
 
 
209
 
        void
210
 
        setFileFlag (const char* opt_string);
211
 
 
212
 
        void
213
 
        setFileFlag (char opt_char);
214
 
 
215
 
        void
216
 
        setFileFlag (const char* opt_string, char opt_char);
217
 
 
218
 
        /*
219
 
         * process the options, registerd using
220
 
         * useCommandArgs() and useFileName();
221
 
         */
222
 
        void
223
 
        processOptions ();
224
 
 
225
 
        void
226
 
        processCommandArgs ();
227
 
 
228
 
        void
229
 
        processCommandArgs (int max_args);
230
 
 
231
 
        bool
232
 
        processFile ();
233
 
 
234
 
        /*
235
 
         * process the specified options
236
 
         */
237
 
        void
238
 
        processCommandArgs (int _argc, char** _argv);
239
 
 
240
 
        void
241
 
        processCommandArgs (int _argc, char** _argv, int max_args);
242
 
 
243
 
        bool
244
 
        processFile (const char* _filename);
245
 
 
246
 
        /*
247
 
         * get the value of the options
248
 
         * will return NULL if no value is set
249
 
         */
250
 
        char*
251
 
        getValue (const char* _option);
252
 
 
253
 
        bool
254
 
        getFlag (const char* _option);
255
 
 
256
 
        char*
257
 
        getValue (char _optchar);
258
 
 
259
 
        bool
260
 
        getFlag (char _optchar);
261
 
 
262
 
        /*
263
 
         * Print Usage
264
 
         */
265
 
        void
266
 
        printUsage ();
267
 
 
268
 
        void
269
 
        printAutoUsage ();
270
 
 
271
 
        void
272
 
        addUsage (const char* line);
273
 
 
274
 
        void
275
 
        printHelp ();
276
 
 
277
 
        /* print auto usage printing for unknown options or flag */
278
 
        void
279
 
        autoUsagePrint (bool flag);
280
 
 
281
 
        /*
282
 
         * get the argument count and arguments sans the options
283
 
         */
284
 
        int
285
 
        getArgc ();
286
 
 
287
 
        char**
288
 
        getArgv ();
289
 
 
290
 
        char*
291
 
        getArgv (int index);
292
 
 
293
 
        bool
294
 
        hasOptions ();
295
 
 
296
 
 
297
 
private:
298
 
 
299
 
        /* the hidden data structure */
300
 
 
301
 
        int         argc;                                  /* commandline arg count  */
302
 
        char**      argv;                                  /* commndline args */
303
 
        const char* filename;                              /* the option file */
304
 
        char*       appname;                               /* the application name from argv[0] */
305
 
 
306
 
        int* new_argv;                                     /* arguments sans options (index to argv) */
307
 
        int  new_argc;                                     /* argument count sans the options */
308
 
        int  max_legal_args;                               /* ignore extra arguments */
309
 
 
310
 
        /* option strings storage + indexing */
311
 
        int          max_options;                          /* maximum number of options */
312
 
        const char** options;                              /* storage */
313
 
        int*         optiontype;                           /* type - common, command, file */
314
 
        int*         optionindex;                          /* index into value storage */
315
 
        int          option_counter;                       /* counter for added options  */
316
 
 
317
 
        /* option chars storage + indexing */
318
 
        int   max_char_options;                            /* maximum number options */
319
 
        char* optionchars;                                 /*  storage */
320
 
        int*  optchartype;                                 /* type - common, command, file */
321
 
        int*  optcharindex;                                /* index into value storage */
322
 
        int   optchar_counter;                             /* counter for added options  */
323
 
 
324
 
        /* values */
325
 
        char** values;                                     /* common value storage */
326
 
        int    g_value_counter;                            /* globally updated value index LAME! */
327
 
 
328
 
        /* help and usage */
329
 
        const char** usage;                                /* usage */
330
 
        int          max_usage_lines;                      /* max usage lines reseverd */
331
 
        int          usage_lines;                          /* number of usage lines */
332
 
 
333
 
        bool command_set;                                  /* if argc/argv were provided */
334
 
        bool file_set;                                     /* if a filename was provided */
335
 
        bool mem_allocated;                                /* if memory allocated in init() */
336
 
        bool posix_style;                                  /* enables to turn off POSIX style options */
337
 
        bool verbose;                                      /* silent|verbose */
338
 
        bool print_usage;                                  /* usage verbose */
339
 
        bool print_help;                                   /* help verbose */
340
 
 
341
 
        char opt_prefix_char;                              /*  '-' in "-w" */
342
 
        char long_opt_prefix [MAX_LONG_PREFIX_LENGTH + 1]; /* '--' in "--width" */
343
 
        char file_delimiter_char;                          /* ':' in width : 100 */
344
 
        char file_comment_char;                            /*  '#' in "#this is a comment" */
345
 
        char equalsign;
346
 
        char comment;
347
 
        char delimiter;
348
 
        char endofline;
349
 
        char whitespace;
350
 
        char nullterminate;
351
 
 
352
 
        bool set;                                          //was static member
353
 
        bool once;                                         //was static member
354
 
 
355
 
        bool hasoptions;
356
 
        bool autousage;
357
 
 
358
 
 
359
 
private:
360
 
 
361
 
        /* the hidden utils */
362
 
 
363
 
        void
364
 
        init ();
365
 
 
366
 
        void
367
 
        init (int maxopt, int maxcharopt);
368
 
 
369
 
        bool
370
 
        alloc ();
371
 
 
372
 
        void
373
 
        cleanup ();
374
 
 
375
 
        bool
376
 
        valueStoreOK ();
377
 
 
378
 
        /* grow storage arrays as required */
379
 
        bool
380
 
        doubleOptStorage ();
381
 
 
382
 
        bool
383
 
        doubleCharStorage ();
384
 
 
385
 
        bool
386
 
        doubleUsageStorage ();
387
 
 
388
 
        bool
389
 
        setValue (const char* option, char* value);
390
 
 
391
 
        bool
392
 
        setFlagOn (const char* option);
393
 
 
394
 
        bool
395
 
        setValue (char optchar, char* value);
396
 
 
397
 
        bool
398
 
        setFlagOn (char optchar);
399
 
 
400
 
        void
401
 
        addOption (const char* option, int type);
402
 
 
403
 
        void
404
 
        addOption (char optchar, int type);
405
 
 
406
 
        void
407
 
        addOptionError (const char* opt);
408
 
 
409
 
        void
410
 
        addOptionError (char opt);
411
 
 
412
 
        bool
413
 
        findFlag (char* value);
414
 
 
415
 
        void
416
 
        addUsageError (const char* line);
417
 
 
418
 
        bool
419
 
        CommandSet ();
420
 
 
421
 
        bool
422
 
        FileSet ();
423
 
 
424
 
        bool
425
 
        POSIX ();
426
 
 
427
 
        char
428
 
        parsePOSIX (char* arg);
429
 
 
430
 
        int
431
 
        parseGNU (char* arg);
432
 
 
433
 
        bool
434
 
        matchChar (char c);
435
 
 
436
 
        int
437
 
        matchOpt (char* opt);
438
 
 
439
 
        /* dot file methods */
440
 
        char*
441
 
        readFile ();
442
 
 
443
 
        char*
444
 
        readFile (const char* fname);
445
 
 
446
 
        bool
447
 
        consumeFile (char* buffer);
448
 
 
449
 
        void
450
 
        processLine (char* theline, int length);
451
 
 
452
 
        char*
453
 
        chomp (char* str);
454
 
 
455
 
        void
456
 
        valuePairs (char* type, char* value);
457
 
 
458
 
        void
459
 
        justValue (char* value);
460
 
 
461
 
        void
462
 
        printVerbose (const char* msg);
463
 
 
464
 
        void
465
 
        printVerbose (char* msg);
466
 
 
467
 
        void
468
 
        printVerbose (char ch);
469
 
 
470
 
        void
471
 
        printVerbose ();
472
 
 
473
 
};
474
 
 
475
 
#endif /* ! _ANYOPTION_H */