w32tex
About: TeX Live provides a comprehensive TeX system including all the major TeX-related programs, macro packages, and fonts that are free software. Windows sources.
  Fossies Dox: w32tex-src.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

poptions.cpp
Go to the documentation of this file.
1 /*
2  poptions.cpp : This file is part of pstoedit
3  program option handling
4 
5  Copyright (C) 1993 - 2020 Wolfgang Glunz, wglunz35_AT_pstoedit.net
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 */
22 #include "poptions.h"
23 
24 #include I_string_h
25 #include I_stdlib
26 #include I_strstream
27 
28 #include "miscutil.h"
29 
30 #ifdef OS_WIN32_WCE
31 #include "WinCEAdapter.h"
32 #endif
33 
34 USESTD
35 #define UNUSEDARG(p)
36 bool IntValueExtractor::getvalue(const char *optname, const char *instring,
37  unsigned int &currentarg, int &result)
38 {
39  if (instring && isdigit(instring[0])) {
40  result = atoi(instring);
41  currentarg++;
42  return true;
43  } else {
44  cout << "missing integer argument for " << optname << " option:" << endl;
45  return false;
46  }
47 }
49 {
50  return "integer";
51 }
52 
54 {
55  return int_ty;
56 }
57 
58 
59 bool DoubleValueExtractor::getvalue(const char *optname, const char *instring,
60  unsigned int &currentarg, double &result)
61 {
62  if (instring) {
63  result = atof(instring);
64  currentarg++;
65  return true;
66  } else {
67  cout << "missing double argument for " << optname << " option:" << endl;
68  return false;
69  }
70 }
72 {
73  return "double";
74 }
75 
77 {
78  return double_ty;
79 }
80 
81 
82 
83 bool CharacterValueExtractor::getvalue(const char *optname, const char *instring,
84  unsigned int &currentarg, char &result)
85 {
86  if (instring) {
87  result = instring[0];
88  currentarg++;
89  return true;
90  } else {
91  cout << "missing character argument for " << optname << " option" << endl;
92  return false;
93  }
94 }
95 
97 {
98  return "character";
99 }
100 
102 {
103  return char_ty;
104 }
105 
107 {
108  return "boolean";
109 }
110 
112 {
113  return bool_ty;
114 }
115 
116 bool BoolInvertingExtractor::getvalue(const char *UNUSEDARG(optname),
117  const char *UNUSEDARG(instring),
118  unsigned int &UNUSEDARG(currentarg), bool &result)
119 {
120  result = !result; // just invert
121  return true;
122 }
123 
124 
125 bool BoolFalseExtractor::getvalue(const char *UNUSEDARG(optname), const char *UNUSEDARG(instring),
126  unsigned int &UNUSEDARG(currentarg), bool &result)
127 {
128  result = false;
129  return true;
130 }
131 
132 
133 bool BoolTrueExtractor::getvalue(const char *UNUSEDARG(optname), const char *UNUSEDARG(instring),
134  unsigned int &UNUSEDARG(currentarg), bool &result)
135 {
136  result = true;
137  return true;
138 }
139 
140 
141 #if 1
142 // debug
143 void ProgramOptions::showvalues(ostream & outstr, bool withdescription) const
144 {
145  for (unsigned int i = 0; i < optcount; i++) {
146  (void) outstr.width(20);
147  outstr << alloptions[i]->flag << "\t : " << alloptions[i]->gettypename() << "\t : ";
148  if (withdescription)
149  outstr << alloptions[i]->description << "\t : ";
150  (void) alloptions[i]->writevalue(outstr);
151  outstr << endl;
152  }
153 }
154 
156 {
157  C_ostrstream tempstream;
158  (void) writevalue(tempstream);
159  tempstream << std::ends;
160 #ifdef USE_NEWSTRSTREAM
161  const std::string str = tempstream.str();
162  // no freeze / delete needed since ostringstream::str returns a string and not char*
163  result = str.data();
164 #else
165  result = tempstream.str();
166  tempstream.rdbuf()->freeze(0);
167 #endif
168 }
169 #endif
170 
171 unsigned int ProgramOptions::parseoptions(ostream & outstr, unsigned int argc,
172  const char *const *argv)
173 {
174  unhandledCounter = 0;
175  unsigned int i = 1; // argv[0] is not of interest
176  //debug outstr << "parsing options: argc : " << argc << endl;
177  while (i < argc) {
178  bool found = false;
179  for (unsigned int j = 0; j < optcount; j++) {
180  const char *optid = alloptions[j]->flag;
181  if (strcmp(optid, argv[i]) == 0) {
182  //debug outstr << " found arg:" << i << " " << argv[i] << endl;
183  const char *nextarg = (i < argc) ? argv[i + 1] : (const char *) nullptr;
184  if (!alloptions[j]->copyvalue(argv[i], nextarg, i)) {
185  outstr << "Error in option " << argv[i] << endl;
186  }
187  found = true;
188  //debug outstr << " after found arg:" << i << " " << argv[i] << endl;
189  break;
190  }
191  }
192  if (!found) {
193  if ((strlen(argv[i]) > 1) && (argv[i])[0] == '-') {
194  outstr << "unknown option " << argv[i] << endl;
195  } else {
198  }
199  }
200  i++;
201  }
202  return unhandledCounter;
203 }
204 
205 static void TeXescapedOutput(ostream & outstr, const char *const st)
206 {
207  const char *s = st;
208  while (s && *s) {
209  switch (*s) {
210  case '[':
211  outstr << "\\Lbr";
212  break;
213  case ']':
214  outstr << "\\Rbr";
215  break;
216  default:
217  outstr << *s;
218  break;
219  }
220  s++;
221  }
222 }
223 
224 void ProgramOptions::showhelp(ostream & outstr, bool forTeX, bool withdescription, int sheet) const
225 {
226  if (optcount && forTeX && withdescription) {
227  outstr << "The following format specific options are available:" << endl;
228  outstr << "\\begin{description}" << endl;
229  }
230  const char *const terminator = withdescription ? "]" : "";
231  for (unsigned int i = 0; i < optcount; i++) {
232  if (forTeX) {
233  if ((!hideFromDoku(*(alloptions[i])) && (sheet == -1))
234  // -1 means : show all sheets (except the hidden ones)
235  || (alloptions[i]->propsheet == sheet)) {
236  if (withdescription)
237  outstr << "\\item[";
238  if (alloptions[i]->gettypeID() == bool_ty) {
239  if (alloptions[i]->optional)
240  outstr << "\\oOpt{";
241  else
242  outstr << "\\Opt{";
243  TeXescapedOutput(outstr, alloptions[i]->flag);
244  outstr << "}" << terminator << endl;
245  } else {
246  if (alloptions[i]->optional)
247  outstr << "\\oOptArg{";
248  else
249  outstr << "\\OptArg{";
250  TeXescapedOutput(outstr, alloptions[i]->flag);
251  outstr << "}";
252  const char *aname =
253  alloptions[i]->argname ? alloptions[i]->argname : "missing arg name";
254  outstr << "{~";
255  TeXescapedOutput(outstr, aname);
256  outstr << "}" << terminator << endl;
257  }
258  if (withdescription) {
259  const char *help =
261  description;
262  outstr << help << endl << endl;
263  }
264 #if 0
265  (void) outstr.width(20);
266  outstr << alloptions[i]->flag << "\t : " << alloptions[i]->
267  gettypename() << "\t : " << alloptions[i]->description;
268  if (alloptions[i]->optional)
269  outstr << "]";
270 #endif
271  if (withdescription)
272  outstr << endl;
273  }
274 
275  } else {
276  if (alloptions[i]->optional)
277  outstr << "[";
278  (void) outstr.width(20);
279  outstr << alloptions[i]->flag << "\t : " << alloptions[i]->
280  gettypename() << "\t : " << alloptions[i]->description;
281  if (alloptions[i]->optional)
282  outstr << "]";
283  outstr << endl;
284  }
285  }
286 
287  if (forTeX && withdescription) {
288  if (optcount) {
289  outstr << "\\end{description}" << endl;
290  } else {
291  // this happens only in the context of driver options
292  outstr << "No format specific options" << endl;
293  }
294  }
295 
296 }
297 
298 void ProgramOptions::dumpunhandled(ostream & outstr) const
299 {
300  if (unhandledCounter) {
301  outstr << "unhandled parameters " << endl;
302  for (unsigned int i = 0; i < unhandledCounter; i++) {
303  outstr << '\t' << unhandledOptions[i] << endl;
304  }
305  } else {
306  outstr << "no unhandled parameters" << endl;
307  }
308 }
309 
310 void ProgramOptions::add(OptionBase * op, const char *const membername_p)
311 {
313  op->membername = membername_p;
314  alloptions[++optcount] = nullptr;
315 }
static const char * gettypename()
Definition: poptions.cpp:106
static unsigned int gettypeID()
Definition: poptions.cpp:111
static bool getvalue(const char *optname, const char *instring, unsigned int &currentarg, bool &result)
Definition: poptions.cpp:125
static bool getvalue(const char *optname, const char *instring, unsigned int &currentarg, bool &result)
Definition: poptions.cpp:116
static bool getvalue(const char *optname, const char *instring, unsigned int &currentarg, bool &result)
Definition: poptions.cpp:133
static const char * gettypename()
Definition: poptions.cpp:96
static bool getvalue(const char *optname, const char *instring, unsigned int &currentarg, char &result)
Definition: poptions.cpp:83
static unsigned int gettypeID()
Definition: poptions.cpp:101
static const char * gettypename()
Definition: poptions.cpp:71
static bool getvalue(const char *optname, const char *instring, unsigned int &currentarg, double &result)
Definition: poptions.cpp:59
static unsigned int gettypeID()
Definition: poptions.cpp:76
static unsigned int gettypeID()
Definition: poptions.cpp:53
static bool getvalue(const char *optname, const char *instring, unsigned int &currentarg, int &result)
Definition: poptions.cpp:36
static const char * gettypename()
Definition: poptions.cpp:48
const char *const flag
Definition: poptions.h:110
void toString(RSString &) const
Definition: poptions.cpp:155
const char *const argname
Definition: poptions.h:111
virtual const char * gettypename() const =0
const char *const TeXhelp
Definition: poptions.h:114
virtual ostream & writevalue(ostream &out) const =0
const char *const description
Definition: poptions.h:113
void showhelp(ostream &outstr, bool forTeX, bool withdescription, int sheet=-1) const
Definition: poptions.cpp:224
void showvalues(ostream &outstr, bool withdescription=true) const
Definition: poptions.cpp:143
void add(OptionBase *op, const char *const membername_p)
Definition: poptions.cpp:310
OptionBase * alloptions[100]
Definition: poptions.h:225
unsigned int optcount
Definition: poptions.h:224
unsigned int parseoptions(ostream &outstr, unsigned int argc, const char *const *argv)
Definition: poptions.cpp:171
void dumpunhandled(ostream &outstr) const
Definition: poptions.cpp:298
const char * unhandledOptions[100]
Definition: poptions.h:221
unsigned int unhandledCounter
Definition: poptions.h:220
virtual bool hideFromDoku(const OptionBase &) const
Definition: poptions.h:212
#define USESTD
Definition: cppcomp.h:199
#define C_ostrstream
Definition: cppcomp.h:184
static const char description[]
Definition: drvwmf.cpp:127
int strcmp()
Definition: coll.cpp:143
long int flag
Definition: f2c.h:53
static void
Definition: fpif.c:118
#define s
Definition: afcover.h:80
small capitals from c petite p scientific i
Definition: afcover.h:80
int atoi(const char *)
double atof(const char *)
int instring
Definition: dvi.c:296
#define string
Definition: ctangleboot.c:111
#define isdigit(c)
Definition: snprintf.c:177
set set set set set set set set set set set set set set set set set set set set *set set set macro pixldst op &r &cond WK op &r &cond WK op &r &cond WK else op &m &cond &ia op &r &cond WK else op &m &cond &ia elseif elseif else error unsupported base if elseif elseif else error unsupported unaligned pixldst unaligned endm macro pixst base base else pixldst st
static void TeXescapedOutput(ostream &outstr, const char *const st)
Definition: poptions.cpp:205
#define UNUSEDARG(p)
Definition: poptions.cpp:35
@ bool_ty
Definition: poptions.h:37
@ double_ty
Definition: poptions.h:37
@ int_ty
Definition: poptions.h:37
@ char_ty
Definition: poptions.h:37
char terminator[9]
#define str(s)
Definition: sh6.c:399
Definition: sh.h:1226
Definition: stemdb.c:56
int j
Definition: t4ht.c:1589
*job_name strlen((char *) job_name) - 4)
found
Definition: tex4ht.c:5000
op
Definition: tex4ht.c:3129
#define argv
Definition: xmain.c:270
#define argc
Definition: xmain.c:269