Eclipse SUMO - Simulation of Urban MObility
LineReader.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 // Copyright (C) 2001-2024 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
19 // Retrieves a file linewise and reports the lines to a handler.
20 /****************************************************************************/
21 #pragma once
22 #include <config.h>
23 
24 #include <string>
25 #include <fstream>
27 
28 
29 // ===========================================================================
30 // class declarations
31 // ===========================================================================
32 class LineHandler;
33 
34 
35 // ===========================================================================
36 // class definitions
37 // ===========================================================================
48 class LineReader {
49 public:
51  LineReader();
52 
53 
61  LineReader(const std::string& file);
62 
63 
65  ~LineReader();
66 
67 
71  bool hasMore() const;
72 
73 
80  void readAll(LineHandler& lh);
81 
82 
90  bool readLine(LineHandler& lh);
91 
92 
97  std::string readLine();
98 
99 
101  void close();
102 
103 
107  std::string getFileName() const;
108 
109 
117  bool setFile(const std::string& file);
118 
119 
123  unsigned long getPosition();
124 
125 
127  void reinit();
128 
129 
134  void setPos(unsigned long pos);
135 
136 
140  bool good() const;
141 
142 
144  return myLinesRead;
145  }
146 
147 private:
149  std::string myFileName;
150 
152  std::ifstream myStrm;
153 
155  char myBuffer[1024];
156 
158  std::string myStrBuffer;
159 
161  int myRead;
162 
165 
167  int myRread;
168 
171 
174 };
Interface definition for a class which retrieves lines from a LineHandler.
Definition: LineHandler.h:42
Retrieves a file linewise and reports the lines to a handler.
Definition: LineReader.h:48
void setPos(unsigned long pos)
Sets the current position within the file to the given value.
Definition: LineReader.cpp:223
void close()
Closes the reading.
~LineReader()
Destructor.
Definition: LineReader.cpp:48
unsigned long getPosition()
Returns the current position within the file.
Definition: LineReader.cpp:188
LineReader()
Constructor.
Definition: LineReader.cpp:38
int mySkipBOM
Number of skipped characters at the file begin (UTF-8 BOM)
Definition: LineReader.h:173
bool good() const
Returns the information whether the stream is readable.
Definition: LineReader.cpp:232
int myRead
Information about how many characters were supplied to the LineHandler.
Definition: LineReader.h:161
int myRread
Information how many bytes were read by the reader from the file.
Definition: LineReader.h:167
int myAvailable
Information how many bytes are available within the used file.
Definition: LineReader.h:164
std::string readLine()
Reads a single (the next) line from the file and returns it.
Definition: LineReader.cpp:120
bool setFile(const std::string &file)
Reinitialises the reader for reading from the given file.
Definition: LineReader.cpp:180
int getLineNumber()
Definition: LineReader.h:143
void reinit()
Reinitialises the reading (of the previous file)
Definition: LineReader.cpp:194
int myLinesRead
Information how many lines were read for meaningful error messages.
Definition: LineReader.h:170
bool hasMore() const
Returns whether another line may be read (the file was not read completely)
Definition: LineReader.cpp:52
std::string getFileName() const
Returns the name of the used file.
Definition: LineReader.cpp:174
std::ifstream myStrm
the stream used
Definition: LineReader.h:152
std::string myFileName
the name of the file to read the contents from
Definition: LineReader.h:149
char myBuffer[1024]
To override MSVC++-bugs, we use an own getline which uses this buffer.
Definition: LineReader.h:155
std::string myStrBuffer
a string-buffer
Definition: LineReader.h:158
void readAll(LineHandler &lh)
Reads the whole file linewise, reporting every line to the given LineHandler.
Definition: LineReader.cpp:58