473,473 Members | 4,185 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Manipulator for parsing quoted strings

I have a long string of quoted strings, like:

"string 1" "string 2" ...

and I need to split this up into the constituent quoted strings. I was
thinking it would be nice if I could somehow put it in an istringstream
and use operator>> to parse quoted strings instead of
whitespace-separated strings. Would it be at all possible to use a
manipulator for this? So that I could write:

is >> quoted >> s1;
is >> quoted >> s2;

and have s1 contain "string 1" and s2 "string 2". quoted() could easily
be implemented to read everything between the quotes, but I can't see a
way for it to hand over the parsed string to the s1 following it. Is it
possible to do this?

(I don't want to have to subclass string just to override its
operator>>. That seems wrong.)

Thanks,
Erik

Jan 31 '06 #1
4 2619

<er*********@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
I have a long string of quoted strings, like:

"string 1" "string 2" ...

and I need to split this up into the constituent quoted strings. I was
thinking it would be nice if I could somehow put it in an istringstream
and use operator>> to parse quoted strings instead of
whitespace-separated strings. Would it be at all possible to use a
manipulator for this? So that I could write:

is >> quoted >> s1;
is >> quoted >> s2;

and have s1 contain "string 1" and s2 "string 2". quoted() could easily
be implemented to read everything between the quotes, but I can't see a
way for it to hand over the parsed string to the s1 following it. Is it
possible to do this?

(I don't want to have to subclass string just to override its
operator>>. That seems wrong.)


Look up 'std::getline()' (a nonmember function from <string>).
Its last parameter denotes a delimiter you can supply (defaults
to '\n'). You could store each extracted token in e.g. a vector,
then remove any unwanted blank or empty strings (or simply deal
with them as they are created).

-Mike
Jan 31 '06 #2
Yep, getline could be used for the job, but I was just curious as to
whether it would be possible to use the extractor syntax for a task
like this. Not because there's no other way of doing it, but just
because I'd really like to do it that way. :)

Jan 31 '06 #3

er*********@gmail.com wrote:
Yep, getline could be used for the job, but I was just curious as to
whether it would be possible to use the extractor syntax for a task
like this. Not because there's no other way of doing it, but just
because I'd really like to do it that way. :)


This is not quite what you wanted, but you could always "fake" the
istream syntax like this:

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

class quoted_extractor
{
public:
quoted_extractor( const std::string & s )
: is_( new std::istringstream(s) )
{
*is_ >> std::noskipws;
}

~quoted_extractor()
{
delete is_;
}

quoted_extractor & operator>> ( std::string & s )
{
char ch;
bool open = false;

s = std::string();

while( !open && *is_ >> ch )
{
if ( ch == '"')
open = true;
}

while( open && *is_ >> ch )
{
if ( ch != '"')
s += ch;
else
open = false;
}

return *this;
}

operator bool() const
{
return is_->good();
}

private:
std::istream *is_;
};
int main()
{
quoted_extractor qs( "\"Hello World\" in german is \"Hallo Welt\"."
);
std::string hello;
while ( qs >> hello )
std::cout << hello << std::endl;
}

Cheers,
Andre

Jan 31 '06 #4
Thanks for the in-depth answer, Andre. That does indeed seem like a lot
of trouble to go through just to get to use the >> syntax. At least
this has given me a greater understanding of what manipulators can and
cannot do.

Feb 1 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: ARK | last post by:
I am writing a search program in ASP(VBScript). The user can enter keywords and press submit. The user can separate the keywords by spaces and/or commas and key words may contain plain words,...
8
by: stoptv | last post by:
Hello group, this is my dilemma: ------------------------------------------------------------------------ #include <iostream> using namespace std; // a regular manipulator ostream & hello(...
12
by: Simone Mehta | last post by:
hi All, I am parsing a CSV file. I want to read every row into a char array of reasonable size and then extract strings from it. <snippet> char foo="hello,world,bye,bye,world"; ........
6
by: John Paulsson | last post by:
Is there a C# pattern or perhaps a .NET framework way of parsing a command lines, supporting quoted filepaths etc? (I'm not talking about the current applications command arguments. I've got...
2
by: neilmcguigan | last post by:
this is more of a text parsing/regex kind of question, but i figured i'd start here. please let me know if this should go somewhere else. I'd like to implement google-like search syntax, a la...
6
by: jack | last post by:
I have a class which overloads the insertion operator '<<' for every type that ostream handles. I do this so that I can use my class a direct replacement for cout and cerr where the insertion...
29
by: zoltan | last post by:
Hi, The scenario is like this : struct ns_rr { const u_char* rdata; }; The rdata field contains some fields such as :
17
by: Mark | last post by:
I must create a routine that finds tokens in small, arbitrary VB code snippets. For example, it might have to find all occurrences of {Formula} I was thinking that using regular expressions...
6
by: i_robot73 | last post by:
I have a file, containing hex values for dates (MMDDYYYY)<status code><??such as: ...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.