Geogram
Version 1.9.1
A programming library of geometric algorithms
|
Reads an ASCII file line per line. More...
#include <geogram/basic/line_stream.h>
Public Member Functions | |
LineInput (const std::string &filename) | |
Creates a new line reader from a file. More... | |
~LineInput () | |
Destroys the line reader. More... | |
bool | OK () const |
Checks if the line reader is ready to read. | |
bool | eof () const |
Checks if line reader has reached the end of the input stream. More... | |
bool | get_line () |
Reads a new line. More... | |
index_t | nb_fields () const |
Gets the number of fields in the current line. More... | |
size_t | line_number () const |
Returns the current line number. More... | |
char * | field (index_t i) |
Gets a line field as a modifiable string. More... | |
const char * | field (index_t i) const |
Gets a line field as a non-modifiable string. More... | |
signed_index_t | field_as_int (index_t i) const |
Gets a line field as an integer. More... | |
index_t | field_as_uint (index_t i) const |
Gets a line field as an unsigned integer. More... | |
double | field_as_double (index_t i) const |
Gets a line field as a double. More... | |
bool | field_matches (index_t i, const char *s) const |
Compares a field with a string. More... | |
void | get_fields (const char *separators=" \t\r\n") |
Splits the current line into fields. More... | |
const char * | current_line () const |
Gets the current line. More... | |
Reads an ASCII file line per line.
LineInput reads an ASCII file line by line and splits the line into a list of white space separated fields that can be accessed individually or converted to numeric values.
Functions field_as_int() and field_as_double() throw exceptions when they cannot convert a field to a integer or floating point value, so it is safe to wrap the LineNumber usage in a try / catch block as follows:
Definition at line 82 of file line_stream.h.
GEO::LineInput::LineInput | ( | const std::string & | filename | ) |
Creates a new line reader from a file.
This open the file filename
for reading and prepares to read it line by line. If the file could not be opened, OK() will return false;
[in] | filename | the name of the file to read |
GEO::LineInput::~LineInput | ( | ) |
Destroys the line reader.
This closes the current input file.
|
inline |
Gets the current line.
If get_fields() was called, then an end-of-string marker '\0' is present at the end of the first field.
Definition at line 259 of file line_stream.h.
|
inline |
Checks if line reader has reached the end of the input stream.
true | if the stream is at end |
false | otherwise |
Definition at line 111 of file line_stream.h.
|
inline |
Gets a line field as a modifiable string.
The function returns the field at index i
. Function get_fields() must be called once after get_line() before calling this function, otherwise the result is undefined.
[in] | i | the index of the field |
i
Definition at line 151 of file line_stream.h.
|
inline |
Gets a line field as a non-modifiable string.
The function returns the field at index i
. Function get_fields() must be called once after get_line() before calling this function, otherwise the result is undefined.
[in] | i | the index of the field |
i
Definition at line 164 of file line_stream.h.
|
inline |
Gets a line field as a double.
The function returns the field at index i
converted to a double. Function get_fields() must be called once after get_line() before calling this function, otherwise the result is undefined.
[in] | i | the index of the field |
i
std::logic_error | if the field cannot be converted to a floating point value |
Definition at line 218 of file line_stream.h.
|
inline |
Gets a line field as an integer.
The function returns the field at index i
converted to an integer. Function get_fields() must be called once after get_line() before calling this function, otherwise the result is undefined.
[in] | i | the index of the field |
i
std::logic_error | if the field cannot be converted to an integer value |
Definition at line 180 of file line_stream.h.
Gets a line field as an unsigned integer.
The function returns the field at index i
converted to an unsigned integer. Function get_fields() must be called once after get_line() before calling this function, otherwise the result is undefined.
[in] | i | the index of the field |
i
std::logic_error | if the field cannot be converted to an unsigned integer value |
Definition at line 199 of file line_stream.h.
|
inline |
Compares a field with a string.
The function compares the field at index i
with string s
and returns true
if they are equal. Function get_fields() must be called once after get_line() before calling this function, otherwise the result is undefined.
[in] | i | the index of the field |
[in] | s | the string to compare the field to |
true | if field at index i equals string s |
false | otherwise |
Definition at line 237 of file line_stream.h.
void GEO::LineInput::get_fields | ( | const char * | separators = " \t\r\n" | ) |
bool GEO::LineInput::get_line | ( | ) |
Reads a new line.
Reads a new line from the input stream. Function get_fields() must be called if you need to access to individual fields in the line with field() and its typed variants.
true | if a line could be read |
false | otherwise. |
|
inline |
Returns the current line number.
If no line has been read so far, line_number() returns 0.
Definition at line 139 of file line_stream.h.
|
inline |
Gets the number of fields in the current line.
Function get_fields() must be called once after get_line() before calling this function, otherwise the result is undefined.
Definition at line 131 of file line_stream.h.