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 Minisat_Options_h
21#define Minisat_Options_h
22
23#include <stdlib.h>
24#include <stdio.h>
25#include <math.h>
26#include <string.h>
27
28#include "sat/bsat2/IntTypes.h"
29#include "sat/bsat2/Vec.h"
31
33
34namespace Minisat {
35
36//==================================================================================================
37// Top-level option parse/help functions:
38
39
40extern int parseOptions (int& argc, char** argv, bool strict = false);
41extern int printUsageAndExit(int argc, char** argv, bool verbose = false);
42extern void setUsageHelp (const char* str);
43extern void setHelpPrefixStr (const char* str);
44
45
46//==================================================================================================
47// Options is an abstract class that gives the interface for all types options:
48
49
50class Option
51{
52 public:
53 const char* name;
54 const char* description;
55 const char* category;
56 const char* type_name;
57
58 static vec<Option*>& getOptionList () { static vec<Option*> options; return options; }
59 static const char*& getUsageString() { static const char* usage_str; return usage_str; }
60 static const char*& getHelpPrefixString() { static const char* help_prefix_str = ""; return help_prefix_str; }
61
62 struct OptionLt {
63 bool operator()(const Option* x, const Option* y) {
64 int test1 = strcmp(x->category, y->category);
65 return test1 < 0 || ( test1 == 0 && strcmp(x->type_name, y->type_name) < 0 );
66 }
67 };
68
69 Option(const char* name_,
70 const char* desc_,
71 const char* cate_,
72 const char* type_) :
73 name (name_)
74 , description(desc_)
75 , category (cate_)
76 , type_name (type_)
77 {
78 getOptionList().push(this);
79 }
80
81 public:
82 virtual ~Option() {}
83
84 virtual bool parse (const char* str) = 0;
85 virtual void help (bool verbose = false) = 0;
86
87 friend int parseOptions (int& argc, char** argv, bool strict);
88 friend int printUsageAndExit (int argc, char** argv, bool verbose);
89 friend void setUsageHelp (const char* str);
90 friend void setHelpPrefixStr (const char* str);
91};
92
93
94//==================================================================================================
95// Range classes with specialization for floating types:
96
97
98struct IntRange {
99 int begin;
100 int end;
101 IntRange(int b, int e) : begin(b), end(e) {}
102};
103
105 int64_t begin;
106 int64_t end;
107 Int64Range(int64_t b, int64_t e) : begin(b), end(e) {}
108};
109
111 double begin;
112 double end;
115 DoubleRange(double b, bool binc, double e, bool einc) : begin(b), end(e), begin_inclusive(binc), end_inclusive(einc) {}
116};
117
118
119//==================================================================================================
120// Double options:
121
122
123class DoubleOption : public Option
124{
125 protected:
127 double value;
128
129 public:
130 DoubleOption(const char* c, const char* n, const char* d, double def = double(), DoubleRange r = DoubleRange(-HUGE_VAL, false, HUGE_VAL, false))
131 : Option(n, d, c, "<double>"), range(r), value(def) {
132 // FIXME: set LC_NUMERIC to "C" to make sure that strtof/strtod parses decimal point correctly.
133 }
134
135 operator double (void) const { return value; }
136 operator double& (void) { return value; }
137 DoubleOption& operator=(double x) { value = x; return *this; }
138
139 virtual bool parse(const char* str){
140 const char* span = str;
141
142 if (!match(span, "-") || !match(span, name) || !match(span, "="))
143 return false;
144
145 char* end;
146 double tmp = strtod(span, &end);
147
148 if (end == NULL)
149 return false;
150 else if (tmp >= range.end && (!range.end_inclusive || tmp != range.end)){
151 fprintf(stderr, "ERROR! value <%s> is too large for option \"%s\".\n", span, name);
152 exit(1);
153 }else if (tmp <= range.begin && (!range.begin_inclusive || tmp != range.begin)){
154 fprintf(stderr, "ERROR! value <%s> is too small for option \"%s\".\n", span, name);
155 exit(1); }
156
157 value = tmp;
158 // fprintf(stderr, "READ VALUE: %g\n", value);
159
160 return true;
161 }
162
163 virtual void help (bool verbose = false){
164 fprintf(stderr, " -%-12s = %-8s %c%4.2g .. %4.2g%c (default: %g)\n",
165 name, type_name,
166 range.begin_inclusive ? '[' : '(',
167 range.begin,
168 range.end,
169 range.end_inclusive ? ']' : ')',
170 value);
171 if (verbose){
172 fprintf(stderr, "\n %s\n", description);
173 fprintf(stderr, "\n");
174 }
175 }
176};
177
178
179//==================================================================================================
180// Int options:
181
182
183class IntOption : public Option
184{
185 protected:
187 int32_t value;
188
189 public:
190 IntOption(const char* c, const char* n, const char* d, int32_t def = int32_t(), IntRange r = IntRange(INT32_MIN, INT32_MAX))
191 : Option(n, d, c, "<int32>"), range(r), value(def) {}
192
193 operator int32_t (void) const { return value; }
194 operator int32_t& (void) { return value; }
195 IntOption& operator= (int32_t x) { value = x; return *this; }
196
197 virtual bool parse(const char* str){
198 const char* span = str;
199
200 if (!match(span, "-") || !match(span, name) || !match(span, "="))
201 return false;
202
203 char* end;
204 int32_t tmp = strtol(span, &end, 10);
205
206 if (end == NULL)
207 return false;
208 else if (tmp > range.end){
209 fprintf(stderr, "ERROR! value <%s> is too large for option \"%s\".\n", span, name);
210 exit(1);
211 }else if (tmp < range.begin){
212 fprintf(stderr, "ERROR! value <%s> is too small for option \"%s\".\n", span, name);
213 exit(1); }
214
215 value = tmp;
216
217 return true;
218 }
219
220 virtual void help (bool verbose = false){
221 fprintf(stderr, " -%-12s = %-8s [", name, type_name);
222 if (range.begin == INT32_MIN)
223 fprintf(stderr, "imin");
224 else
225 fprintf(stderr, "%4d", range.begin);
226
227 fprintf(stderr, " .. ");
228 if (range.end == INT32_MAX)
229 fprintf(stderr, "imax");
230 else
231 fprintf(stderr, "%4d", range.end);
232
233 fprintf(stderr, "] (default: %d)\n", value);
234 if (verbose){
235 fprintf(stderr, "\n %s\n", description);
236 fprintf(stderr, "\n");
237 }
238 }
239};
240
241/*
242// Leave this out for visual C++ until Microsoft implements C99 and gets support for strtoll.
243#ifndef _MSC_VER
244
245class Int64Option : public Option
246{
247 protected:
248 Int64Range range;
249 int64_t value;
250
251 public:
252 Int64Option(const char* c, const char* n, const char* d, int64_t def = int64_t(), Int64Range r = Int64Range(INT64_MIN, INT64_MAX))
253 : Option(n, d, c, "<int64>"), range(r), value(def) {}
254
255 operator int64_t (void) const { return value; }
256 operator int64_t& (void) { return value; }
257 Int64Option& operator= (int64_t x) { value = x; return *this; }
258
259 virtual bool parse(const char* str){
260 const char* span = str;
261
262 if (!match(span, "-") || !match(span, name) || !match(span, "="))
263 return false;
264
265 char* end;
266 int64_t tmp = strtoll(span, &end, 10);
267
268 if (end == NULL)
269 return false;
270 else if (tmp > range.end){
271 fprintf(stderr, "ERROR! value <%s> is too large for option \"%s\".\n", span, name);
272 exit(1);
273 }else if (tmp < range.begin){
274 fprintf(stderr, "ERROR! value <%s> is too small for option \"%s\".\n", span, name);
275 exit(1); }
276
277 value = tmp;
278
279 return true;
280 }
281
282 virtual void help (bool verbose = false){
283 fprintf(stderr, " -%-12s = %-8s [", name, type_name);
284 if (range.begin == INT64_MIN)
285 fprintf(stderr, "imin");
286 else
287 fprintf(stderr, "%4"PRIi64, range.begin);
288
289 fprintf(stderr, " .. ");
290 if (range.end == INT64_MAX)
291 fprintf(stderr, "imax");
292 else
293 fprintf(stderr, "%4"PRIi64, range.end);
294
295 fprintf(stderr, "] (default: %"PRIi64")\n", value);
296 if (verbose){
297 fprintf(stderr, "\n %s\n", description);
298 fprintf(stderr, "\n");
299 }
300 }
301};
302#endif
303*/
304
305//==================================================================================================
306// String option:
307
308
309class StringOption : public Option
310{
311 const char* value;
312 public:
313 StringOption(const char* c, const char* n, const char* d, const char* def = NULL)
314 : Option(n, d, c, "<string>"), value(def) {}
315
316 operator const char* (void) const { return value; }
317 operator const char*& (void) { return value; }
318 StringOption& operator= (const char* x) { value = x; return *this; }
319
320 virtual bool parse(const char* str){
321 const char* span = str;
322
323 if (!match(span, "-") || !match(span, name) || !match(span, "="))
324 return false;
325
326 value = span;
327 return true;
328 }
329
330 virtual void help (bool verbose = false){
331 fprintf(stderr, " -%-10s = %8s\n", name, type_name);
332 if (verbose){
333 fprintf(stderr, "\n %s\n", description);
334 fprintf(stderr, "\n");
335 }
336 }
337};
338
339
340//==================================================================================================
341// Bool option:
342
343
344class BoolOption : public Option
345{
346 bool value;
347
348 public:
349 BoolOption(const char* c, const char* n, const char* d, bool v)
350 : Option(n, d, c, "<bool>"), value(v) {}
351
352 operator bool (void) const { return value; }
353 operator bool& (void) { return value; }
354 BoolOption& operator=(bool b) { value = b; return *this; }
355
356 virtual bool parse(const char* str){
357 const char* span = str;
358
359 if (match(span, "-")){
360 bool b = !match(span, "no-");
361
362 if (strcmp(span, name) == 0){
363 value = b;
364 return true; }
365 }
366
367 return false;
368 }
369
370 virtual void help (bool verbose = false){
371
372 fprintf(stderr, " -%s, -no-%s", name, name);
373
374 for (uint32_t i = 0; i < 32 - strlen(name)*2; i++)
375 fprintf(stderr, " ");
376
377 fprintf(stderr, " ");
378 fprintf(stderr, "(default: %s)\n", value ? "on" : "off");
379 if (verbose){
380 fprintf(stderr, "\n %s\n", description);
381 fprintf(stderr, "\n");
382 }
383 }
384};
385
386//=================================================================================================
387}
388
390
391#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
BoolOption(const char *c, const char *n, const char *d, bool v)
Definition Options.h:349
BoolOption & operator=(bool b)
Definition Options.h:354
virtual void help(bool verbose=false)
Definition Options.h:370
virtual bool parse(const char *str)
Definition Options.h:356
DoubleRange range
Definition Options.h:126
virtual bool parse(const char *str)
Definition Options.h:139
virtual void help(bool verbose=false)
Definition Options.h:163
DoubleOption & operator=(double x)
Definition Options.h:137
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:130
virtual bool parse(const char *str)
Definition Options.h:197
IntOption & operator=(int32_t x)
Definition Options.h:195
virtual void help(bool verbose=false)
Definition Options.h:220
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:190
virtual bool parse(const char *str)=0
const char * name
Definition Options.h:53
virtual void help(bool verbose=false)=0
Option(const char *name_, const char *desc_, const char *cate_, const char *type_)
Definition Options.h:69
friend int printUsageAndExit(int argc, char **argv, bool verbose)
const char * category
Definition Options.h:55
const char * type_name
Definition Options.h:56
static const char *& getUsageString()
Definition Options.h:59
friend int parseOptions(int &argc, char **argv, bool strict)
static const char *& getHelpPrefixString()
Definition Options.h:60
const char * description
Definition Options.h:54
virtual ~Option()
Definition Options.h:82
friend void setHelpPrefixStr(const char *str)
static vec< Option * > & getOptionList()
Definition Options.h:58
friend void setUsageHelp(const char *str)
virtual bool parse(const char *str)
Definition Options.h:320
StringOption & operator=(const char *x)
Definition Options.h:318
virtual void help(bool verbose=false)
Definition Options.h:330
StringOption(const char *c, const char *n, const char *d, const char *def=NULL)
Definition Options.h:313
#define bool
Definition espresso.h:254
Definition Alg.h:28
void setUsageHelp(const char *str)
Definition Options.cpp:62
int printUsageAndExit(int argc, char **argv, bool verbose=false)
Definition Options.cpp:64
void setHelpPrefixStr(const char *str)
Definition Options.cpp:63
int parseOptions(int &argc, char **argv, bool strict=false)
Definition Options.cpp:28
DoubleRange(double b, bool binc, double e, bool einc)
Definition Options.h:115
Int64Range(int64_t b, int64_t e)
Definition Options.h:107
IntRange(int b, int e)
Definition Options.h:101
bool operator()(const Option *x, const Option *y)
Definition Options.h:63
int strlen()
int strcmp()
VOID_HACK exit()
#define HUGE_VAL
Definition util_old.h:295