Skip to content

Commit

Permalink
core.stdc.stdio: add experimental support for MSVCRT 14 (VS 2015).
Browse files Browse the repository at this point in the history
  • Loading branch information
kinke committed May 24, 2015
1 parent 41983f8 commit 6a1c9d3
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 40 deletions.
143 changes: 109 additions & 34 deletions src/core/stdc/stdio.d
Expand Up @@ -32,6 +32,9 @@ private
}
}

// uncomment for MSVCRT >= 14 (VS 2015)
//version(LDC) { version(Win64) version = LDC_MSVCRT14; }

extern (C):
@system:
nothrow:
Expand Down Expand Up @@ -77,19 +80,30 @@ else version( CRuntime_Microsoft )
///
FILENAME_MAX = 260,
///
TMP_MAX = 32767,
///
_SYS_OPEN = 20, // non-standard
}

///
enum int _NFILE = 512; // non-standard

version( LDC_MSVCRT14 ) // requires MSVCRT >= 14 (VS 2015)
{
///
enum int TMP_MAX = int.max;
///
enum int L_tmpnam = 260;
}
else
{
///
enum int TMP_MAX = short.max;
///
enum string _P_tmpdir = "\\"; // non-standard
///
enum wstring _wP_tmpdir = "\\"; // non-standard
///
enum int L_tmpnam = _P_tmpdir.length + 12;
}
}
else version( linux )
{
Expand Down Expand Up @@ -259,8 +273,18 @@ version( CRuntime_DigitalMars )
else version( CRuntime_Microsoft )
{
///
alias int fpos_t; //check this
alias long fpos_t;

version( LDC_MSVCRT14 ) // requires MSVCRT >= 14 (VS 2015)
{
///
struct _iobuf
{
void* _Placeholder;
}
}
else
{
///
struct _iobuf
{
Expand All @@ -273,6 +297,7 @@ else version( CRuntime_Microsoft )
int _bufsiz;
char* _tmpfname;
}
}

///
alias shared(_iobuf) FILE;
Expand Down Expand Up @@ -549,38 +574,53 @@ else version( CRuntime_Microsoft )
_IOLBF = 0x40,
///
_IONBF = 4,
}

extern shared void function() _fcloseallp;

version( LDC_MSVCRT14 ) // requires MSVCRT >= 14 (VS 2015)
{
shared(FILE)* __acrt_iob_func(uint);
///
_IOREAD = 1, // non-standard
shared FILE* stdin; // = __acrt_iob_func(0);
///
_IOWRT = 2, // non-standard
shared FILE* stdout; // = __acrt_iob_func(1);
///
_IOMYBUF = 8, // non-standard
shared FILE* stderr; // = __acrt_iob_func(2);
}
else
{
enum
{
///
_IOEOF = 0x10, // non-standard
_IOREAD = 1, // non-standard
///
_IOERR = 0x20, // non-standard
_IOWRT = 2, // non-standard
///
_IOSTRG = 0x40, // non-standard
_IOMYBUF = 8, // non-standard
///
_IORW = 0x80, // non-standard
_IOEOF = 0x10, // non-standard
///
_IOAPP = 0x200, // non-standard
_IOERR = 0x20, // non-standard
///
_IOSTRG = 0x40, // non-standard
///
_IORW = 0x80, // non-standard
///
_IOAPP = 0x200, // non-standard
///
_IOAPPEND = 0x200, // non-standard
}
}

extern shared void function() _fcloseallp;
shared(FILE)* __iob_func();

private extern shared FILE[_NFILE] _iob;

shared(FILE)* __iob_func();

///
shared FILE* stdin; // = &__iob_func()[0];
///
shared FILE* stdout; // = &__iob_func()[1];
///
shared FILE* stderr; // = &__iob_func()[2];
///
shared FILE* stdin; // = &__iob_func()[0];
///
shared FILE* stdout; // = &__iob_func()[1];
///
shared FILE* stderr; // = &__iob_func()[2];
}
}
else version( linux )
{
Expand Down Expand Up @@ -802,6 +842,15 @@ version (MinGW)
}
else
{
version (LDC_MSVCRT14)
{
// MS define the *printf and *scanf function families inline
// starting with VS 2015 and its breaking changes.
// Link against a lib provided by MS containing appropriate
// implementations.
pragma(lib, "legacy_stdio_definitions.lib");
}

///
int fprintf(FILE* stream, in char* format, ...);
///
Expand Down Expand Up @@ -941,21 +990,46 @@ else version( CRuntime_DigitalMars )
}
else version( CRuntime_Microsoft )
{
// No unsafe pointer manipulation.
extern (D) @trusted
version( LDC_MSVCRT14 ) // requires MSVCRT >= 14 (VS 2015)
{
///
void rewind(FILE* stream) { fseek(stream,0L,SEEK_SET); stream._flag = stream._flag & ~_IOERR; }
///
pure void clearerr(FILE* stream) { stream._flag = stream._flag & ~(_IOERR|_IOEOF); }
///
pure int feof(FILE* stream) { return stream._flag&_IOEOF; }
// No unsafe pointer manipulation.
@trusted
{
///
void rewind(FILE* stream);
///
pure void clearerr(FILE* stream);
///
pure int feof(FILE* stream);
///
pure int ferror(FILE* stream);
///
pure int fileno(FILE* stream);
}

///
pure int ferror(FILE* stream) { return stream._flag&_IOERR; }
int vsnprintf(char* s, size_t n, in char* format, va_list args);
///
pure int fileno(FILE* stream) { return stream._file; }
int snprintf(char* s, size_t n, in char* format, ...);
}
///
else
{
// No unsafe pointer manipulation.
extern (D) @trusted
{
///
void rewind(FILE* stream) { fseek(stream, 0L, SEEK_SET); stream._flag = stream._flag & ~_IOERR; }
///
pure void clearerr(FILE* stream) { stream._flag = stream._flag & ~(_IOERR | _IOEOF); }
///
pure int feof(FILE* stream) { return stream._flag & _IOEOF; }
///
pure int ferror(FILE* stream) { return stream._flag & _IOERR; }
///
pure int fileno(FILE* stream) { return stream._file; }
}

///
int _snprintf(char* s, size_t n, in char* fmt, ...);
///
alias _snprintf snprintf;
Expand Down Expand Up @@ -1002,6 +1076,7 @@ else version( CRuntime_Microsoft )
else
return _filbuf(fp);
}
}

///
int _lock_file(FILE *fp);
Expand Down
21 changes: 15 additions & 6 deletions src/rt/dmain2.d
Expand Up @@ -270,13 +270,22 @@ extern (C) int _d_run_main(int argc, char **argv, MainFunc mainFunc)
}
version (CRuntime_Microsoft)
{
auto fp = __iob_func();
stdin = &fp[0];
stdout = &fp[1];
stderr = &fp[2];
static if (__traits(compiles, __acrt_iob_func(0))) // requires MSVCRT >= 14 (VS 2015)
{
stdin = __acrt_iob_func(0);
stdout = __acrt_iob_func(1);
stderr = __acrt_iob_func(2);
}
else
{
auto fp = __iob_func();
stdin = &fp[0];
stdout = &fp[1];
stderr = &fp[2];

// ensure that sprintf generates only 2 digit exponent when writing floating point values
_set_output_format(_TWO_DIGIT_EXPONENT);
// ensure that *printf generates only 2 digit exponent when writing floating point values
_set_output_format(_TWO_DIGIT_EXPONENT);
}

// enable full precision for reals
version(Win64)
Expand Down

0 comments on commit 6a1c9d3

Please sign in to comment.