read/write stream - parsing

Discussion in 'C++' started by Rafal 'Raf256' Maj, Dec 20, 2004.

  1. 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)
     
    Rafal 'Raf256' Maj, Dec 20, 2004
    #1
    1. Advertising

  2. news:Xns95C589D0285F8raf256com@213.180.128.20

    > 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)
     
    Rafal 'Raf256' Maj, Dec 20, 2004
    #2
    1. Advertising

  3. Rafal 'Raf256' Maj wrote:
    >
    > 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.

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Dec 20, 2004
    #3
  4. news:

    > 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)
     
    Rafal 'Raf256' Maj, Dec 20, 2004
    #4
  5. Rafal 'Raf256' Maj wrote:
    >
    > news:
    >
    > > 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
     
    Karl Heinz Buchegger, Dec 20, 2004
    #5
  6. Rafal 'Raf256' Maj

    Jeff Flinn Guest

    Rafal 'Raf256' Maj wrote:
    > 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
     
    Jeff Flinn, Dec 20, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rasmusson, Lars
    Replies:
    1
    Views:
    772
    popov
    Apr 30, 2004
  2. Deep
    Replies:
    6
    Views:
    505
    Nick Keighley
    Feb 28, 2007
  3. Replies:
    9
    Views:
    653
    Alex Buell
    Apr 27, 2006
  4. vishnu
    Replies:
    0
    Views:
    2,929
    vishnu
    Dec 14, 2007
  5. Larry
    Replies:
    1
    Views:
    165
    Mark Clements
    Mar 14, 2006
Loading...

Share This Page