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 Minisat_ParseUtils_h
22 #define Minisat_ParseUtils_h
23
24 #include <stdlib.h>
25 #include <stdio.h>
26
27 #include <zlib.h>
28
29 namespace Parse {
30
31 //-------------------------------------------------------------------------------------------------
32 // A simple buffered character stream class:
33
34 static const int buffer_size = 1048576;
35
36
37 class StreamBuffer {
38 gzFile in;
39 unsigned char buf[buffer_size];
40 int pos;
41 int size;
42
assureLookahead()43 void assureLookahead() {
44 if (pos >= size) {
45 pos = 0;
46 size = gzread(in, buf, sizeof(buf)); } }
47
48 public:
StreamBuffer(gzFile i)49 explicit StreamBuffer(gzFile i) : in(i), pos(0), size(0) { assureLookahead(); }
50
51 int operator * () const { return (pos >= size) ? EOF : buf[pos]; }
52 void operator ++ () { pos++; assureLookahead(); }
position()53 int position () const { return pos; }
54 };
55
56
57 //-------------------------------------------------------------------------------------------------
58 // End-of-file detection functions for StreamBuffer and char*:
59
60
isEof(StreamBuffer & in)61 static inline bool isEof(StreamBuffer& in) { return *in == EOF; }
isEof(const char * in)62 static inline bool isEof(const char* in) { return *in == '\0'; }
63
64 //-------------------------------------------------------------------------------------------------
65 // Generic parse functions parametrized over the input-stream type.
66
67
68 template<class B>
skipWhitespace(B & in)69 static void skipWhitespace(B& in) {
70 while ((*in >= 9 && *in <= 13) || *in == 32)
71 ++in; }
72
73
74 template<class B>
skipLine(B & in)75 static void skipLine(B& in) {
76 for (;;){
77 if (isEof(in)) return;
78 if (*in == '\n') { ++in; return; }
79 ++in; } }
80
81
82 template<class B>
parseInt(B & in)83 static int parseInt(B& in) {
84 int val = 0;
85 bool neg = false;
86 skipWhitespace(in);
87 if (*in == '-') neg = true, ++in;
88 else if (*in == '+') ++in;
89 if (*in < '0' || *in > '9') fprintf(stderr, "PARSE ERROR! Unexpected char: %c\n", *in), exit(3);
90 while (*in >= '0' && *in <= '9')
91 val = val*10 + (*in - '0'),
92 ++in;
93 return neg ? -val : val; }
94
95
96 // String matching: in case of a match the input iterator will be advanced the corresponding
97 // number of characters.
98 template<class B>
match(B & in,const char * str)99 static bool match(B& in, const char* str) {
100 int i;
101 for (i = 0; str[i] != '\0'; i++)
102 if (in[i] != str[i])
103 return false;
104
105 in += i;
106
107 return true;
108 }
109
110 // String matching: consumes characters eagerly, but does not require random access iterator.
111 template<class B>
eagerMatch(B & in,const char * str)112 static bool eagerMatch(B& in, const char* str) {
113 for (; *str != '\0'; ++str, ++in)
114 if (*str != *in)
115 return false;
116 return true; }
117
118
119 //=================================================================================================
120 }
121
122 #endif
123