LOOS  v2.3.2
LineReader.cpp
1 /*
2  This file is part of LOOS.
3 
4  LOOS (Lightweight Object-Oriented Structure library)
5  Copyright (c) 2012, Tod D. Romo, Alan Grossfield
6  Department of Biochemistry and Biophysics
7  School of Medicine & Dentistry, University of Rochester
8 
9  This package (LOOS) is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation under version 3 of the License.
12 
13  This package is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 #include <iostream>
23 #include <fstream>
24 
25 #include <exceptions.hpp>
26 #include "LineReader.hpp"
27 
28 namespace loos {
29 
30  std::istream& LineReader::stream() const { return(*_is); }
31  void LineReader::stream(std::istream& is) { _is = &is; _lineno = 1; }
32 
33  std::string LineReader::name() const { return(_name); }
34  void LineReader::name(const std::string& name) { _name = name; }
35 
36 
37 
39  if (! _lines.empty()) {
40  _current_line = _lines.back();
41  _lines.pop_back();
42  } else if (_is->eof())
43  return(false);
44  else {
45  while (getline(*_is, _current_line)) {
46  ++_lineno;
47  stripComment(_current_line);
48  stripLeadingWhitespace(_current_line);
49  if (! skipLine(_current_line) )
50  break;
51  }
52  }
53 
54  checkState();
55  return( _is->good() );
56  }
57 
58  void LineReader::push_back(const std::string& s) {
59  _lines.push_back(s);
60  }
61 
62  std::string LineReader::line() const { return(_current_line); }
63 
64  unsigned int LineReader::lineNumber() const { return(_lineno); }
65 
66  void LineReader::checkState() const {
67  if (!(_is->good() || _is->eof())) {
68 
69  if (! _name.empty())
70  throw(FileReadErrorWithLine(_name, _lineno));
71  else
72  throw(FileReadErrorWithLine(_lineno));
73  }
74  }
75 
76  void LineReader::stripComment(std::string& s) const {
77  if (_comment_char != '\0') {
78  std::string::size_type i = s.find('#');
79  if (i != std::string::npos)
80  s.erase(i, s.length() - i);
81  }
82  }
83 
84  void LineReader::stripLeadingWhitespace(std::string& s) const {
85  if (! _leading_chars.empty()) {
86  std::string::size_type i = s.find_first_not_of(" \t");
87  if (i > 0)
88  s.erase(0, i);
89  }
90  }
91 
92  bool LineReader::skipLine(const std::string& s) const {
93  return(s.empty());
94  }
95 
96 
97 }
virtual void push_back(const std::string &s)
Put a line back onto the file (virtually)
Definition: LineReader.cpp:58
virtual std::string line() const
The currently read line.
Definition: LineReader.cpp:62
virtual std::string name() const
Access the name associated with the internal stream.
Definition: LineReader.cpp:33
Namespace for most things not already encapsulated within a class.
virtual std::istream & stream() const
Access the internal stream pointer.
Definition: LineReader.cpp:30
virtual bool getNext()
Get the next line from the file, returning true if successful.
Definition: LineReader.cpp:38
Errors that occur while reading a text file (where lines are tracked)
Definition: exceptions.hpp:190
virtual uint lineNumber() const
The current line number into the file.
Definition: LineReader.cpp:64