Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > string splitting

Reply
Thread Tools

string splitting

 
 
xyz
Guest
Posts: n/a
 
      04-29-2008
I have a string
16:23:18.659343 131.188.37.230.22 131.188.37.59.1398 tcp 168

for example lets say for the above string
16:23:18.659343 -- time
131.188.37.230 -- srcaddress
22 --srcport
131.188.37.59 --destaddress
1398 --destport
tcp --protocol
168 --size
i need to split the string such that i need to get all these
parameters....
the field widths are not fixed..i have some times four/three digits
srcport ..so i cant do it with substr function...i need this in c++
i am not getting an idea how to split it..
thank you for any help
 
Reply With Quote
 
 
 
 
Lars Uffmann
Guest
Posts: n/a
 
      04-29-2008
Google for c++ explode, you'll find a lot of infos on how to do it.
 
Reply With Quote
 
 
 
 
alasham.said@gmail.com
Guest
Posts: n/a
 
      04-29-2008
See "A string tokenizer" here: http://oopweb.com/CPP/Documents/CPPH...g-HOWTO-7.html

Regards.
 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      04-29-2008


--
Jim Langston

"xyz" <> wrote in message
news:8daf3ab2-37ef-47ac-b2b4-...
>I have a string
> 16:23:18.659343 131.188.37.230.22 131.188.37.59.1398 tcp 168
>
> for example lets say for the above string
> 16:23:18.659343 -- time
> 131.188.37.230 -- srcaddress
> 22 --srcport
> 131.188.37.59 --destaddress
> 1398 --destport
> tcp --protocol
> 168 --size
> i need to split the string such that i need to get all these
> parameters....
> the field widths are not fixed..i have some times four/three digits
> srcport ..so i cant do it with substr function...i need this in c++
> i am not getting an idea how to split it..
> thank you for any help


Not complete but giving you all the pieces.

You should use your favorite method for converting from strings to ints,
I'im showing a manual stringstream way, but I use a template myself.

Output is:

16:23:18.659343 -- time
131.188.37.230.22 -- srcaddress/port
131.188.37.59.1398 -- destaddress/port
tcp -- protocol
168 -- size

131.188.37.230 : 22

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

