www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - sscanf() equivalent for D using Phobos library?

reply rocknroll714 <rocknroll714 gmail.com> writes:
Hi. I discovered D recently and I am very, very impressed. I've
decided to switch from C/C++ over to D as my programming language of
choice. Right now I'm porting one of my C++ applications to D, and I
need some help with a simple conversion.

How would I do this:

if(sscanf(line, "%[^:]: %[^\r\n]", name, value) != 2) {...}

In D? I'm looping through a text file using the File.getline() command
and splitting each line into two separate variables with the sscanf
command. The if conditional check is to make sure the line is
formatted properly (the text file is the settings file for my
application).

Help please!
Dec 03 2008
next sibling parent reply "Jarrett Billingsley" <jarrett.billingsley gmail.com> writes:
On Wed, Dec 3, 2008 at 6:37 PM, rocknroll714 <rocknroll714 gmail.com> wrote:
 Hi. I discovered D recently and I am very, very impressed. I've
 decided to switch from C/C++ over to D as my programming language of
 choice. Right now I'm porting one of my C++ applications to D, and I
 need some help with a simple conversion.

 How would I do this:

 if(sscanf(line, "%[^:]: %[^\r\n]", name, value) != 2) {...}

 In D? I'm looping through a text file using the File.getline() command
 and splitting each line into two separate variables with the sscanf
 command. The if conditional check is to make sure the line is
 formatted properly (the text file is the settings file for my
 application).

 Help please!

import std.cstream;, and use din.readf. readf is documented in std.stream. :)
Dec 03 2008
parent reply Spacen Jasset <spacenjasset yahoo.co.uk> writes:
Jarrett Billingsley wrote:
 On Wed, Dec 3, 2008 at 6:37 PM, rocknroll714 <rocknroll714 gmail.com> wrote:
 Hi. I discovered D recently and I am very, very impressed. I've
 decided to switch from C/C++ over to D as my programming language of
 choice. Right now I'm porting one of my C++ applications to D, and I
 need some help with a simple conversion.

 How would I do this:

 if(sscanf(line, "%[^:]: %[^\r\n]", name, value) != 2) {...}

 In D? I'm looping through a text file using the File.getline() command
 and splitting each line into two separate variables with the sscanf
 command. The if conditional check is to make sure the line is
 formatted properly (the text file is the settings file for my
 application).

 Help please!

import std.cstream;, and use din.readf. readf is documented in std.stream. :)

Dec 04 2008
parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
"Spacen Jasset" wrote
 Jarrett Billingsley wrote:
 On Wed, Dec 3, 2008 at 6:37 PM, rocknroll714 <rocknroll714 gmail.com> 
 wrote:
 Hi. I discovered D recently and I am very, very impressed. I've
 decided to switch from C/C++ over to D as my programming language of
 choice. Right now I'm porting one of my C++ applications to D, and I
 need some help with a simple conversion.

 How would I do this:

 if(sscanf(line, "%[^:]: %[^\r\n]", name, value) != 2) {...}

 In D? I'm looping through a text file using the File.getline() command
 and splitting each line into two separate variables with the sscanf
 command. The if conditional check is to make sure the line is
 formatted properly (the text file is the settings file for my
 application).

 Help please!

import std.cstream;, and use din.readf. readf is documented in std.stream. :)


might be talking out of my ass, since I'm a tango user, but I think readf is a function used by all streams? So you just need to make that char[] into a stream, and then you get readf functionality, maybe. -Steve
Dec 04 2008
prev sibling next sibling parent BCS <ao pathlink.com> writes:
Reply to rocknroll714,

 Hi. I discovered D recently and I am very, very impressed. I've
 decided to switch from C/C++ over to D as my programming language of
 choice. Right now I'm porting one of my C++ applications to D, and I
 need some help with a simple conversion.
 
 How would I do this:
 
 if(sscanf(line, "%[^:]: %[^\r\n]", name, value) != 2) {...}
 
 In D? I'm looping through a text file using the File.getline() command
 and splitting each line into two separate variables with the sscanf
 command. The if conditional check is to make sure the line is
 formatted properly (the text file is the settings file for my
 application).
 
 Help please!
 

the quick and wrong way that works is to go find std.c.stdio and call the C sscanf function. (be sure to uses toStringz on the line arg and to allocate for the outputs as you would in c as for a native D equivalent... I'm not sure.
Dec 03 2008
prev sibling parent reply "Bill Baxter" <wbaxter gmail.com> writes:
On Fri, Dec 5, 2008 at 9:06 AM, Steven Schveighoffer
<schveiguy yahoo.com> wrote:
 "Spacen Jasset" wrote
 Jarrett Billingsley wrote:
 On Wed, Dec 3, 2008 at 6:37 PM, rocknroll714 <rocknroll714 gmail.com>
 wrote:
 Hi. I discovered D recently and I am very, very impressed. I've
 decided to switch from C/C++ over to D as my programming language of
 choice. Right now I'm porting one of my C++ applications to D, and I
 need some help with a simple conversion.

 How would I do this:

 if(sscanf(line, "%[^:]: %[^\r\n]", name, value) != 2) {...}

 In D? I'm looping through a text file using the File.getline() command
 and splitting each line into two separate variables with the sscanf
 command. The if conditional check is to make sure the line is
 formatted properly (the text file is the settings file for my
 application).

 Help please!

import std.cstream;, and use din.readf. readf is documented in std.stream. :)


might be talking out of my ass, since I'm a tango user, but I think readf is a function used by all streams? So you just need to make that char[] into a stream, and then you get readf functionality, maybe.

