www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.announce - std.stdio.write, writeln, fwrite, fwriteln

reply "Lionello Lunesu" <lionello lunesu.remove.com> writes:
This is for the cout lovers out there:

I like writefln, but many times it's doing too much. I often forget to print
strings explicitly using "%s", to prevent errors in the (rare) case the
string contains %-characters. That's why I've added write, writeln, fwrite,
fwriteln to std\stdio.d

Attached the changed files from DMD 0.178 (including ddoc). (I have no idea
what flags to pass to diff to get the usual diff output; do tell)

Changes:
* std.format.doFormat has an optional 4th parameter "bool parse" that
determines whether strings should be parsed for %-characters. It defaults to
'true' (for compatibility).
* std.stdio.writefx has an optional 5th parameter "bool parse". Ditto.
* std.stdio.write, writeln, fwrite, fwriteln call writefx with parse=false,
which in turn calls doFormat with parse=false.

Because the signature of doFormat and writefx has changed, you'll have
linker errors when linking to old obj/libs. Since we're <1.0 I thought it
would not be a problem. If it is, I'll have to make new functions doFormat2,
writefx2 (or so) and make the old doFormat, writefx call those new
functions.

By the way: since the "parse" flag is tested early on, printing strings with
write,writeln will probably be slightly faster as well.

Also: I hereby release my changes to the public domain.

L.
Dec 28 2006
next sibling parent "Lionello Lunesu" <lionello lunesu.remove.com> writes:
I forgot to mention:

To test, overwrite the files in dmd/src/phobos/std/ and recompile Phobos.
(follow the instructions from 
http://www.prowiki.org/wiki4d/wiki.cgi?HowTo/CompilePhobos )

Copy the resulting phobos.lib to dmd/lib/, overwriting the phobos.lib that's 
already there.

And an example:

#import std.stdio;
#void main() {
#   writeln("test ", 23, " is %s ", 5.0);
#}

Prints:
test 23 is %s 5

L. 
Dec 28 2006
prev sibling next sibling parent reply "Andrei Alexandrescu (See Website For Email)" <SeeWebsiteForEmail erdani.org> writes:
Lionello Lunesu wrote:
 This is for the cout lovers out there:
 
 I like writefln, but many times it's doing too much. I often forget to print
 strings explicitly using "%s", to prevent errors in the (rare) case the
 string contains %-characters. That's why I've added write, writeln, fwrite,
 fwriteln to std\stdio.d

How about the converse read* functions? Andrei
Dec 28 2006
parent reply "Lionello Lunesu" <lionello lunesu.remove.com> writes:
"Andrei Alexandrescu (See Website For Email)" 
<SeeWebsiteForEmail erdani.org> wrote in message 
news:45941A78.50908 erdani.org...
 Lionello Lunesu wrote:
 This is for the cout lovers out there:

 I like writefln, but many times it's doing too much. I often forget to 
 print
 strings explicitly using "%s", to prevent errors in the (rare) case the
 string contains %-characters. That's why I've added write, writeln, 
 fwrite,
 fwriteln to std\stdio.d

How about the converse read* functions?

I didn't know they existed? Ah wait, they don't! I see, std.stdio is actually only std-output : ) L.
Dec 28 2006
next sibling parent =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Lionello Lunesu wrote:

How about the converse read* functions?

I didn't know they existed? Ah wait, they don't! I see, std.stdio is actually only std-output : )

They exist, but only for std.stream (din) There is also an "unFormat" implementation there (in vreadf). --anders
Dec 28 2006
prev sibling parent reply Sean Kelly <sean f4.ca> writes:
Lionello Lunesu wrote:
 "Andrei Alexandrescu (See Website For Email)" 
 <SeeWebsiteForEmail erdani.org> wrote in message 
 news:45941A78.50908 erdani.org...
 Lionello Lunesu wrote:
 This is for the cout lovers out there:

 I like writefln, but many times it's doing too much. I often forget to 
 print
 strings explicitly using "%s", to prevent errors in the (rare) case the
 string contains %-characters. That's why I've added write, writeln, 
 fwrite,
 fwriteln to std\stdio.d


I didn't know they existed? Ah wait, they don't! I see, std.stdio is actually only std-output : )

Yup, they don't exist, though std.stream does have read functions. As an alternative, I've put a copy of the readf/unFormat routines I wrote ages ago online here: http://www.invisibleduck.org/~sean/code/stdio.d http://www.invisibleduck.org/~sean/code/unformat.d http://www.invisibleduck.org/~sean/code/utf.d They convert everything to dchar internally during processing and this may not be a terribly optimal decision (though given my limited understanding of Unicode at the time it seemed the most prudent approach), but the code may be useful regardless, as it's a complete implementation of the C99 scanf function with some additions for D. The utf changes are necessary to preserve the one-char putback guarantee that C requires, though things admittedly get a bit weird with a single Unicode char being represented by up to four bytes. Sean
Dec 28 2006
parent reply =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Sean Kelly wrote:

 As an alternative, I've put a copy of the readf/unFormat routines I wrote 
 ages ago online here:
 
 http://www.invisibleduck.org/~sean/code/stdio.d
 http://www.invisibleduck.org/~sean/code/unformat.d
 http://www.invisibleduck.org/~sean/code/utf.d

Just for the archives: the ugly workarounds I did at http://www.algonet.se/~afb/d/stdio/ turned out to be just because Mac OS X 10.3 / GCC 3.3 doesn't support templates fully, and GDC uses that for the TypeInfo... For Mac OS X 10.4 / GCC 4.0, it should be working OK ? (DMDScript is also broken on the old, for same reason) --anders
Dec 28 2006
parent reply Sean Kelly <sean f4.ca> writes:
Anders F Björklund wrote:
 Sean Kelly wrote:
 
 As an alternative, I've put a copy of the readf/unFormat routines I 
 wrote ages ago online here:

 http://www.invisibleduck.org/~sean/code/stdio.d
 http://www.invisibleduck.org/~sean/code/unformat.d
 http://www.invisibleduck.org/~sean/code/utf.d

Just for the archives: the ugly workarounds I did at http://www.algonet.se/~afb/d/stdio/ turned out to be just because Mac OS X 10.3 / GCC 3.3 doesn't support templates fully, and GDC uses that for the TypeInfo...

Thanks for pointing this out. I couldn't remember why the workarounds were necessary.
 For Mac OS X 10.4 / GCC 4.0, it should be working OK ?

I hope so. Aside from some inline asm issues, I haven't seen anything DMD can compile that the new GDC cannot. Sean
Dec 28 2006
parent =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Sean Kelly wrote:

 ... because Mac OS X 10.3 / GCC 3.3 doesn't support
 templates fully, and GDC uses that for the TypeInfo...

Thanks for pointing this out. I couldn't remember why the workarounds were necessary.

I've worked on a back-port from Apple's GCC (which *does* support one-only linkage) to FSF GCC 3.3 (which doesn't), but haven't been able to get it out of the crashing stage. Then again, Intel Macs has made it (10.3) obsolete anyway. --anders PS. http://www.algonet.se/~afb/d/gcc-3.3.6-linkonce.patch (If you can make it work, please follow up to D.gnu!)
Dec 28 2006
prev sibling next sibling parent reply =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Lionello Lunesu wrote:

 I like writefln, but many times it's doing too much. I often forget to print
 strings explicitly using "%s", to prevent errors in the (rare) case the
 string contains %-characters. That's why I've added write, writeln, fwrite,
 fwriteln to std\stdio.d

I hope that you have better luck with your patch than I did with mine... http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=digitalmars.D&artnum=15627
 Attached the changed files from DMD 0.178 (including ddoc). (I have no idea
 what flags to pass to diff to get the usual diff output; do tell)

diff -u
 Because the signature of doFormat and writefx has changed, you'll have
 linker errors when linking to old obj/libs. Since we're <1.0 I thought it
 would not be a problem. If it is, I'll have to make new functions doFormat2,
 writefx2 (or so) and make the old doFormat, writefx call those new
 functions.

I hope so. Secretly I also hope that readf / unFormat will make it too ? (see http://www.digitalmars.com/d/archives/digitalmars/D/21692.html) --anders
Dec 28 2006
parent "Lionello Lunesu" <lionello lunesu.remove.com> writes:
"Anders F Björklund" <afb algonet.se> wrote in message 
news:en166e$1lou$1 digitaldaemon.com...
 I hope that you have better luck with your patch than I did with mine...

 http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=digitalmars.D&artnum=15627

I figured it had to be done before : S Maybe 'yet another patch' will convince Walter that this is badly needed : )
 Attached the changed files from DMD 0.178 (including ddoc). (I have no 
 idea
 what flags to pass to diff to get the usual diff output; do tell)

diff -u

Thanks.. Why isn't that the default, I wonder.
 I hope so. Secretly I also hope that readf / unFormat will make it too ?
 (see http://www.digitalmars.com/d/archives/digitalmars/D/21692.html)

Hey Andrei! Check that out : ) My wish for the new year will be Phobos being hosted on dsource. L.
Dec 28 2006
prev sibling parent "Lionello Lunesu" <lionello lunesu.remove.com> writes:
The diff -u against files from 0.178.

This patch also solves the following bug in doFormat:
(a bug that has nothing to do with write/writeln, but is easily solvable 
because of them)

#import std.stdio;
#void main() {
#   const char[][] x = ["%s","123"];
#   writefln(x);
#}

C:\dmd>dmd -run bug
[Error: std.format

This happens because doFormat is called recursively, once for each of the 
elements of the array. This means that there should never be any parsing for 
string elements, since there'll never be any 2nd argument to be formatted.

Because the new doFormat takes the extra "bool parse" parameter, the fix is 
simple: invoke doFormat with parse set to false.

L. 
Dec 28 2006