 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Rafal 'Raf256' Maj Guest
|
Posted: Mon Dec 20, 2004 12:31 pm Post subject: read/write stream - parsing |
|
|
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.
--
~~~~=~~~~l_;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
_|___J ____, Pozdrawiam, moje www, C++, kontakt, itd.:
X-( ssn256 ) Rafal Maj Raf256 - http://www.raf256.com/me-news/
,"-------------" (strona w budowie)
|
|
| Back to top |
|
 |
Rafal 'Raf256' Maj Guest
|
Posted: Mon Dec 20, 2004 12:35 pm Post subject: Re: read/write stream - parsing |
|
|
[email]spam (AT) raf256 (DOT) com[/email] news:Xns95C589D0285F8raf256com (AT) 213 (DOT) 180.128.20
| Quote: | 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?
--
~~~~=~~~~l_;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
_|___J ____, Pozdrawiam, moje www, C++, kontakt, itd.:
X-( ssn256 ) Rafal Maj Raf256 - http://www.raf256.com/me-news/
,"-------------" (strona w budowie)
|
|
| Back to top |
|
 |
Karl Heinz Buchegger Guest
|
Posted: Mon Dec 20, 2004 12:42 pm Post subject: Re: read/write stream - parsing |
|
|
Rafal 'Raf256' Maj wrote:
| Quote: |
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
| Quote: | , 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.
--
Karl Heinz Buchegger
[email]kbuchegg (AT) gascad (DOT) at[/email]
|
|
| Back to top |
|
 |
Rafal 'Raf256' Maj Guest
|
Posted: Mon Dec 20, 2004 12:44 pm Post subject: Re: read/write stream - parsing |
|
|
[email]kbuchegg (AT) gascad (DOT) at[/email] news:41C6C8AD.BC467AF3 (AT) gascad (DOT) at
| Quote: | 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?)
--
~~~~=~~~~l_;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
_|___J ____, Pozdrawiam, moje www, C++, kontakt, itd.:
X-( ssn256 ) Rafal Maj Raf256 - http://www.raf256.com/me-news/
,"-------------" (strona w budowie)
|
|
| Back to top |
|
 |
Karl Heinz Buchegger Guest
|
Posted: Mon Dec 20, 2004 12:52 pm Post subject: Re: read/write stream - parsing |
|
|
Rafal 'Raf256' Maj wrote:
| Quote: |
[email]kbuchegg (AT) gascad (DOT) at[/email] news:41C6C8AD.BC467AF3 (AT) gascad (DOT) at
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?)
|
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.
--
Karl Heinz Buchegger
[email]kbuchegg (AT) gascad (DOT) at[/email]
|
|
| Back to top |
|
 |
Jeff Flinn Guest
|
Posted: Mon Dec 20, 2004 1:35 pm Post subject: Re: read/write stream - parsing |
|
|
Rafal 'Raf256' Maj wrote:
| Quote: | Hi,
I need to parse a file.
|
....
| Quote: | 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
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|