edit
[satlib.git] / glucose-syrup / utils / ParseUtils.h
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 Glucose_ParseUtils_h
22 #define Glucose_ParseUtils_h
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <math.h>
27
28 #include <zlib.h>
29
30 namespace Glucose {
31
32 //-------------------------------------------------------------------------------------------------
33 // A simple buffered character stream class:
34
35 static const int buffer_size = 1048576;
36
37
38 class StreamBuffer {
39     gzFile        in;
40     unsigned char buf[buffer_size];
41     int           pos;
42     int           size;
43
44     void assureLookahead() {
45         if (pos >= size) {
46             pos  = 0;
47             size = gzread(in, buf, sizeof(buf)); } }
48
49 public:
50     explicit StreamBuffer(gzFile i) : in(i), pos(0), size(0) { assureLookahead(); }
51
52     int  operator *  () const { return (pos >= size) ? EOF : buf[pos]; }
53     void operator ++ ()       { pos++; assureLookahead(); }
54     int  position    () const { return pos; }
55 };
56
57
58 //-------------------------------------------------------------------------------------------------
59 // End-of-file detection functions for StreamBuffer and char*:
60
61
62 static inline bool isEof(StreamBuffer& in) { return *in == EOF;  }
63 static inline bool isEof(const char*   in) { return *in == '\0'; }
64
65 //-------------------------------------------------------------------------------------------------
66 // Generic parse functions parametrized over the input-stream type.
67
68
69 template<class B>
70 static void skipWhitespace(B& in) {
71     while ((*in >= 9 && *in <= 13) || *in == 32)
72         ++in; }
73
74
75 template<class B>
76 static void skipLine(B& in) {
77     for (;;){
78         if (isEof(in)) return;
79         if (*in == '\n') { ++in; return; }
80         ++in; } }
81
82 template<class B>
83 static double parseDouble(B& in) { // only in the form X.XXXXXe-XX
84     bool    neg= false;
85         double accu = 0.0;
86         double currentExponent = 1;
87         int exponent;
88         
89     skipWhitespace(in);
90     if(*in == EOF) return 0;
91     if      (*in == '-') neg = true, ++in;
92     else if (*in == '+') ++in;
93     if (*in < '1' || *in > '9') printf("PARSE ERROR! Unexpected char: %c\n", *in), exit(3);
94         accu = (double)(*in - '0');
95         ++in;
96         if (*in != '.') printf("PARSE ERROR! Unexpected char: %c\n", *in),exit(3);
97         ++in; // skip dot
98         currentExponent = 0.1;
99     while (*in >= '0' && *in <= '9')
100         accu = accu + currentExponent * ((double)(*in - '0')),
101                 currentExponent /= 10,
102         ++in;
103         if (*in != 'e') printf("PARSE ERROR! Unexpected char: %c\n", *in),exit(3);
104         ++in; // skip dot
105         exponent = parseInt(in); // read exponent
106         accu *= pow(10,exponent);
107         return neg ? -accu:accu;
108 }
109
110
111 template<class B>
112 static int parseInt(B& in) {
113     int     val = 0;
114     bool    neg = false;
115     skipWhitespace(in);
116     if      (*in == '-') neg = true, ++in;
117     else if (*in == '+') ++in;
118     if (*in < '0' || *in > '9') fprintf(stderr, "PARSE ERROR! Unexpected char: %c\n", *in), exit(3);
119     while (*in >= '0' && *in <= '9')
120         val = val*10 + (*in - '0'),
121         ++in;
122     return neg ? -val : val; }
123
124
125 // String matching: in case of a match the input iterator will be advanced the corresponding
126 // number of characters.
127 template<class B>
128 static bool match(B& in, const char* str) {
129     int i;
130     for (i = 0; str[i] != '\0'; i++)
131         if (in[i] != str[i])
132             return false;
133
134     in += i;
135
136     return true; 
137 }
138
139 // String matching: consumes characters eagerly, but does not require random access iterator.
140 template<class B>
141 static bool eagerMatch(B& in, const char* str) {
142     for (; *str != '\0'; ++str, ++in)
143         if (*str != *in)
144             return false;
145     return true; }
146
147
148 //=================================================================================================
149 }
150
151 #endif