Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

ParseUtils.h
Go to the documentation of this file.
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 #pragma once
22 
23 #include <stdlib.h>
24 #include <stdio.h>
25 
26 //#include <ogdf/lib/minisat/win/zlib.h>
27 
28 #pragma GCC visibility push(default)
29 namespace Minisat {
30 namespace Internal {
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 
67 //-------------------------------------------------------------------------------------------------
68 // Generic parse functions parametrized over the input-stream type.
69 
70 
71 template<class B>
72 static void skipWhitespace(B& in) {
73  while ((*in >= 9 && *in <= 13) || *in == 32)
74  ++in; }
75 
76 
77 template<class B>
78 static void skipLine(B& in) {
79  for (;;){
80  if (isEof(in)) return;
81  if (*in == '\n') { ++in; return; }
82  ++in; } }
83 
84 
85 template<class B>
86 static int parseInt(B& in) {
87  int val = 0;
88  bool neg = false;
89  skipWhitespace(in);
90  if (*in == '-') neg = true, ++in;
91  else if (*in == '+') ++in;
92  if (*in < '0' || *in > '9') fprintf(stderr, "PARSE ERROR! Unexpected char: %c\n", *in), exit(3);
93  while (*in >= '0' && *in <= '9')
94  val = val*10 + (*in - '0'),
95  ++in;
96  return neg ? -val : val; }
97 
98 
99 // String matching: in case of a match the input iterator will be advanced the corresponding
100 // number of characters.
101 template<class B>
102 static bool match(B& in, const char* str) {
103  int i;
104  for (i = 0; str[i] != '\0'; i++)
105  if (in[i] != str[i])
106  return false;
107 
108  in += i;
109 
110  return true;
111 }
112 
113 // String matching: consumes characters eagerly, but does not require random access iterator.
114 template<class B>
115 static bool eagerMatch(B& in, const char* str) {
116  for (; *str != '\0'; ++str, ++in)
117  if (*str != *in)
118  return false;
119  return true; }
120 
121 
122 //=================================================================================================
123 } // namespace Internal
124 } // namespace Minisat
125 #pragma GCC visibility pop
Minisat::Internal::skipLine
static void skipLine(B &in)
Definition: ParseUtils.h:78
Minisat::Internal::skipWhitespace
static void skipWhitespace(B &in)
Definition: ParseUtils.h:72
Minisat::Internal::eagerMatch
static bool eagerMatch(B &in, const char *str)
Definition: ParseUtils.h:115
Minisat
Definition: Minisat.h:55
Minisat::Internal::match
static bool match(B &in, const char *str)
Definition: ParseUtils.h:102
Minisat::Internal::buffer_size
static const int buffer_size
Definition: ParseUtils.h:35
Minisat::Internal::parseInt
static int parseInt(B &in)
Definition: ParseUtils.h:86