int main()
{
std::string Input( "16:23:18.659343 131.188.37.230.22 131.188.37.59.1398
tcp 168" );
std::stringstream Stream( Input );

std::string Time;
std::string SrcAddressPort;
std::string DestAddressPort;
std::string Protocol;
int Size;

if ( Stream >> Time >> SrcAddressPort >> DestAddressPort >> Protocol >>
Size )
{
std::cout << Time << " -- time\n" <<
SrcAddressPort << " -- srcaddress/port\n" <<
DestAddressPort << " -- destaddress/port\n" <<
Protocol << " -- protocol\n" <<
Size << " -- size\n\n";
}
else
std::cerr << "Parsing error\n";

std::string SrcAddress;
std::string PortString;
int SrcPort = 0;

SrcAddress = SrcAddressPort.substr( 0,
SrcAddressPort.find_last_of('.') );
PortString = SrcAddressPort.substr( SrcAddressPort.find_last_of('.') +
1, std::string::npos );

std::stringstream Convert;
Convert << PortString;
Convert >> SrcPort;

std::cout << SrcAddress << " : " << SrcPort << "\n";

}



 
Reply With Quote
 
kwikius
Guest
Posts: n/a
 
      04-29-2008

"xyz" <> wrote in message
news:8daf3ab2-37ef-47ac-b2b4-...
>I have a string
> 16:23:18.659343 131.188.37.230.22 131.188.37.59.1398 tcp 168
>
> for example lets say for the above string
> 16:23:18.659343 -- time
> 131.188.37.230 -- srcaddress
> 22 --srcport
> 131.188.37.59 --destaddress
> 1398 --destport
> tcp --protocol
> 168 --size
> i need to split the string such that i need to get all these
> parameters....
> the field widths are not fixed..i have some times four/three digits
> srcport ..so i cant do it with substr function...i need this in c++
> i am not getting an idea how to split it..
> thank you for any help


Parsing is best solved formally with a parser generator, for which the best
option is to write a grammar.

Below is a LL(1) grammar written as source code for slk parser:
LL(1) grammar is very similar to hand written parsing

http://home.earthlink.net/~slkpg/

In the grammar the parts prefixed with "__" are actions which you write
code for in C++ (or C ,Java or C#).
Slk does most of the rest of the working in creating the application

----------------

/*
slk grammar
integer and tcp are terminals from the lexer
*/

parser :
time src dest proto

time:
integer __hr : integer __min : integer __sec_int [ . integer __sec_frac ]

src:
integer __s1 . integer __s2 . integer __s3 . integer __s4 . integer __port

dest:
integer __d1 . integer __d2 . integer __d3 . integer __d4

proto:
tcp integer __size


-----------------

regards
Andy Little






 
Reply With Quote
 
xyz
Guest
Posts: n/a
 
      04-29-2008
On Apr 29, 3:32*pm, "kwikius" <a...@servocomm.freeserve.co.uk> wrote:
> "xyz" <lavanyaredd...@gmail.com> wrote in message
>
> news:8daf3ab2-37ef-47ac-b2b4-...
>
>
>
> >I have a string
> > 16:23:18.659343 131.188.37.230.22 131.188.37.59.1398 tcp 168

>
> > for example lets say for the above string
> > 16:23:18.659343 -- time
> > 131.188.37.230 * -- srcaddress
> > 22 * * * * * * * * * * * *--srcport
> > 131.188.37.59 * *--destaddress
> > 1398 * * * * * * * * *--destport
> > tcp * * * * * * * * * *--protocol
> > 168 * * * * * * * * *--size
> > i need to split the string such that i need to get all these
> > parameters....
> > the field widths are not fixed..i have some times four/three digits
> > srcport ..so i cant do it with substr function...i need this in c++
> > i am not getting an idea how to split it..
> > thank you for any help

>
> Parsing is best solved formally with a parser generator, for which the best
> option is to write a grammar.
>
> Below is a LL(1) grammar written as source code for slk parser:
> LL(1) *grammar is very similar to hand written parsing
>
> http://home.earthlink.net/~slkpg/
>
> In the grammar the *parts prefixed with "__" are actions which you write
> code for in C++ (or C ,Java or C#).
> Slk does most of the rest of the working in creating the application
>
> ----------------
>
> /*
> slk grammar
> integer and tcp are terminals *from the lexer
> */
>
> parser :
> * time src dest proto
>
> time:
> * integer __hr : integer __min : integer *__sec_int [ . integer __sec_frac ]
>
> src:
> * integer __s1 . integer __s2 . integer __s3 . integer __s4 . integer __port
>
> dest:
> * integer __d1 . integer __d2 . integer __d3 . integer __d4
>
> proto:
> * tcp integer __size
>
> -----------------
>
> regards
> Andy Little


i solved it....thanks to all
 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      04-29-2008
xyz wrote:

> I have a string


Pick a language. You posted the same thing (twice) to comp.lang.c.





Brian
 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      04-30-2008
Jim Langston wrote:
>> I have a string
>> 16:23:18.659343 131.188.37.230.22 131.188.37.59.1398 tcp 168
>>
>> for example lets say for the above string
>> 16:23:18.659343 -- time
>> 131.188.37.230 -- srcaddress
>> 22 --srcport
>> 131.188.37.59 --destaddress
>> 1398 --destport
>> tcp --protocol
>> 168 --size
>> i need to split the string such that i need to get all these
>> parameters....
>> the field widths are not fixed..i have some times four/three digits
>> srcport ..so i cant do it with substr function...i need this in c++
>> i am not getting an idea how to split it..
>> thank you for any help

>
> Not complete but giving you all the pieces.
>
> You should use your favorite method for converting from strings to
> ints, I'im showing a manual stringstream way, but I use a template
> myself.
> Output is:
>
> 16:23:18.659343 -- time
> 131.188.37.230.22 -- srcaddress/port
> 131.188.37.59.1398 -- destaddress/port
> tcp -- protocol
> 168 -- size
>
> 131.188.37.230 : 22
>
> #include <string>
> #include <sstream>
> #include <iostream>
>
> int main()
> {
> std::string Input( "16:23:18.659343 131.188.37.230.22
> 131.188.37.59.1398 tcp 168" );
> std::stringstream Stream( Input );
>
> std::string Time;
> std::string SrcAddressPort;
> std::string DestAddressPort;
> std::string Protocol;
> int Size;
>
> if ( Stream >> Time >> SrcAddressPort >> DestAddressPort >>
> Protocol >> Size )
> {
> std::cout << Time << " -- time\n" <<
> SrcAddressPort << " -- srcaddress/port\n" <<
> DestAddressPort << " -- destaddress/port\n" <<
> Protocol << " -- protocol\n" <<
> Size << " -- size\n\n";
> }
> else
> std::cerr << "Parsing error\n";
>
> std::string SrcAddress;
> std::string PortString;
> int SrcPort = 0;
>
> SrcAddress = SrcAddressPort.substr( 0,
> SrcAddressPort.find_last_of('.') );
> PortString = SrcAddressPort.substr(
> SrcAddressPort.find_last_of('.') + 1, std::string::npos );


Oh, I forgot about a substr overload. This line can be simplified to:
PortString = SrcAddressPort.substr( SrcAddressPort.find_last_of('.') +
1 );

std::string::npos is default for 2nd paramenter.

> std::stringstream Convert;
> Convert << PortString;
> Convert >> SrcPort;
>
> std::cout << SrcAddress << " : " << SrcPort << "\n";
>
> }


--
Jim Langston



 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      04-30-2008
On Apr 29, 3:32 pm, "kwikius" <a...@servocomm.freeserve.co.uk> wrote:
> "xyz" <lavanyaredd...@gmail.com> wrote in message
> >I have a string
> > 16:23:18.659343 131.188.37.230.22 131.188.37.59.1398 tcp 168


> > for example lets say for the above string
> > 16:23:18.659343 -- time
> > 131.188.37.230 -- srcaddress
> > 22 --srcport
> > 131.188.37.59 --destaddress
> > 1398 --destport
> > tcp --protocol
> > 168 --size
> > i need to split the string such that i need to get all these
> > parameters....
> > the field widths are not fixed..i have some times four/three digits
> > srcport ..so i cant do it with substr function...i need this in c++
> > i am not getting an idea how to split it..


> Parsing is best solved formally with a parser generator, for
> which the best option is to write a grammar.


I don't think that there's a general consensus about that. None
of the C++ compilers I know use a parser generator for their
grammar, for example, but prefer hand written ones.

In the case at hand, of course, you don't even need a full
parser; his problem can be solved simply by means of extended
regular expressions, such as those supported by boost::regex.

--
James Kanze (GABI Software) email:
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      04-30-2008
James Kanze wrote:
> On Apr 29, 3:32 pm, "kwikius" <a...@servocomm.freeserve.co.uk> wrote:
>> "xyz" <lavanyaredd...@gmail.com> wrote in message
>>> I have a string
>>> 16:23:18.659343 131.188.37.230.22 131.188.37.59.1398 tcp 168

>
>>> for example lets say for the above string
>>> 16:23:18.659343 -- time
>>> 131.188.37.230 -- srcaddress
>>> 22 --srcport
>>> 131.188.37.59 --destaddress
>>> 1398 --destport
>>> tcp --protocol
>>> 168 --size
>>> i need to split the string such that i need to get all these
>>> parameters....
>>> the field widths are not fixed..i have some times four/three digits
>>> srcport ..so i cant do it with substr function...i need this in c++
>>> i am not getting an idea how to split it..

>
>> Parsing is best solved formally with a parser generator, for
>> which the best option is to write a grammar.

>
> I don't think that there's a general consensus about that. None
> of the C++ compilers I know use a parser generator for their
> grammar, for example, but prefer hand written ones.
>
> In the case at hand, of course, you don't even need a full
> parser; his problem can be solved simply by means of extended
> regular expressions, such as those supported by boost::regex.


Reading up on C++0x it is supposed to contain regular expressions. Which is
good for this, but bad because I hate regex.

But, truthfully, having regex in the language will make parsing this type of
thing a *lot* easier. Although to me regex expressions usually look like
just so much line noise.

--
Jim Langston



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Splitting Strings in a map<string, string> byte8bits@gmail.com C++ 3 04-15-2008 06:24 PM
splitting a string into a drop down =?Utf-8?B?SnVzdGlu?= ASP .Net 1 10-25-2004 01:32 AM
Re: Splitting up the definitions of a class into different files (splitting public from private)? John Dibling C++ 0 07-19-2003 04:41 PM
Re: Splitting up the definitions of a class into different files (splitting public from private)? Mark C++ 0 07-19-2003 04:24 PM
Re: Splitting up the definitions of a class into different files (splitting public from private)? John Ericson C++ 0 07-19-2003 04:03 PM



Advertisments