read/write stream - parsing

R

Rafal 'Raf256' Maj

Hi,
I need to parse a file. This means reading from it as from std::istream.
But - sometimes I also need to put-back some text I read before.
What type of string can I use for that? Something like:


void cLine::Load(std::istream &data) {
data.ignore(4); // ignore word "line"
char c; // ignore { and } chars
data >> c >> x1 >> y1 >> x2 >> y2 >> c;
}


void Parse(std::istream &data) { // <------
cStr token;
data >> token;
if (token == "line") line.Load(data);
else if (token == "rect") ....
else ParseError();
}

Syntax of file is for example:
line{1 2 3 4}

Problem is that main Parser must read "line" to know what object to
construct and load, and later load of this object also needs to read same
"line" because this is the syntax.
 
R

Rafal 'Raf256' Maj

(e-mail address removed)
Syntax of file is for example:
line{1 2 3 4}
Problem is that main Parser must read "line" to know what object to
construct and load, and later load of this object also needs to read
same "line" because this is the syntax.

Hmm.. something like unget() but for entire words...

or perhaps it would be a good idea to use

seekg( -token.size() , ios_base::cur);

? it is compatible when data is in fact an ifstream?
 
K

Karl Heinz Buchegger

Rafal said:
Hi,
I need to parse a file. This means reading from it as from std::istream.
But - sometimes I also need to put-back some text I read before.
What type of string can I use for that? Something like:

void cLine::Load(std::istream &data) {
data.ignore(4); // ignore word "line"
char c; // ignore { and } chars
data >> c >> x1 >> y1 >> x2 >> y2 >> c;
}

void Parse(std::istream &data) { // <------
cStr token;
data >> token;
if (token == "line") line.Load(data);
else if (token == "rect") ....
else ParseError();
}

Syntax of file is for example:
line{1 2 3 4}

Problem is that main Parser must read "line" to know what object to
construct and load
yes

, and later load of this object also needs to read same
"line" because this is the syntax.

No. Why should it expect to parse "line" from the input. That
part of the input has already been parsed and was used to make
the decission which object to create. So when the line parsing
function is called, simply continue with parsing the "{ 1 2 3 4 }"
input.

There is no need to put anything back into the stream. Don't
make the whole thing more complicated then it needs to be.
 
R

Rafal 'Raf256' Maj

(e-mail address removed)
No. Why should it expect to parse "line" from the input. That
part of the input has already been parsed and was used to make
the decission which object to create. So when the line parsing
function is called, simply continue with parsing the "{ 1 2 3 4 }"
input.

There is no need to put anything back into the stream. Don't
make the whole thing more complicated then it needs to be.

Hmm.. Yes, I think that YOu are right, I will use this implementation of
parsing.

Just for curiosity, the seekg thingy will work? (assuming that it works on
stringstram, on on ifstream and the file didnt change in between?)
 
K

Karl Heinz Buchegger

Rafal said:
(e-mail address removed)

Hmm.. Yes, I think that YOu are right, I will use this implementation of
parsing.

Just for curiosity, the seekg thingy will work? (assuming that it works on
stringstram, on on ifstream and the file didnt change in between?)

Sure it would work.
But what for? You are seeking back in the stream just to read something
from the stream you already know: "line". No need to do that.
 
J

Jeff Flinn

Rafal said:
Hi,
I need to parse a file.
....

Syntax of file is for example:
line{1 2 3 4}

Problem is that main Parser must read "line" to know what object to
construct and load, and later load of this object also needs to read
same "line" because this is the syntax.

Do yourself a favor and use a parsing framework. See
http://www.boost.org/libs/spirit/index.html for an embedded parser
framework.

Jeff Flinn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top