changing delimiters for std::stream

Discussion in 'C++' started by shea martin, Aug 30, 2004.

  1. shea martin

    shea martin Guest

    Is there a way to change the delimiters used by >> in an istream? I am
    parsing a markup file, and want <tag>word</tag> to be broken into 3
    strings. I would prefer not to use a 3rd party lib, though I am sure
    there are lots out there. I am hoping that the std::streams can provide
    this functionality.

    ~S
     
    shea martin, Aug 30, 2004
    #1
    1. Advertising

  2. shea martin

    John Ericson Guest

    "shea martin" <> wrote in message
    news:AVtYc.264228$gE.238334@pd7tw3no...
    > Is there a way to change the delimiters used by >> in an

    istream? I am
    > parsing a markup file, and want <tag>word</tag> to be

    broken into 3
    > strings. I would prefer not to use a 3rd party lib,

    though I am sure
    > there are lots out there. I am hoping that the

    std::streams can provide
    > this functionality.
    >
    > ~S


    I suppose you could write a ctype facet that defines '<' and
    '>' (and '/'?) as whitespace. I'd go with a 3rd party lib,
    myself.

    Best regards, JE
     
    John Ericson, Aug 30, 2004
    #2
    1. Advertising

  3. shea martin

    Mike Wahler Guest

    "shea martin" <> wrote in message
    news:AVtYc.264228$gE.238334@pd7tw3no...
    > Is there a way to change the delimiters used by >> in an istream? I am
    > parsing a markup file, and want <tag>word</tag> to be broken into 3
    > strings. I would prefer not to use a 3rd party lib, though I am sure
    > there are lots out there. I am hoping that the std::streams can provide
    > this functionality.


    Read about 'facets'. But it does take some effort to get
    things right. If it were me, I'd use an already written
    library for this.

    -Mike
     
    Mike Wahler, Aug 30, 2004
    #3
  4. shea martin

    Jerry Coffin Guest

    shea martin <> wrote in message news:<AVtYc.264228$gE.238334@pd7tw3no>...
    > Is there a way to change the delimiters used by >> in an istream? I am
    > parsing a markup file, and want <tag>word</tag> to be broken into 3
    > strings. I would prefer not to use a 3rd party lib, though I am sure
    > there are lots out there. I am hoping that the std::streams can provide
    > this functionality.


    I haven't tested it, but this should at least be sort of close. For
    the moment it assumes that you want all the usual white space to
    remain as white space, and just add '<', '>' and '/' to the usual.

    class my_ctype : public
    std::ctype<char>
    {
    mask my_table[UCHAR_MAX];
    public:
    my_ctype(size_t refs = 0)
    : std::ctype<char>(my_table, false, refs)
    {
    std::copy(classic_table(), classic_table() + table_size,
    my_table);
    my_table[widen('<')] = (mask)space;
    my_table[widen('>')] = (mask)space;
    my_table[widen('/')] = (mask)space;
    }
    };

    Theoretically, you want to use table_size instead of UCHAR_MAX for the
    size of the table, but on at least a few standard library
    implementations that won't work, so I've cheated and used UCHAR_MAX
    instead.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
     
    Jerry Coffin, Aug 30, 2004
    #4
  5. On Sun, 29 Aug 2004 23:46:40 GMT, shea martin <>
    wrote:

    >Is there a way to change the delimiters used by >> in an istream? I am
    >parsing a markup file, and want <tag>word</tag> to be broken into 3
    >strings. I would prefer not to use a 3rd party lib, though I am sure
    >there are lots out there. I am hoping that the std::streams can provide
    >this functionality.


    Don't reinvent the wheel. Check out the Boost "tokenizer" library:

    http://www.boost.org/libs/tokenizer/index.html

    The Boost libraries will largely be included in the next C++ STL, so
    whenever this happens, they won't be "third party" anymore.

    --
    Bob Hairgrove
     
    Bob Hairgrove, Aug 30, 2004
    #5
    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.

Share This Page