1 |
|
/************************************************************************************[ParseUtils.h] |
2 |
|
Copyright (c) 2003-2006, Niklas Een, Niklas Sorensson |
3 |
|
Copyright (c) 2007-2010, Niklas Sorensson |
4 |
|
|
5 |
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and |
6 |
|
associated documentation files (the "Software"), to deal in the Software without restriction, |
7 |
|
including without limitation the rights to use, copy, modify, merge, publish, distribute, |
8 |
|
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
9 |
|
furnished to do so, subject to the following conditions: |
10 |
|
|
11 |
|
The above copyright notice and this permission notice shall be included in all copies or |
12 |
|
substantial portions of the Software. |
13 |
|
|
14 |
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT |
15 |
|
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
16 |
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
17 |
|
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT |
18 |
|
OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
19 |
|
**************************************************************************************************/ |
20 |
|
|
21 |
|
#ifndef BVMinisat_ParseUtils_h |
22 |
|
#define BVMinisat_ParseUtils_h |
23 |
|
|
24 |
|
#include <stdlib.h> |
25 |
|
#include <stdio.h> |
26 |
|
|
27 |
|
//#include <zlib.h> |
28 |
|
#include <unistd.h> |
29 |
|
|
30 |
|
namespace cvc5 { |
31 |
|
namespace BVMinisat { |
32 |
|
|
33 |
|
//------------------------------------------------------------------------------------------------- |
34 |
|
// A simple buffered character stream class: |
35 |
|
|
36 |
|
static const int buffer_size = 1048576; |
37 |
|
|
38 |
|
|
39 |
|
class StreamBuffer { |
40 |
|
int in; |
41 |
|
unsigned char buf[buffer_size]; |
42 |
|
int pos; |
43 |
|
int size; |
44 |
|
|
45 |
|
void assureLookahead() { |
46 |
|
if (pos >= size) { |
47 |
|
pos = 0; |
48 |
|
size = read(in, buf, sizeof(buf)); } } |
49 |
|
|
50 |
|
public: |
51 |
|
explicit StreamBuffer(int i) : in(i), pos(0), size(0) { assureLookahead(); } |
52 |
|
|
53 |
|
int operator * () const { return (pos >= size) ? EOF : buf[pos]; } |
54 |
|
void operator ++ () { pos++; assureLookahead(); } |
55 |
|
int position () const { return pos; } |
56 |
|
}; |
57 |
|
|
58 |
|
|
59 |
|
//------------------------------------------------------------------------------------------------- |
60 |
|
// End-of-file detection functions for StreamBuffer and char*: |
61 |
|
|
62 |
|
|
63 |
|
static inline bool isEof(StreamBuffer& in) { return *in == EOF; } |
64 |
|
static inline bool isEof(const char* in) { return *in == '\0'; } |
65 |
|
|
66 |
|
//------------------------------------------------------------------------------------------------- |
67 |
|
// Generic parse functions parametrized over the input-stream type. |
68 |
|
|
69 |
|
|
70 |
|
template<class B> |
71 |
|
static void skipWhitespace(B& in) { |
72 |
|
while ((*in >= 9 && *in <= 13) || *in == 32) |
73 |
|
++in; } |
74 |
|
|
75 |
|
|
76 |
|
template<class B> |
77 |
|
static void skipLine(B& in) { |
78 |
|
for (;;){ |
79 |
|
if (isEof(in)) return; |
80 |
|
if (*in == '\n') { ++in; return; } |
81 |
|
++in; } } |
82 |
|
|
83 |
|
|
84 |
|
template<class B> |
85 |
|
static int parseInt(B& in) { |
86 |
|
int val = 0; |
87 |
|
bool neg = false; |
88 |
|
skipWhitespace(in); |
89 |
|
if (*in == '-') neg = true, ++in; |
90 |
|
else if (*in == '+') ++in; |
91 |
|
if (*in < '0' || *in > '9') fprintf(stderr, "PARSE ERROR! Unexpected char: %c\n", *in), exit(3); |
92 |
|
while (*in >= '0' && *in <= '9') |
93 |
|
val = val*10 + (*in - '0'), |
94 |
|
++in; |
95 |
|
return neg ? -val : val; } |
96 |
|
|
97 |
|
|
98 |
|
// String matching: in case of a match the input iterator will be advanced the corresponding |
99 |
|
// number of characters. |
100 |
|
template<class B> |
101 |
|
static bool match(B& in, const char* str) { |
102 |
|
int i; |
103 |
|
for (i = 0; str[i] != '\0'; i++) |
104 |
|
if (in[i] != str[i]) |
105 |
|
return false; |
106 |
|
|
107 |
|
in += i; |
108 |
|
|
109 |
|
return true; |
110 |
|
} |
111 |
|
|
112 |
|
// String matching: consumes characters eagerly, but does not require random access iterator. |
113 |
|
template<class B> |
114 |
|
static bool eagerMatch(B& in, const char* str) { |
115 |
|
for (; *str != '\0'; ++str, ++in) |
116 |
|
if (*str != *in) |
117 |
|
return false; |
118 |
|
return true; } |
119 |
|
|
120 |
|
|
121 |
|
//================================================================================================= |
122 |
|
} // namespace BVMinisat |
123 |
|
} // namespace cvc5 |
124 |
|
|
125 |
|
#endif |