diff --git a/source/dyaml/emitter.d b/source/dyaml/emitter.d index 19cbd5f..d53c668 100644 --- a/source/dyaml/emitter.d +++ b/source/dyaml/emitter.d @@ -253,7 +253,7 @@ struct Emitter stream_.writeExact(buffer.ptr, buffer.length * dchar.sizeof); break; } - catch(WriteException e) + catch(Exception e) { throw new Error("Unable to write to stream: " ~ e.msg); } diff --git a/source/dyaml/reader.d b/source/dyaml/reader.d index 1f1d074..b2a25a4 100644 --- a/source/dyaml/reader.d +++ b/source/dyaml/reader.d @@ -993,7 +993,7 @@ void testEndian(R)() void testPeekPrefixForward(R)() { - import std.stream; + import dyaml.stream; writeln(typeid(R).toString() ~ ": peek/prefix/forward unittest"); ubyte[] data = ByteOrderMarks[BOM.UTF8] ~ cast(ubyte[])"data"; auto reader = new R(data); @@ -1011,7 +1011,7 @@ void testPeekPrefixForward(R)() void testUTF(R)() { - import std.stream; + import dyaml.stream; writeln(typeid(R).toString() ~ ": UTF formats unittest"); dchar[] data = cast(dchar[])"data"; void utf_test(T)(T[] data, BOM bom) diff --git a/source/dyaml/stream.d b/source/dyaml/stream.d index 91ddc16..01fd4af 100644 --- a/source/dyaml/stream.d +++ b/source/dyaml/stream.d @@ -1,8 +1,35 @@ module dyaml.stream; +enum BOM { + UTF8, /// UTF-8 + UTF16LE, /// UTF-16 Little Endian + UTF16BE, /// UTF-16 Big Endian + UTF32LE, /// UTF-32 Little Endian + UTF32BE, /// UTF-32 Big Endian +} + +import std.system; + +private enum int NBOMS = 5; +immutable Endian[NBOMS] BOMEndian = +[ std.system.endian, + Endian.littleEndian, Endian.bigEndian, + Endian.littleEndian, Endian.bigEndian + ]; + +immutable ubyte[][NBOMS] ByteOrderMarks = +[ [0xEF, 0xBB, 0xBF], + [0xFF, 0xFE], + [0xFE, 0xFF], + [0xFF, 0xFE, 0x00, 0x00], + [0x00, 0x00, 0xFE, 0xFF] + ]; + interface YStream { void writeExact(const void* buffer, size_t size); size_t write(const(ubyte)[] buffer); + void flush(); + @property bool writeable(); } class YMemoryStream : YStream { @@ -16,6 +43,10 @@ class YMemoryStream : YStream { data ~= buffer; return buffer.length; } + + void flush() {} + + @property bool writeable() { return true; } } class YFile : YStream { @@ -27,10 +58,17 @@ class YFile : YStream { } void writeExact(const void* buffer, size_t size) { - this.file.write(buffer[0 .. size]); + this.file.write(cast(const ubyte[])buffer[0 .. size]); } size_t write(const(ubyte)[] buffer) { - this.file.write(buffer[0 .. size]); + this.file.write(buffer); + return buffer.length; } + + void flush() { + this.file.flush(); + } + + @property bool writeable() { return true; } } diff --git a/source/dyaml/testemitter.d b/source/dyaml/testemitter.d index 1adbe37..fd320ec 100644 --- a/source/dyaml/testemitter.d +++ b/source/dyaml/testemitter.d @@ -15,6 +15,7 @@ import std.file; import std.range; import std.typecons; +import dyaml.stream; import dyaml.dumper; import dyaml.event; import dyaml.testcommon; diff --git a/source/dyaml/testrepresenter.d b/source/dyaml/testrepresenter.d index ee60ec9..5690110 100644 --- a/source/dyaml/testrepresenter.d +++ b/source/dyaml/testrepresenter.d @@ -48,7 +48,9 @@ void testRepresenterTypes(bool verbose, string codeFilename) } } - auto emitStream = new MemoryStream; + import dyaml.stream; + + auto emitStream = new YMemoryStream; auto representer = new Representer; representer.addRepresenter!TestClass(&representClass); representer.addRepresenter!TestStruct(&representStruct);