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