[m-rev.] for review: binary standard input/output streams on Windows

Julien Fischer juliensf at csse.unimelb.edu.au
Fri Apr 20 12:16:27 AEST 2012


On Fri, 20 Apr 2012, Peter Wang wrote:

> Branches: main, 11.07
>
> The binary input and output streams were not necessarily put into binary
> translation mode when using MinGW, and probably MSVC.
>
> library/io.m:
> 	Fix the problem by using _setmode() to change the translation
> 	mode after the file is opened.
>
> tests/hard_coded/Mmakefile:
> tests/hard_coded/binary_stdin.exp:
> tests/hard_coded/binary_stdin.inp:
> tests/hard_coded/binary_stdin.m:
> tests/hard_coded/binary_stdout.exp:
> tests/hard_coded/binary_stdout.m:
> 	Add test cases.
>
> diff --git a/library/io.m b/library/io.m
> index 9a13c75..5d12eb3 100644
> --- a/library/io.m
> +++ b/library/io.m
> @@ -6300,6 +6300,22 @@ MR_Unsigned mercury_current_text_output_index;
> MR_Unsigned mercury_current_binary_input_index;
> MR_Unsigned mercury_current_binary_output_index;
>
> +static void
> +mercury_set_binary_mode(FILE *f)
> +{
> +#if defined(MR_MSVC) || defined(MR_MINGW)
> +    /*
> +    ** Calling fdopen with 'b' in the mode string does not necessarily put the
> +    ** file into binary translation mode on Windows.

According to MSDN
(<http://msdn.microsoft.com/en-us/library/dye30d82.aspx), having 'b' in
the mode string _should_ do that.  (We should probably be using _fdopen
with MSVC rather than fdopen since the latter is deprecated, but that's
another issue.)  Is this just (another) case of Microsoft's API's
behaving in a completely stupid way?

>      This is the case with
> +    ** MinGW and, reportedly, MSVC. The cause is likely the MSVC CRT.
> +    ** The solution is to change the mode on the file after opening.
> +    */
> +    _setmode(_fileno(f), _O_BINARY);
> +#else
> +    (void)f;
> +#endif

Why #else branch here?

Julien.
--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to:       mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions:          mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the reviews mailing list