www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Add "setBinaryMode" and "setTextMode" to stdio?

reply Johan Engelen <j j.nl> writes:
Hi all,
   To fix EOL writing with "dfmt ---> stdout" on Windows, stdout 
has to be set to binary mode [1]. The code for this is 
non-trivial, and some DMD internal magic is needed:

version(Windows)
{
     // See Phobos' stdio.File.rawWrite
     {
         import std.stdio;
         immutable fd = fileno(stdout.getFP());
         setmode(fd, _O_BINARY);
         version(CRuntime_DigitalMars)
         {
             import core.atomic : atomicOp;
             atomicOp!"&="(__fhnd_info[fd], ~FHND_TEXT);
         }
     }
}

I think it'd be very nice to have stdio.File.setBinaryMode() and 
stdio.File.setTextMode().
In dfmt's case, rawWrite is not available because 
stdout.lockingTextWriter() is used, which only has put().

Should I work on a PR for setBinaryMode+setTextMode ?

thanks,
   Johan
Jan 12 2016
parent reply Jakob Ovrum <jakobovrum gmail.com> writes:
On Wednesday, 13 January 2016 at 00:13:13 UTC, Johan Engelen 
wrote:
 Should I work on a PR for setBinaryMode+setTextMode ?
std.stdio is intended as a wrapper around stdio.h, which I don't think supports setting the mode post-fopen, but if we can support those operations for all practical targets then I think they would be nice additions nevertheless.
Jan 12 2016
parent Johan Engelen <j j.nl> writes:
On Wednesday, 13 January 2016 at 01:48:13 UTC, Jakob Ovrum wrote:
 On Wednesday, 13 January 2016 at 00:13:13 UTC, Johan Engelen 
 wrote:
 Should I work on a PR for setBinaryMode+setTextMode ?
std.stdio is intended as a wrapper around stdio.h, which I don't think supports setting the mode post-fopen
I decided to implement a setRawMode, that basically makes permanent the temporary changes that rawWrite/rawRead do. https://github.com/D-Programming-Language/phobos/pull/3929
Jan 14 2016