Spaces & Delimiters in a streams

Y

Y. H.

Hi,

I'm having the following problem. I'm trying to write a simple parser
for a script that looks like this:

<command> [<arg-1>], [<arg-2>], [<arg-3>], ...

Giving an argument is optional. If it's not present (meaning there are 2
commans in a row) we take a default value.
Naturally, I'm using an istream to parse each line.

My question is this: is there a way to make a seperation between spaces
and delimiters? The thing is,
that because I have commas between each argument, I want the comma to be
the delimiter, but I still
want to ignore the spaces.

The only two ways I know is either to use getline with ',' as a
delimiter - but that wouldn't ignore spaces.
The other is to write your own facet class which sets ',' as a space. -
but then after reading the first comma,
if there's another comma straight after it - the stream will read on -
taking it as a white space, instead of treating
it as a delimiter and stop there.

Any ideas?

Thanks,
Yaron
 
M

Mike Wahler

Y. H. said:
Hi,

I'm having the following problem. I'm trying to write a simple parser
for a script that looks like this:

<command> [<arg-1>], [<arg-2>], [<arg-3>], ...

Giving an argument is optional. If it's not present (meaning there are 2
commans in a row) we take a default value.
Naturally, I'm using an istream to parse each line.

My question is this: is there a way to make a seperation between spaces
and delimiters?

That depends upon what you're using for delimiters.
The thing is,
that because I have commas between each argument, I want the comma to be
the delimiter, but I still
want to ignore the spaces.

The only two ways I know is either to use getline with ',' as a
delimiter - but that wouldn't ignore spaces.
The other is to write your own facet class which sets ',' as a space. -
but then after reading the first comma,
if there's another comma straight after it - the stream will read on -
taking it as a white space, instead of treating
it as a delimiter and stop there.

Any ideas?

std::string tok;
std::getline(stream, tok, ',');
std::istringstream iss(tok);
std::string result; /* or whatever type you need */
iss >> result;

This assumes that each 'arg' between the commas has
no embedded whitespace.

And Of course you'll want to add error checking.


-Mike
 
J

Jeff Flinn

Y. H. said:
Hi,

I'm having the following problem. I'm trying to write a simple parser
for a script that looks like this:

<command> [<arg-1>], [<arg-2>], [<arg-3>], ...

Giving an argument is optional. If it's not present (meaning there are 2
commans in a row) we take a default value.
Naturally, I'm using an istream to parse each line.

My question is this: is there a way to make a seperation between spaces
and delimiters? The thing is,
that because I have commas between each argument, I want the comma to be
the delimiter, but I still
want to ignore the spaces.

The only two ways I know is either to use getline with ',' as a
delimiter - but that wouldn't ignore spaces.
The other is to write your own facet class which sets ',' as a space. -
but then after reading the first comma,
if there's another comma straight after it - the stream will read on -
taking it as a white space, instead of treating
it as a delimiter and stop there.

Any ideas?

see www.boost.org. Check out tokenizer, regex and/or spirit.

Jeff F
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top