ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
Options.h
Go to the documentation of this file.
1/***************************************************************************************[Options.h]
2Copyright (c) 2008-2010, Niklas Sorensson
3
4Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
5associated documentation files (the "Software"), to deal in the Software without restriction,
6including without limitation the rights to use, copy, modify, merge, publish, distribute,
7sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
8furnished to do so, subject to the following conditions:
9
10The above copyright notice and this permission notice shall be included in all copies or
11substantial portions of the Software.
12
13THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
14NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
16DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
17OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18**************************************************************************************************/
19
20#ifndef Glucose_Options_h
21#define Glucose_Options_h
22
23#define __STDC_FORMAT_MACROS
24
25#include <stdlib.h>
26#include <stdio.h>
27#include <math.h>
28#include <string.h>
29
31#include "sat/glucose2/Vec.h"
33
35
36namespace Gluco2 {
37
38//==================================================================================================
39// Top-level option parse/help functions:
40
41
42extern void parseOptions (int& argc, char** argv, bool strict = false);
43extern void printUsageAndExit(int argc, char** argv, bool verbose = false);
44extern void setUsageHelp (const char* str);
45extern void setHelpPrefixStr (const char* str);
46
47
48//==================================================================================================
49// Options is an abstract class that gives the interface for all types options:
50
51
52class Option
53{
54 public:
55 const char* name;
56 const char* description;
57 const char* category;
58 const char* type_name;
59
60 static vec<Option*>& getOptionList () { static vec<Option*> options; return options; }
61 static const char*& getUsageString() { static const char* usage_str; return usage_str; }
62 static const char*& getHelpPrefixString() { static const char* help_prefix_str = ""; return help_prefix_str; }
63
64 struct OptionLt {
65 bool operator()(const Option* x, const Option* y) {
66 int test1 = strcmp(x->category, y->category);
67 return test1 < 0 || (test1 == 0 && strcmp(x->type_name, y->type_name) < 0);
68 }
69 };
70
71 Option(const char* name_,
72 const char* desc_,
73 const char* cate_,
74 const char* type_) :
75 name (name_)
76 , description(desc_)
77 , category (cate_)
78 , type_name (type_)
79 {
80 getOptionList().push(this);
81 }
82
83 public:
84 virtual ~Option() {}
85
86 virtual bool parse (const char* str) = 0;
87 virtual void help (bool verbose = false) = 0;
88
89 friend void parseOptions (int& argc, char** argv, bool strict);
90 friend void printUsageAndExit (int argc, char** argv, bool verbose);
91 friend void setUsageHelp (const char* str);
92 friend void setHelpPrefixStr (const char* str);
93};
94
95
96//==================================================================================================
97// Range classes with specialization for floating types:
98
99
100struct IntRange {
101 int begin;
102 int end;
103 IntRange(int b, int e) : begin(b), end(e) {}
104};
105
107 int64_t begin;
108 int64_t end;
109 Int64Range(int64_t b, int64_t e) : begin(b), end(e) {}
110};
111
113 double begin;
114 double end;
117 DoubleRange(double b, bool binc, double e, bool einc) : begin(b), end(e), begin_inclusive(binc), end_inclusive(einc) {}
118};
119
120
121//==================================================================================================
122// Double options:
123
124
125class DoubleOption : public Option
126{
127 protected:
129 double value;
130
131 public:
132 DoubleOption(const char* c, const char* n, const char* d, double def = double(), DoubleRange r = DoubleRange(-HUGE_VAL, false, HUGE_VAL, false))
133 : Option(n, d, c, "<double>"), range(r), value(def) {
134 // FIXME: set LC_NUMERIC to "C" to make sure that strtof/strtod parses decimal point correctly.
135 }
136
137 operator double (void) const { return value; }
138 operator double& (void) { return value; }
139 DoubleOption& operator=(double x) { value = x; return *this; }
140
141 virtual bool parse(const char* str){
142 const char* span = str;
143
144 if (!match(span, "-") || !match(span, name) || !match(span, "="))
145 return false;
146
147 char* end;
148 double tmp = strtod(span, &end);
149
150 if (end == NULL)
151 return false;
152 else if (tmp >= range.end && (!range.end_inclusive || tmp != range.end)){
153 fprintf(stderr, "ERROR! value <%s> is too large for option \"%s\".\n", span, name);
154 exit(1);
155 }else if (tmp <= range.begin && (!range.begin_inclusive || tmp != range.begin)){
156 fprintf(stderr, "ERROR! value <%s> is too small for option \"%s\".\n", span, name);
157 exit(1); }
158
159 value = tmp;
160 // fprintf(stderr, "READ VALUE: %g\n", value);
161
162 return true;
163 }
164
165 virtual void help (bool verbose = false){
166 fprintf(stderr, " -%-12s = %-8s %c%4.2g .. %4.2g%c (default: %g)\n",
167 name, type_name,
168 range.begin_inclusive ? '[' : '(',
169 range.begin,
170 range.end,
171 range.end_inclusive ? ']' : ')',
172 value);
173 if (verbose){
174 fprintf(stderr, "\n %s\n", description);
175 fprintf(stderr, "\n");
176 }
177 }
178};
179
180
181//==================================================================================================
182// Int options:
183
184
185class IntOption : public Option
186{
187 protected:
189 int32_t value;
190
191 public:
192 IntOption(const char* c, const char* n, const char* d, int32_t def = int32_t(), IntRange r = IntRange(INT32_MIN, INT32_MAX))
193 : Option(n, d, c, "<int32>"), range(r), value(def) {}
194
195 operator int32_t (void) const { return value; }
196 operator int32_t& (void) { return value; }
197 IntOption& operator= (int32_t x) { value = x; return *this; }
198
199 virtual bool parse(const char* str){
200 const char* span = str;
201
202 if (!match(span, "-") || !match(span, name) || !match(span, "="))
203 return false;
204
205 char* end;
206 int32_t tmp = strtol(span, &end, 10);
207
208 if (end == NULL)
209 return false;
210 else if (tmp > range.end){
211 fprintf(stderr, "ERROR! value <%s> is too large for option \"%s\".\n", span, name);
212 exit(1);
213 }else if (tmp < range.begin){
214 fprintf(stderr, "ERROR! value <%s> is too small for option \"%s\".\n", span, name);
215 exit(1); }
216
217 value = tmp;
218
219 return true;
220 }
221
222 virtual void help (bool verbose = false){
223 fprintf(stderr, " -%-12s = %-8s [", name, type_name);
224 if (range.begin == INT32_MIN)
225 fprintf(stderr, "imin");
226 else
227 fprintf(stderr, "%4d", range.begin);
228
229 fprintf(stderr, " .. ");
230 if (range.end == INT32_MAX)
231 fprintf(stderr, "imax");
232 else
233 fprintf(stderr, "%4d", range.end);
234
235 fprintf(stderr, "] (default: %d)\n", value);
236 if (verbose){
237 fprintf(stderr, "\n %s\n", description);
238 fprintf(stderr, "\n");
239 }
240 }
241};
242
243
244// Leave this out for visual C++ until Microsoft implements C99 and gets support for strtoll.
245#ifndef _MSC_VER
246
247class Int64Option : public Option
248{
249 protected:
251 int64_t value;
252
253 public:
254 Int64Option(const char* c, const char* n, const char* d, int64_t def = int64_t(), Int64Range r = Int64Range(INT64_MIN, INT64_MAX))
255 : Option(n, d, c, "<int64>"), range(r), value(def) {}
256
257 operator int64_t (void) const { return value; }
258 operator int64_t& (void) { return value; }
259 Int64Option& operator= (int64_t x) { value = x; return *this; }
260
261 virtual bool parse(const char* str){
262 const char* span = str;
263
264 if (!match(span, "-") || !match(span, name) || !match(span, "="))
265 return false;
266
267 char* end;
268 int64_t tmp = strtoll(span, &end, 10);
269
270 if (end == NULL)
271 return false;
272 else if (tmp > range.end){
273 fprintf(stderr, "ERROR! value <%s> is too large for option \"%s\".\n", span, name);
274 exit(1);
275 }else if (tmp < range.begin){
276 fprintf(stderr, "ERROR! value <%s> is too small for option \"%s\".\n", span, name);
277 exit(1); }
278
279 value = tmp;
280
281 return true;
282 }
283
284 virtual void help (bool verbose = false){
285 fprintf(stderr, " -%-12s = %-8s [", name, type_name);
286 if (range.begin == INT64_MIN)
287 fprintf(stderr, "imin");
288 else
289 fprintf(stderr, "%4d", (int)range.begin);
290
291 fprintf(stderr, " .. ");
292 if (range.end == INT64_MAX)
293 fprintf(stderr, "imax");
294 else
295 fprintf(stderr, "%4d", (int)range.end);
296
297 fprintf(stderr, "] (default: %d)\n", (int)value);
298 if (verbose){
299 fprintf(stderr, "\n %s\n", description);
300 fprintf(stderr, "\n");
301 }
302 }
303};
304#endif
305
306//==================================================================================================
307// String option:
308
309
310class StringOption : public Option
311{
312 const char* value;
313 public:
314 StringOption(const char* c, const char* n, const char* d, const char* def = NULL)
315 : Option(n, d, c, "<string>"), value(def) {}
316
317 operator const char* (void) const { return value; }
318 operator const char*& (void) { return value; }
319 StringOption& operator= (const char* x) { value = x; return *this; }
320
321 virtual bool parse(const char* str){
322 const char* span = str;
323
324 if (!match(span, "-") || !match(span, name) || !match(span, "="))
325 return false;
326
327 value = span;
328 return true;
329 }
330
331 virtual void help (bool verbose = false){
332 fprintf(stderr, " -%-10s = %8s\n", name, type_name);
333 if (verbose){
334 fprintf(stderr, "\n %s\n", description);
335 fprintf(stderr, "\n");
336 }
337 }
338};
339
340
341//==================================================================================================
342// Bool option:
343
344
345class BoolOption : public Option
346{
347 bool value;
348
349 public:
350 BoolOption(const char* c, const char* n, const char* d, bool v)
351 : Option(n, d, c, "<bool>"), value(v) {}
352
353 operator bool (void) const { return value; }
354 operator bool& (void) { return value; }
355 BoolOption& operator=(bool b) { value = b; return *this; }
356
357 virtual bool parse(const char* str){
358 const char* span = str;
359
360 if (match(span, "-")){
361 bool b = !match(span, "no-");
362
363 if (strcmp(span, name) == 0){
364 value = b;
365 return true; }
366 }
367
368 return false;
369 }
370
371 virtual void help (bool verbose = false){
372
373 fprintf(stderr, " -%s, -no-%s", name, name);
374
375 for (uint32_t i = 0; i < 32 - strlen(name)*2; i++)
376 fprintf(stderr, " ");
377
378 fprintf(stderr, " ");
379 fprintf(stderr, "(default: %s)\n", value ? "on" : "off");
380 if (verbose){
381 fprintf(stderr, "\n %s\n", description);
382 fprintf(stderr, "\n");
383 }
384 }
385};
386
387//=================================================================================================
388}
389
391
392#endif
unsigned int uint32_t
Definition Fxch.h:32
#define ABC_NAMESPACE_CXX_HEADER_START
#define ABC_NAMESPACE_CXX_HEADER_END
#define INT32_MAX
Definition pstdint.h:415
#define INT32_MIN
Definition pstdint.h:418
virtual bool parse(const char *str)
Definition Options.h:357
BoolOption(const char *c, const char *n, const char *d, bool v)
Definition Options.h:350
virtual void help(bool verbose=false)
Definition Options.h:371
BoolOption & operator=(bool b)
Definition Options.h:355
DoubleOption(const char *c, const char *n, const char *d, double def=double(), DoubleRange r=DoubleRange(-HUGE_VAL, false, HUGE_VAL, false))
Definition Options.h:132
DoubleRange range
Definition Options.h:128
virtual bool parse(const char *str)
Definition Options.h:141
DoubleOption & operator=(double x)
Definition Options.h:139
virtual void help(bool verbose=false)
Definition Options.h:165
Int64Option(const char *c, const char *n, const char *d, int64_t def=int64_t(), Int64Range r=Int64Range(INT64_MIN, INT64_MAX))
Definition Options.h:254
Int64Option & operator=(int64_t x)
Definition Options.h:259
Int64Range range
Definition Options.h:250
virtual void help(bool verbose=false)
Definition Options.h:284
virtual bool parse(const char *str)
Definition Options.h:261
IntRange range
Definition Options.h:188
IntOption & operator=(int32_t x)
Definition Options.h:197
virtual void help(bool verbose=false)
Definition Options.h:222
virtual bool parse(const char *str)
Definition Options.h:199
IntOption(const char *c, const char *n, const char *d, int32_t def=int32_t(), IntRange r=IntRange(INT32_MIN, INT32_MAX))
Definition Options.h:192
virtual void help(bool verbose=false)=0
virtual bool parse(const char *str)=0
static const char *& getHelpPrefixString()
Definition Options.h:62
const char * category
Definition Options.h:57
friend void printUsageAndExit(int argc, char **argv, bool verbose)
friend void parseOptions(int &argc, char **argv, bool strict)
const char * name
Definition Options.h:55
static const char *& getUsageString()
Definition Options.h:61
Option(const char *name_, const char *desc_, const char *cate_, const char *type_)
Definition Options.h:71
static vec< Option * > & getOptionList()
Definition Options.h:60
virtual ~Option()
Definition Options.h:84
const char * type_name
Definition Options.h:58
friend void setHelpPrefixStr(const char *str)
friend void setUsageHelp(const char *str)
const char * description
Definition Options.h:56
StringOption & operator=(const char *x)
Definition Options.h:319
StringOption(const char *c, const char *n, const char *d, const char *def=NULL)
Definition Options.h:314
virtual void help(bool verbose=false)
Definition Options.h:331
virtual bool parse(const char *str)
Definition Options.h:321
#define bool
Definition espresso.h:254
Definition Alg.h:28
void setUsageHelp(const char *str)
Definition Options2.cpp:60
void setHelpPrefixStr(const char *str)
Definition Options2.cpp:61
void printUsageAndExit(int argc, char **argv, bool verbose=false)
Definition Options2.cpp:62
void parseOptions(int &argc, char **argv, bool strict=false)
Definition Options2.cpp:28
DoubleRange(double b, bool binc, double e, bool einc)
Definition Options.h:117
Int64Range(int64_t b, int64_t e)
Definition Options.h:109
IntRange(int b, int e)
Definition Options.h:103
bool operator()(const Option *x, const Option *y)
Definition Options.h:65
int strlen()
int strcmp()
VOID_HACK exit()
#define HUGE_VAL
Definition util_old.h:295