April 15, 2015
On 4/15/15 10:47 AM, 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");
> ------------------------------

Just a warning, "binary mode" is a C feature, not an OS feature. So you have to call the functions that are relevant to the C library you are using. On Windows, this could be DMC or MSVCRT. I'm not 100% sure the DMC way would be the same as above.

-Steve
April 15, 2015
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;
		}
	}
}
April 15, 2015
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
April 15, 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)
July 07, 2017
Vetoed after several years of nothing:
https://issues.dlang.org/show_bug.cgi?id=9776#c7

I'm getting really fucking tired of D making up excuses to throw "do the right thing by default" straight into the gutter. D steering didn't used to be this way, and that was exactly what make D into something worthwhile in the first place. Now we're just diving head-first into C++-management (minus the committes).
1 2 3
Next ›   Last »