Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

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#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)
29namespace Minisat {
30namespace Internal {
31
32//-------------------------------------------------------------------------------------------------
33// A simple buffered character stream class:
34
35static const int buffer_size = 1048576;
36
37/*
38class 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
49public:
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
62static inline bool isEof(StreamBuffer& in) { return *in == EOF; }
63static 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
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
84
85template<class B>
86static int parseInt(B& in) {
87 int val = 0;
88 bool neg = false;
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.
101template<class B>
102static 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.
114template<class B>
115static 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
static const int buffer_size
Definition ParseUtils.h:35
static bool eagerMatch(B &in, const char *str)
Definition ParseUtils.h:115
static bool match(B &in, const char *str)
Definition ParseUtils.h:102
static int parseInt(B &in)
Definition ParseUtils.h:86
static void skipLine(B &in)
Definition ParseUtils.h:78
static void skipWhitespace(B &in)
Definition ParseUtils.h:72