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!

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. :)

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. :)

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

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

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

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.

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.

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.

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.