Jump to page: 1 2
Thread overview
sscanf() equivalent for D using Phobos library?
Dec 03, 2008
rocknroll714
Dec 04, 2008
Spacen Jasset
Dec 05, 2008
Bill Baxter
Dec 05, 2008
BCS
Dec 05, 2008
Bill Baxter
Dec 05, 2008
BCS
Dec 05, 2008
BCS
Dec 04, 2008
BCS
December 03, 2008
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!

December 04, 2008
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.  :)
December 04, 2008
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.


December 04, 2008
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.  :)
This seems to work on a file, but can this be done on a char[] ?
December 05, 2008
"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.  :)
> This seems to work on a file, but can this be done on a char[] ?

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


December 05, 2008
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.  :)
>> This seems to work on a file, but can this be done on a char[] ?
>
> 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
December 05, 2008
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!?!?


December 05, 2008
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
December 05, 2008
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.
December 05, 2008
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
>>>>>>> 
>>> 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
> 

there should so be a std.scan.scan function to go with the std.format.format.


« First   ‹ Prev
1 2