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