Debian Codegraph @

utils/datafile.cc

raw
1 /* This file is part of the skol suite.
2 Copyright (C) 2005, 2008 Mikael Djurfeldt
3
4 The skol suite is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 3, or (at
7 your option) any later version.
8
9 The skol suite is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with the skol suite; see the file COPYING. If not, write to
16 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
18
19
20 #include <iostream>
21 #include <limits>
22 #include <cctype>
23
24 #include "datafile.h"
25
26 using std::numeric_limits;
27
28 bool
29 Datafile::atHeaderLine ()
30 {
31 return peek () == '#';
32 }
33
34 void
35 Datafile::ignoreLine ()
36 {
37 ignore (numeric_limits<int>::max (), '\n');
38 }
39
40 void
41 Datafile::ignoreWhitespace ()
42 {
43 while (isspace (peek ()))
44 ignore ();
45 }
46
47 bool
48 Datafile::scanLine (const char* pattern)
49 {
50 const char* p = pattern;
51 while (*p != '\0')
52 {
53 if (peek () == '\n')
54 {
55 ignore ();
56 return false;
57 }
58 if (peek () == *p)
59 ++p;
60 else
61 p = pattern;
62 ignore ();
63 }
64 return true;
65 }
66
67 bool
68 Datafile::read (const char* pattern, int& x)
69 {
70 seekg (0);
71 while (atHeaderLine ())
72 if (scanLine (pattern))
73 {
74 *this >> x;
75 return true;
76 }
77 return false;
78 }
79
80 bool
81 Datafile::read (const char* pattern, double& x)
82 {
83 seekg (0);
84 while (atHeaderLine ())
85 if (scanLine (pattern))
86 {
87 *this >> x;
88 return true;
89 }
90 return false;
91 }
92
93 void
94 Datafile::skipHeader ()
95 {
96 ignoreWhitespace ();
97 while (atHeaderLine ())
98 ignoreLine ();
99 }