By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,305 Members | 1,588 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,305 IT Pros & Developers. It's quick & easy.

changing delimiters for std::stream

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"shea martin" <sm*****@arcis.com> 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
Jul 22 '05 #2

P: n/a
"shea martin" <sm*****@arcis.com> 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
Jul 22 '05 #3

P: n/a
shea martin <sm*****@arcis.com> 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.
Jul 22 '05 #4

P: n/a
On Sun, 29 Aug 2004 23:46:40 GMT, shea martin <sm*****@arcis.com>
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
No**********@Home.com
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.