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