Skip to content
Snippets Groups Projects
Commit aea81c6d authored by Vasileios Karakasis's avatar Vasileios Karakasis
Browse files

Better and cleaner parser semantics.

The parser's semantics are now cleaner and the implementation more
robust. Cell parsing now follows the semantics of the standard input
stream (same as of standard UNIX's read), but instead of reading in
bytes or builtin types, we are reading full cell records. This has the
following implications:

1. Empty and comment lines are consumed silently.
2. End-of-file will be set on the stream only if a read operation
   returns zero. Practically, this means, eof bit will be set in the
   stream at the first attempt to read a cell record after the final
   cell record in the stream was read.
3. Exceptional conditions in the stream (e.g., eof or bad bits) stop the
   parsing process without raising an exception and return control to the
   user which is responsible to deal with them. The only exceptional
   condition that we handle internally is the presence of very long lines,
   in which case we raise an std::runtime_error().

Thanks to these cleaner semantics, the parser is now able to deal with
files not ending with a new line. The previous version would have missed
the last record.

Due to (2) the following code for reading from an input stream will
yield an additional default constructed cell record at the end:

while (!swc_input.eof()) {
    cell_record cell;
    swc_input >> cell;
    // do sth with the cell
}

According to (2), after the final record is read, eof is not yet set at
the stream, which means the while loop will be entered one more time.
The attempt to read a record from the stream then will raise the eof bit
and no cell will be read in, leaving `cell' at its default constructed
state. To avoid such a side-effect, the best way to read records from an
input stream is the following:

cell_record cell;
while( !(swc_input >> cell).eof()) {
    // do sth with the cell
}

In this case, after the final record is read, the next attempt to read
in a record will set the eof bit and won't enter the loop.
parent 7284fba8
No related branches found
No related tags found
No related merge requests found
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment