Geogram  Version 1.8.9-rc
A programming library of geometric algorithms
GEO::LineInput Class Reference

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...
 

Detailed Description

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:

try {
LineInput in(filename);
while( !in.eof() && in.get_line() ) {
in.get_fields();
double d = in.field_as_double(2);
}
}
catch(const std::logic_error& ex) {
std::cerr << "Got an error: " << ex.what << std::endl;
}
LineInput(const std::string &filename)
Creates a new line reader from a file.

Definition at line 82 of file line_stream.h.

Constructor & Destructor Documentation

◆ LineInput()

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;

Parameters
[in]filenamethe name of the file to read

◆ ~LineInput()

GEO::LineInput::~LineInput ( )

Destroys the line reader.

This closes the current input file.

Member Function Documentation

◆ current_line()

const char* GEO::LineInput::current_line ( ) const
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.

Returns
a const pointer to the internal buffer that stores the current line

Definition at line 259 of file line_stream.h.

◆ eof()

bool GEO::LineInput::eof ( ) const
inline

Checks if line reader has reached the end of the input stream.

Return values
trueif the stream is at end
falseotherwise

Definition at line 111 of file line_stream.h.

◆ field() [1/2]

char* GEO::LineInput::field ( index_t  i)
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.

Parameters
[in]ithe index of the field
Returns
the modifiable pointer to field string at index i

Definition at line 151 of file line_stream.h.

◆ field() [2/2]

const char* GEO::LineInput::field ( index_t  i) const
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.

Parameters
[in]ithe index of the field
Returns
the const pointer to field string at index i

Definition at line 164 of file line_stream.h.

◆ field_as_double()

double GEO::LineInput::field_as_double ( index_t  i) const
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.

Parameters
[in]ithe index of the field
Returns
the floating point value of the field at index i
Exceptions
std::logic_errorif the field cannot be converted to a floating point value

Definition at line 218 of file line_stream.h.

◆ field_as_int()

signed_index_t GEO::LineInput::field_as_int ( index_t  i) const
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.

Parameters
[in]ithe index of the field
Returns
the integer value of the field at index i
Exceptions
std::logic_errorif the field cannot be converted to an integer value

Definition at line 180 of file line_stream.h.

◆ field_as_uint()

index_t GEO::LineInput::field_as_uint ( index_t  i) const
inline

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.

Parameters
[in]ithe index of the field
Returns
the unsigned integer value of the field at index i
Exceptions
std::logic_errorif the field cannot be converted to an unsigned integer value

Definition at line 199 of file line_stream.h.

◆ field_matches()

bool GEO::LineInput::field_matches ( index_t  i,
const char *  s 
) const
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.

Parameters
[in]ithe index of the field
[in]sthe string to compare the field to
Return values
trueif field at index i equals string s
falseotherwise

Definition at line 237 of file line_stream.h.

◆ get_fields()

void GEO::LineInput::get_fields ( const char *  separators = " \t\r\n")

Splits the current line into fields.

The function uses separators to split the current line into individual fields that can be accessed by field() and its typed variants.

Parameters
[in]separatorsa string that contains all the characters considered as separators.
See also
field()

◆ get_line()

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.

Return values
trueif a line could be read
falseotherwise.

◆ line_number()

size_t GEO::LineInput::line_number ( ) const
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.

◆ nb_fields()

index_t GEO::LineInput::nb_fields ( ) const
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.

Returns
the number of fields in the current line

Definition at line 131 of file line_stream.h.


The documentation for this class was generated from the following file: