www.digitalmars.com         C & C++   DMDScript  

D - native D puts()

reply Jonathan Andrew <Jonathan_member pathlink.com> writes:
Hello, for those interested, I whipped up a quick & dirty puts() function for D
strings. It was my understanding that the c.stdio puts() expects null
termination. I figured it would be an easy way for people new to the language to
play around without having to worry about all the %.*s stuff in printf.

//-------------------cut here--------------------------
import windows;
int putsd(char[] instring)
{
uint numwritten;
HANDLE h;
h = (HANDLE) -11;			

WriteFile(h, instring, instring.length, &numwritten, null);

if(numwritten != instring.length)
return 1;
else
return 0;
}

//--------------------cut here----------------------

Is there a lower-level way to do this? I figured calling the windows WriteFile
was as low as practical, but if somebody knows an even better way to implement
this, I would love to see it.

Thanks,
-Jon
May 08 2003
parent reply "Sean L. Palmer" <palmer.sean verizon.net> writes:
I hate to see this sort of stuff proliferating.  It would be prudent to
hammer out a good D streams spec now.  Preferrably something that doesn't
require layering atop C libraries.  Hopefully add a "stddbg" sort of debug
output stream as well (defaults to stderr or stdout if you're on a
console-only platform).

Assuming handle -11 is stdout doesn't seem to be such a good idea, either.
I'm not saying it doesn't work;  I haven't checked.

Sean

"Jonathan Andrew" <Jonathan_member pathlink.com> wrote in message
news:b9epl6$1qri$1 digitaldaemon.com...
 Hello, for those interested, I whipped up a quick & dirty puts() function

 strings. It was my understanding that the c.stdio puts() expects null
 termination. I figured it would be an easy way for people new to the

 play around without having to worry about all the %.*s stuff in printf.

 //-------------------cut here--------------------------
 import windows;
 int putsd(char[] instring)
 {
 uint numwritten;
 HANDLE h;
 h = (HANDLE) -11;

 WriteFile(h, instring, instring.length, &numwritten, null);

 if(numwritten != instring.length)
 return 1;
 else
 return 0;
 }

 //--------------------cut here----------------------

 Is there a lower-level way to do this? I figured calling the windows

 was as low as practical, but if somebody knows an even better way to

 this, I would love to see it.

 Thanks,
 -Jon

May 08 2003
next sibling parent Jonathan Andrew <Jonathan_member pathlink.com> writes:
I hate to see this sort of stuff proliferating.  It would be prudent to
hammer out a good D streams spec now.  Preferrably something that doesn't
require layering atop C libraries.  Hopefully add a "stddbg" sort of debug
output stream as well (defaults to stderr or stdout if you're on a
console-only platform).

Assuming handle -11 is stdout doesn't seem to be such a good idea, either.
I'm not saying it doesn't work;  I haven't checked.

Sean

As far as I knew, WriteFile is just a windows system call buried in a library somwhere. If I am wrong about this, please let me know, I'm new to this whole thing. I got the -11 from Pavel's windows.d. There are other functions that will look up this number, but I figured Pavel knew what he was doing when he wrote his API, certainly more than I do =). -Jon
May 09 2003
prev sibling parent reply Karl Bochert <kbochert copper.net> writes:
On Thu, 8 May 2003 22:01:21 -0700, "Sean L. Palmer" <palmer.sean verizon.net>
wrote:
 I hate to see this sort of stuff proliferating.  It would be prudent to
 hammer out a good D streams spec now.  Preferrably something that doesn't
 require layering atop C libraries.  Hopefully add a "stddbg" sort of debug
 output stream as well (defaults to stderr or stdout if you're on a
 console-only platform).
 
 Assuming handle -11 is stdout doesn't seem to be such a good idea, either.
 I'm not saying it doesn't work;  I haven't checked.
 
 Sean

How about stdin stdout stderr stdkbd -- read keyboard even if input redirected stddbg -- Behavior determined by command-line switch (or similar) -debug Output to console -log Output to file -regress Compare to existing regression file -release No output Karl Bochert
May 09 2003
parent reply "C. Sauls" <ibisbasenji yahoo.com> writes:
 stdkbd -- read keyboard even if input redirected

 stddbg -- Behavior determined by command-line switch (or similar)
    -debug  Output to console
    -log        Output to file
    -regress  Compare to existing regression file
    -release  No output

I like the idea with this, but rather than the switches as you have them, it might be safer to use something like: -stddbg=console -stddbg=log -stddbg=regress -stddbg=none & -stddbg=release And I'm thinking the compiler could seek&destroy calls to stddbg when 'none' or 'release' is specified. But I'm not a compiler expert by the slightest means. -- C. Sauls
May 09 2003
next sibling parent Ilya Minkov <midiclub 8ung.at> writes:
There is also another target where to output: there is some way to make 
a debugger, in the case it is running, display and log a message.

C. Sauls wrote:
 I like the idea with this, but rather than the switches as you have them, it
 might be safer to use something like:
     -stddbg=console
     -stddbg=log
     -stddbg=regress
     -stddbg=none & -stddbg=release
 
 And I'm thinking the compiler could seek&destroy calls to stddbg when 'none'
 or 'release' is specified.  But I'm not a compiler expert by the slightest
 means.
 
  -- C. Sauls

May 09 2003
prev sibling parent reply Karl Bochert <kbochert copper.net> writes:
On Fri, 9 May 2003 09:06:22 -0500, "C. Sauls" <ibisbasenji yahoo.com> wrote:
 stdkbd -- read keyboard even if input redirected

 stddbg -- Behavior determined by command-line switch (or similar)
    -debug  Output to console
    -log        Output to file
    -regress  Compare to existing regression file
    -release  No output

I like the idea with this, but rather than the switches as you have them, it might be safer to use something like: -stddbg=console -stddbg=log -stddbg=regress -stddbg=none & -stddbg=release

As long as it can handle output to more than one location i.e. '-stddgb=console,log' Best is if the compiler can toggle these flags from the source -- something like (in another world) '#pragma stddbg=log' Can D do this?
 
 And I'm thinking the compiler could seek&destroy calls to stddbg when 'none'
 or 'release' is specified.  But I'm not a compiler expert by the slightest
 means.

 
  -- C. Sauls
 
 

May 09 2003
parent "Walter" <walter digitalmars.com> writes:
"Karl Bochert" <kbochert copper.net> wrote in message
news:1103_1052526238 bose...
 Best is if the compiler can toggle these flags from the source --

 like (in another world) '#pragma stddbg=log'
 Can D do this?

Yes - check out the version and debug statements.
May 11 2003