Can we get rid of non-raw write?
armando sano via Digitalmars-d
digitalmars-d at puremagic.com
Wed Apr 15 16:59:49 PDT 2015
On Wednesday, 15 April 2015 at 17:53:24 UTC, Jürgen Reichmann
wrote:
> On Wednesday, 15 April 2015 at 15:17:27 UTC, Jürgen Reichmann
> wrote:
>> On Wednesday, 15 April 2015 at 14:47:46 UTC, armando sano
>> wrote:
>>> Reviving old topic... It is possible to force stdout to write
>>> in binary mode on Windows, see
>>> https://msdn.microsoft.com/en-us/library/tw4k6df8.aspx
>>>
>>> In C, the solution is:
>>>
>>> -----------------------------
>>> #include <stdio.h>
>>> #include <fcntl.h>
>>> #include <io.h>
>>>
>>> /*...*/
>>>
>>> int result = _setmode( _fileno( stdout ), _O_BINARY );
>>> if ( result == -1 )
>>> perror ("Cannot set stdout to binary mode");
>>> else
>>> perror ("stdout set to binary mode");
>>> ------------------------------
>>>
>>>
>>> In Python, the solution is:
>>>
>>> ------------------------------
>>> import platform
>>> if platform.system() == "Windows":
>>> import os, msvcrt
>>> msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
>>> ------------------------------
>>>
>>> Since D can interface C, it must be possible to do the same
>>> in D? (how, I am not sure)
>>
>> my humble solution:
>>
>> void setFileModeBinary(File f)
>> {
>> import std.c.stdlib;
>> version(Windows) {
>> immutable fd = _fileno(f.getFP);
>> f.flush();
>> _setmode(fd, _O_BINARY);
>> version(DigitalMars) {
>> // @@@BUG@@@ 4243
>> immutable info = __fhnd_info[fd];
>> __fhnd_info[fd] &= ~FHND_TEXT;
>> }
>> }
>> }
>
> Sorry, solution above is no longer valid.
> Version below works for DMD 2.066 and 2.067 (tested for X86).
>
> void setFileModeBinary(File f, bool setBinary = true)
> {
> // extracted from phobos stdio rawWrite
> version(Windows) {
> import std.stdio, std.c.stdlib;
>
> f.flush(); // before changing translation mode
> immutable fd = _fileno(f.getFP);
>
> if (setBinary) {
> _setmode(fd, _O_BINARY);
>
> version(CRuntime_DigitalMars) { // D2.067
> import core.atomic;
>
> // @@@BUG@@@ 4243
> atomicOp!"&="(__fhnd_info[fd], ~FHND_TEXT);
> } else version(DigitalMars) { // D2.066
> version (Win32) {
> import core.atomic;
>
> // @@@BUG@@@ 4243
> atomicOp!"&="(__fhnd_info[fd], ~FHND_TEXT);
> }
> }
> } else {
> version (MICROSOFT_STDIO) {}
> else {
> enum _O_TEXT = 0x4000;
> }
>
> _setmode(fd, _O_TEXT);
>
> version(CRuntime_DigitalMars) { // D2.067
> import core.atomic;
>
> // @@@BUG@@@ 4243
> atomicOp!"&="(__fhnd_info[fd], ~FHND_TEXT);
> } else version(DigitalMars) { // D2.066
> version (Win32) {
> import core.atomic;
>
> // @@@BUG@@@ 4243
> atomicOp!"|="(__fhnd_info[fd], FHND_TEXT);
> }
> }
> }
> }
> }
>
> IMO a function like this belongs in Phobos stdio
>
> jürgen
Thanks for posting your solution Jürgen, works great! I agree it
would be a nice addition to stdio (certainly one I was looking
for for a while)
More information about the Digitalmars-d
mailing list