Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions std/stdio.d
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,56 @@ Throws: $(D ErrnoException) if the file is not opened or if the call to $(D fwri
assert(std.file.read(deleteme) == "\r\n\n\r\n");
}

/**
Sets the $(LREF File) to "raw" mode, such that all subsequent reads and writes will
behave like $(LREF rawRead) and $(LREF rawWrite).

Calls $(LREF flush) (all platforms), and sets the $(D FILE*) to binary mode on Windows.

This can be used to set $(D stdin) and $(D stdout), which are implicitly opened in text
mode, to binary mode on Windows.

See_Also:
$(LREF rawRead)
$(LREF rawWrite)
*/
void setRawMode()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The terminology is binary vs. text. Then we probably need a means to get the current mode, so there's no escaping the API:

enum FileMode { text, binary }
void getMode(FileMode);
void setMode(FileMode);

{
flush(); // before changing translation mode
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope this throws on error.

version(Windows)
{
immutable fd = ._fileno(_p.handle);
._setmode(fd, _O_BINARY);
version(DIGITAL_MARS_STDIO)
{
import core.atomic;
immutable info = __fhnd_info[fd];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

variable not used, remove.

atomicOp!"&="(__fhnd_info[fd], ~FHND_TEXT);
}
}
}

unittest
{
static import std.file;

auto deleteme = testFilename();
auto f = File(deleteme, "w"); // open in text mode
scope(exit) std.file.remove(deleteme);
f.write("\r\n\n\r\n");
f.setRawMode();
f.write("raw\r\n\n\r\n");
f.close();
version(Windows)
{
assert(std.file.read(deleteme) == "\r\r\n\r\n\r\r\nraw\r\n\n\r\n");
}
else
{
assert(std.file.read(deleteme) == "\r\n\n\r\nraw\r\n\n\r\n");
}
}

/**
Calls $(WEB cplusplus.com/reference/clibrary/cstdio/fseek.html, fseek)
for the file handle.
Expand Down