LOOS  v2.3.2
LineReader.hpp
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 
23 #if !defined(LOOS_LINE_READER_HPP)
24 #define LOOS_LINE_READER_HPP
25 
26 
27 #include <iostream>
28 #include <string>
29 #include <stdexcept>
30 #include <list>
31 #include <loos_defs.hpp>
32 
33 
34 namespace loos {
35 
36 
38 
39  class LineReader {
40  public:
41  LineReader()
42  : _is(0), _lineno(1), _comment_char('#'), _leading_chars(" \t")
43  { }
44 
45  LineReader(std::istream& is)
46  : _is(&is), _lineno(1), _comment_char('#'), _leading_chars(" \t")
47  { }
48 
49  LineReader(std::istream& is, const std::string& name)
50  : _is(&is), _lineno(1), _name(name), _comment_char('#'), _leading_chars(" \t")
51  { }
52 
53  virtual ~LineReader() { }
54 
56 
59  void setCommentCharacter(const char c) { _comment_char = c; }
60 
62 
66  void setLeadingCharacters(const std::string& s) { _leading_chars = s; }
67 
69  virtual std::istream& stream() const;
70 
72  virtual void stream(std::istream& is);
73 
75  virtual std::string name() const;
76 
78  virtual void name(const std::string& name);
79 
81  virtual bool getNext();
82 
84  virtual void push_back(const std::string& s);
85 
87  virtual std::string line() const;
88 
90  virtual uint lineNumber() const;
91 
92  virtual void resetLineNumber() {
93  _lineno = 1;
94  }
95 
96  protected:
97 
98 
99  /*
100  The following member functions are the ones that will most
101  likely need to be changed in derived classes to alter the
102  functionality of the reader...
103  */
104 
105  // Verifies the state of the stream (throws if error)
106  virtual void checkState() const;
107 
108  // Remove comments from the string
109  virtual void stripComment(std::string& s) const;
110 
111  // Remove leading whitespace (space and tabs)
112  virtual void stripLeadingWhitespace(std::string& s) const;
113 
114  // True means to skip the current line (e.g. the line is empty)
115  virtual bool skipLine(const std::string& s) const;
116 
117 
118  protected:
119  std::istream* _is;
120  unsigned int _lineno;
121  std::string _name;
122  char _comment_char;
123  std::string _leading_chars;
124  std::string _current_line;
125  std::list<std::string> _lines;
126 
127  };
128 }
129 
130 
131 
132 
133 #endif
virtual void push_back(const std::string &s)
Put a line back onto the file (virtually)
Definition: LineReader.cpp:58
void setCommentCharacter(const char c)
Set the character to use for comments.
Definition: LineReader.hpp:59
virtual std::string line() const
The currently read line.
Definition: LineReader.cpp:62
void setLeadingCharacters(const std::string &s)
Set the characters to strip from the start of each line.
Definition: LineReader.hpp:66
virtual std::string name() const
Access the name associated with the internal stream.
Definition: LineReader.cpp:33
Class for reading line-by-line from a file while tracking line numbers and stripping comments...
Definition: LineReader.hpp:39
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
virtual uint lineNumber() const
The current line number into the file.
Definition: LineReader.cpp:64