File: datafile.cc

package info (click to toggle)
music 1.2.1-0.7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,596 kB
  • sloc: cpp: 26,805; sh: 5,674; python: 510; makefile: 406; ansic: 224
file content (99 lines) | stat: -rw-r--r-- 1,881 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/* This file is part of the skol suite.
   Copyright (C) 2005, 2008 Mikael Djurfeldt

   The skol suite is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 3, or (at
   your option) any later version.

   The skol suite is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with the skol suite; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.  */


#include <iostream>
#include <limits>
#include <cctype>

#include "datafile.h"

using std::numeric_limits;

bool
Datafile::atHeaderLine ()
{
  return peek () == '#';
}

void
Datafile::ignoreLine ()
{
  ignore (numeric_limits<int>::max (), '\n');
}

void
Datafile::ignoreWhitespace ()
{
  while (isspace (peek ()))
    ignore ();
}

bool
Datafile::scanLine (const char* pattern)
{
  const char* p = pattern;
  while (*p != '\0')
    {
      if (peek () == '\n')
	{
	  ignore ();
	  return false;
	}
      if (peek () == *p)
	++p;
      else
	p = pattern;
      ignore ();
    }
  return true;
}

bool
Datafile::read (const char* pattern, int& x)
{
  seekg (0);
  while (atHeaderLine ())
    if (scanLine (pattern))
      {
	*this >> x;
	return true;
      }
  return false;
}

bool
Datafile::read (const char* pattern, double& x)
{
  seekg (0);
  while (atHeaderLine ())
    if (scanLine (pattern))
      {
	*this >> x;
	return true;
      }
  return false;
}

void
Datafile::skipHeader ()
{
  ignoreWhitespace ();
  while (atHeaderLine ())
    ignoreLine ();
}