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