I think that's what std.stream.MemoryStream is for, but I might be talking out of my ass too because I've never used it. But it looks like you could say scope thestream = new std.stream.MemoryStream(thebuffer); then use thestream just like it was a file. You may have to cast(byte[])thebuffer, though. --bb
Dec 04 2008
next sibling parent reply BCS <ao pathlink.com> writes:
Reply to Bill,

 On Wed, Dec 3, 2008 at 6:37 PM, rocknroll714
 <rocknroll714 gmail.com> wrote:
 
 I'm looping through a text file using the File.getline()
 command




scope thestream = new std.stream.MemoryStream(thebuffer);

One new MemoeryStream per line!?!?
Dec 04 2008
parent reply =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Jarrett Billingsley wrote:

 I could never quite figure out why Stream.readf was not separated out
 into a separate function in std.format.  It probably wouldn't be a lot
 of work.

I thought that was what std.unformat was for ? But I would have put readf in std.stdio too... --anders
Dec 05 2008
parent =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Jarrett Billingsley wrote:

 I could never quite figure out why Stream.readf was not separated out
 into a separate function in std.format.  It probably wouldn't be a lot
 of work.


Such a library exists? It's not in the D docs. I don't see anything in the Phobos source dir either.

It was suggested for DMD 0.128, but I don't think it made it. --anders
Dec 05 2008
prev sibling next sibling parent reply "Bill Baxter" <wbaxter gmail.com> writes:
On Fri, Dec 5, 2008 at 11:09 AM, BCS <ao pathlink.com> wrote:
 Reply to Bill,

 On Wed, Dec 3, 2008 at 6:37 PM, rocknroll714
 <rocknroll714 gmail.com> wrote:

 I'm looping through a text file using the File.getline()
 command




scope thestream = new std.stream.MemoryStream(thebuffer);

One new MemoeryStream per line!?!?

It's scope! No worries mate! ;-) But seriously, it does sound heavyweight compared to a single sscanf function call. Honestly, std.stream is just barely functional enough to write the simplest stream code, so I wouldn't be surprised if that's what it takes to scan strings with it. Not much worse than what it takes to do it with std::stream in c++ though. I'm really talking out of my ass though, because I can probably count on one hand how many times written code to use scanf or stringstream in my life. If I have to parse input I'm much more likely to reach for the regexp hammer or write a simpler parser. --bb
Dec 04 2008
parent BCS <ao pathlink.com> writes:
Reply to Bill,

 On Fri, Dec 5, 2008 at 11:09 AM, BCS <ao pathlink.com> wrote:
 
 Reply to Bill,
 
 On Wed, Dec 3, 2008 at 6:37 PM, rocknroll714
 <rocknroll714 gmail.com> wrote:
 I'm looping through a text file using the File.getline() command
 






But seriously, it does sound heavyweight compared to a single sscanf function call. Honestly, std.stream is just barely functional enough to write the simplest stream code, so I wouldn't be surprised if that's what it takes to scan strings with it. Not much worse than what it takes to do it with std::stream in c++ though. I'm really talking out of my ass though, because I can probably count on one hand how many times written code to use scanf or stringstream in my life. If I have to parse input I'm much more likely to reach for the regexp hammer or write a simpler parser. --bb

there should so be a std.scan.scan function to go with the std.format.format.
Dec 04 2008
prev sibling next sibling parent reply "Jarrett Billingsley" <jarrett.billingsley gmail.com> writes:
On Thu, Dec 4, 2008 at 9:25 PM, Bill Baxter <wbaxter gmail.com> wrote:
 It's scope!  No worries mate! ;-)

 But seriously, it does sound heavyweight compared to a single sscanf
 function call.
 Honestly, std.stream is just barely functional enough to write the
 simplest stream code, so I wouldn't be surprised if that's what it
 takes to scan strings with it.  Not much worse than what it takes to
 do it with std::stream in c++ though.

 I'm really talking out of my ass though, because I can probably count
 on one hand how many times written code to use scanf or stringstream
 in my life.  If I have to parse input I'm much more likely to reach
 for the regexp hammer or write a simpler parser.

I could never quite figure out why Stream.readf was not separated out into a separate function in std.format. It probably wouldn't be a lot of work.
Dec 04 2008
parent BCS <ao pathlink.com> writes:
Reply to Jarrett,

 On Thu, Dec 4, 2008 at 9:25 PM, Bill Baxter <wbaxter gmail.com> wrote:
 
 It's scope!  No worries mate! ;-)
 
 But seriously, it does sound heavyweight compared to a single sscanf
 function call.
 Honestly, std.stream is just barely functional enough to write the
 simplest stream code, so I wouldn't be surprised if that's what it
 takes to scan strings with it.  Not much worse than what it takes to
 do it with std::stream in c++ though.
 I'm really talking out of my ass though, because I can probably count
 on one hand how many times written code to use scanf or stringstream
 in my life.  If I have to parse input I'm much more likely to reach
 for the regexp hammer or write a simpler parser.
 

into a separate function in std.format. It probably wouldn't be a lot of work.

do it, submit patch, become hero. <G>
Dec 04 2008
prev sibling parent "Jarrett Billingsley" <jarrett.billingsley gmail.com> writes:
On Fri, Dec 5, 2008 at 5:43 AM, Anders F Bj=F6rklund <afb algonet.se> wrote=
:
 Jarrett Billingsley wrote:

 I could never quite figure out why Stream.readf was not separated out
 into a separate function in std.format.  It probably wouldn't be a lot
 of work.

I thought that was what std.unformat was for ?

Such a library exists? It's not in the D docs. I don't see anything in the Phobos source dir either.
 But I would have put readf in std.stdio too...

 --anders

Dec 05 2008