1 module utils.io; 2 import utils; 3 import std.bitmanip; 4 import std..string; 5 6 public import std.file; 7 public import std.stdio; 8 9 /** 10 Reads file value in big endian fashion 11 */ 12 T readValue(T)(ref File file) { 13 size_t currPos = file.tell(); 14 T value = bigEndianToNative!T(file.rawRead(new ubyte[T.sizeof])[0 .. T.sizeof]); 15 return value; 16 } 17 18 /** 19 Reads file value in big endian fashion 20 */ 21 T peekValue(T)(ref File file) { 22 T val = file.readValue!T; 23 file.skip(-cast(ptrdiff_t)T.sizeof); 24 return val; 25 } 26 27 /** 28 Reads a string 29 */ 30 string readStr(ref File file, uint length) { 31 return cast(string) file.rawRead(new ubyte[length]); 32 } 33 34 /** 35 Peeks a string 36 */ 37 string peekStr(ref File file, uint length) { 38 string val = file.readStr(length); 39 file.seek(-(cast(int)length), SEEK_CUR); 40 return val; 41 } 42 43 /** 44 Reads a pascal string 45 */ 46 string readPascalStr(ref File file, uint readTo = 0) { 47 uint length = file.readValue!ubyte; 48 uint extra = readTo > 0 ? readTo-length : 0; 49 if (length == 0) { 50 file.skip(1); 51 return ""; 52 } 53 54 string str = file.readStr(length); 55 if (extra > 0) file.skip(extra); 56 return str[0..length]; 57 } 58 59 /** 60 Reads values 61 */ 62 ubyte[] read(ref File file, size_t length) { 63 return file.rawRead(new ubyte[length]); 64 } 65 66 /** 67 Peeks values 68 */ 69 ubyte[] peek(ref File file, ptrdiff_t length) { 70 ubyte[] result = file.read(length); 71 file.seek(-cast(ptrdiff_t)length, SEEK_CUR); 72 return result; 73 } 74 75 /** 76 Skips bytes 77 */ 78 void skip(ref File file, ptrdiff_t length) { 79 file.seek(cast(ptrdiff_t)length, SEEK_CUR); 80 }