1 #ifndef _POPTIONS_H
2 #define _POPTIONS_H
3 /*
4 poptions.h : This file is part of pstoedit
5 program option handling
6
7 Copyright (C) 1993 - 2014 Wolfgang Glunz, wglunz35_AT_pstoedit.net
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22
23 */
24
25 #ifndef cppcomp_h
26 #include "cppcomp.h"
27 #endif
28
29 #include I_iostream
30 #include I_ostream
31 #include I_istream
32
33 #include "miscutil.h"
34
35 USESTD
36
37 enum baseTypeIDs { int_ty, double_ty, bool_ty, char_ty };
38
39 class DLLEXPORT IntValueExtractor {
40 public:
41 static bool getvalue(const char *optname, const char *instring, unsigned int ¤targ, int &result);
42 static const char *gettypename() ;
43 static unsigned int gettypeID();
44 };
45
46 class DLLEXPORT DoubleValueExtractor {
47 public:
48 static bool getvalue(const char *optname, const char *instring, unsigned int ¤targ, double &result) ;
49 static const char *gettypename() ;
50 static unsigned int gettypeID();
51 };
52
53
54 class DLLEXPORT CharacterValueExtractor {
55 public:
56 static bool getvalue(const char *optname, const char *instring, unsigned int ¤targ, char &result) ;
57 static const char *gettypename() ;
58 static unsigned int gettypeID();
59 };
60
61 class DLLEXPORT BoolBaseExtractor {
62 public:
63 static const char *gettypename();
64 static unsigned int gettypeID();
65 };
66
67 //lint -esym(1790,BoolBaseExtractor)
68 class DLLEXPORT BoolInvertingExtractor : public BoolBaseExtractor {
69 public:
70 static bool getvalue(const char *optname, const char *instring, unsigned int ¤targ, bool &result) ;
71 };
72
73 class DLLEXPORT BoolFalseExtractor : public BoolBaseExtractor{
74 public:
75 static bool getvalue(const char *optname, const char *instring, unsigned int ¤targ, bool &result) ;
76 };
77
78 class DLLEXPORT BoolTrueExtractor : public BoolBaseExtractor{
79 public:
80 static bool getvalue(const char *optname, const char *instring, unsigned int ¤targ, bool &result);
81 };
82
83
84
85 class DLLEXPORT OptionBase {
86 public:
OptionBase(bool optional_p,const char * flag_p,const char * argname_p,int propsheet_p,const char * description_p,const char * TeXhelp_p)87 OptionBase(bool optional_p,const char *flag_p, const char *argname_p, int propsheet_p, const char *description_p, const char * TeXhelp_p):
88 flag(flag_p),
89 argname(argname_p),
90 propsheet(propsheet_p),
91 description(description_p),
92 TeXhelp(TeXhelp_p),
93 optional(optional_p),
94 membername(""){ // membername is set during "add" because it was simpler to grab the membername during the add
95 };
~OptionBase()96 virtual ~OptionBase() { membername = 0; }
97 virtual ostream & writevalue(ostream & out) const = 0;
98 #if 0
99 void toString(RSString &) const;
100 #endif
101 virtual bool copyvalue(const char *optname, const char *valuestring, unsigned int ¤targ) = 0;
102 virtual bool copyvalue_simple(const char *valuestring) = 0;
103 // virtual bool copyvalue_simple(bool boolvalue) = 0;
104 virtual const char *gettypename() const = 0;
105 virtual unsigned int gettypeID() const = 0;
106 virtual void * GetAddrOfValue() = 0; // will return real type instead of void
107
108 //lint -esym(1540,OptionBase::flag) // not freed
109 //lint -esym(1540,OptionBase::description) // not freed
110 const char * const flag; // -bf
111 const char * const argname; // a meaningfull name of the argument (if not a boolean option)
112 int propsheet; // the number of the propertysheet to place this option on
113 const char * const description; // help text
114 const char * const TeXhelp;
115 bool optional;
116 const char * membername;
117
118 private:
119 OptionBase(); // disabled
120 OptionBase(const OptionBase&); // disabled
121 const OptionBase& operator=(const OptionBase&); // disabled
122 };
123
124 template <class ValueType, class ExtractorType >
125 class OptionT : public OptionBase {
126 public:
127 OptionT < ValueType, ExtractorType > (bool optional_p, const char *flag_p, const char *argname_p, int propsheet_p, const char *description_p, const char * TeXhelp_p, const ValueType & initialvalue) :
OptionBase(optional_p,flag_p,argname_p,propsheet_p,description_p,TeXhelp_p)128 OptionBase(optional_p, flag_p, argname_p, propsheet_p, description_p, TeXhelp_p),
129 value(initialvalue) {
130 };
131 OptionT < ValueType, ExtractorType > (bool optional_p, const char *flag_p, const char *argname_p, int propsheet_p, const char *description_p, const char * TeXhelp_p ) :
OptionBase(optional_p,flag_p,argname_p,propsheet_p,description_p,TeXhelp_p)132 OptionBase(optional_p, flag_p, argname_p, propsheet_p, description_p, TeXhelp_p),
133 value() // use default init for value
134 {
135 //lint -esym(1401,*::value) // not initialized - we use the default ctor here
136 };
137 virtual ostream & writevalue(ostream & out) const;
138
139
copyvalue(const char * optname,const char * valuestring,unsigned int & currentarg)140 virtual bool copyvalue(const char *optname, const char *valuestring, unsigned int ¤targ) {
141 return ExtractorType::getvalue(optname, valuestring, currentarg, value);
142 }
copyvalue_simple(const char * valuestring)143 bool copyvalue_simple(const char *valuestring) {
144 unsigned int num = 0;
145 return copyvalue("no name because of copyvalue_simple",valuestring,num);
146 }
147 #if 0
148 bool copyvalue_simple(bool boolvalue ) {
149 // value = boolvalue;
150 // unsigned int num = 0;
151 // we cannot just use the "extractor" since that works like the option has given on the commandline
152 // but copyvalue_simple is called for all options - but that does not mean that all options are
153 // "put on the virtual commandline" - clear ?
154 // return copyvalue("no name because of copyvalue_simple","",num);
155 value = boolvalue;
156 return true; // wogl FIXME
157 }
158 #endif
gettypename()159 virtual const char *gettypename() const {
160 return ExtractorType::gettypename();
161 }
gettypeID()162 virtual unsigned int gettypeID() const {
163 return ExtractorType::gettypeID();
164 }
165 //lint -save -esym(1539,OptionBase::optional) // not assigned a value
166 //lint -e(1763)
operator()167 ValueType & operator()() { return value; }
operator()168 const ValueType & operator()() const { return value; }
ValueType()169 operator ValueType () const { return value; }
170 /* const ValueType & */ void operator =(const ValueType & arg) {
171 /* return */ value = arg;
172 //lint -esym(1539,OptionBase::propsheet) // not assigned in op=
173 //lint -esym(1539,OptionBase::membername) // not assigned in op=
174 } // cannot return a reference, because char*::operator= does not
175 //lint -restore
176 bool operator !=(const ValueType & arg) const { return value != arg; }
177 bool operator ==(const ValueType & arg) const { return value == arg; }
178 bool operator !() const { return !value ; }
GetAddrOfValue()179 virtual void * GetAddrOfValue() { return &value;}
180
181
182 ValueType value;
183
184 private:
185 OptionT();// disabled
186 OptionT(const OptionT&);// disabled
187 const OptionT& operator=(const OptionT&); // disabled
188 };
189
190
191 template <class ValueType, class ExtractorType >
writevalue(ostream & out)192 ostream & OptionT<ValueType, ExtractorType>::writevalue(ostream & out) const {
193 out << value;
194 return out;
195 }
196
197 class DLLEXPORT ProgramOptions {
198 public:
expectUnhandled(expectUnhandled_p)199 ProgramOptions(bool expectUnhandled_p = false) : expectUnhandled(expectUnhandled_p), unhandledCounter(0), optcount(0) { unhandledOptions[0]=0;alloptions[0]=0; };
200
~ProgramOptions()201 virtual ~ProgramOptions() {}
202 unsigned int parseoptions(ostream & outstr, unsigned int argc, const char * const*argv) ;
203 // unsigned int sheet: -1 indicates "all"
204 void showhelp(ostream & outstr, bool forTeX, bool withdescription, int sheet = -1) const ;
205 void dumpunhandled(ostream & outstr) const ;
206 #if 0
207 void showvalues(ostream & outstr, bool withdescription = true) const ;
208 #endif
getOptionConstIterator()209 const OptionBase * const * getOptionConstIterator() const { return &alloptions[0]; }
getOptionIterator()210 OptionBase * const * getOptionIterator() const { return &alloptions[0]; }
numberOfOptions()211 unsigned int numberOfOptions() const { return optcount; }
hideFromDoku(const OptionBase &)212 virtual bool hideFromDoku(const OptionBase& /* opt */ ) const { return false; } // some options may be hidden, i.e. debug only options
213
214
215 protected:
216 void add(OptionBase * op, const char * const membername) ;
217
218 public:
219 bool expectUnhandled; // whether to expect unhandled arguments
220 unsigned int unhandledCounter;
221 const char *unhandledOptions[100];
222
223 private:
224 unsigned int optcount;
225 OptionBase *alloptions[100];
226
227 private:
228 ProgramOptions(const ProgramOptions&); // disabled
229 const ProgramOptions& operator =(const ProgramOptions&); // disabled
230 };
231
232 #endif
233
234