1 module dyaml.stream; 2 3 enum BOM { 4 UTF8, /// UTF-8 5 UTF16LE, /// UTF-16 Little Endian 6 UTF16BE, /// UTF-16 Big Endian 7 UTF32LE, /// UTF-32 Little Endian 8 UTF32BE, /// UTF-32 Big Endian 9 } 10 11 import std.system; 12 13 private enum int NBOMS = 5; 14 immutable Endian[NBOMS] BOMEndian = 15 [ std.system.endian, 16 Endian.littleEndian, Endian.bigEndian, 17 Endian.littleEndian, Endian.bigEndian 18 ]; 19 20 immutable ubyte[][NBOMS] ByteOrderMarks = 21 [ [0xEF, 0xBB, 0xBF], 22 [0xFF, 0xFE], 23 [0xFE, 0xFF], 24 [0xFF, 0xFE, 0x00, 0x00], 25 [0x00, 0x00, 0xFE, 0xFF] 26 ]; 27 28 interface YStream { 29 void writeExact(const void* buffer, size_t size); 30 size_t write(const(ubyte)[] buffer); 31 void flush(); 32 @property bool writeable(); 33 } 34 35 class YMemoryStream : YStream { 36 ubyte[] data; 37 38 void writeExact(const void* buffer, size_t size) { 39 data ~= cast(ubyte[])buffer[0 .. size]; 40 } 41 42 size_t write(const(ubyte)[] buffer) { 43 data ~= buffer; 44 return buffer.length; 45 } 46 47 void flush() {} 48 49 @property bool writeable() { return true; } 50 } 51 52 class YFile : YStream { 53 static import std.stdio; 54 std.stdio.File file; 55 56 this(string fn) { 57 this.file = std.stdio.File(fn, "w"); 58 } 59 60 void writeExact(const void* buffer, size_t size) { 61 this.file.write(cast(const ubyte[])buffer[0 .. size]); 62 } 63 64 size_t write(const(ubyte)[] buffer) { 65 this.file.write(buffer); 66 return buffer.length; 67 } 68 69 void flush() { 70 this.file.flush(); 71 } 72 73 @property bool writeable() { return true; } 74 }