Merge pull request #6 from dlang-tour/yfile-stream-from-existing-file

Add support for writing strings to existing files through YFile stream
This commit is contained in:
Petar Kirov 2016-12-10 20:41:13 +02:00 committed by GitHub
commit 6fc5cc752a

View file

@ -32,6 +32,7 @@ interface YStream
{ {
void writeExact(const void* buffer, size_t size); void writeExact(const void* buffer, size_t size);
size_t write(const(ubyte)[] buffer); size_t write(const(ubyte)[] buffer);
size_t write(const(char)[] str);
void flush(); void flush();
@property bool writeable(); @property bool writeable();
} }
@ -51,6 +52,11 @@ class YMemoryStream : YStream
return buffer.length; return buffer.length;
} }
size_t write(const(char)[] str)
{
return write(cast(const(ubyte)[])str);
}
void flush() {} void flush() {}
@property bool writeable() { return true; } @property bool writeable() { return true; }
@ -66,6 +72,18 @@ class YFile : YStream
this.file = std.stdio.File(fn, "w"); this.file = std.stdio.File(fn, "w");
} }
this(std.stdio.File file)
{
this.file = file;
}
unittest
{
import std.stdio : stdout;
auto stream = new YFile(stdout);
stream.write("Test writing to stdout through YFile stream\n");
}
void writeExact(const void* buffer, size_t size) void writeExact(const void* buffer, size_t size)
{ {
this.file.rawWrite(cast(const) buffer[0 .. size]); this.file.rawWrite(cast(const) buffer[0 .. size]);
@ -77,6 +95,11 @@ class YFile : YStream
return buffer.length; return buffer.length;
} }
size_t write(const(char)[] str)
{
return write(cast(const(ubyte)[])str);
}
void flush() void flush()
{ {
this.file.flush(); this.file.flush();