1 /*
2     Copyright © 2020, Inochi2D Project
3     Distributed under the 2-Clause BSD License, see LICENSE file.
4     
5     Authors: Luna Nielsen
6 */
7 module inochi2d.fmt.io;
8 import std.bitmanip;
9 import std.string;
10 
11 public import std.file;
12 public import std.stdio;
13 
14 /**
15     Reads file value in big endian fashion
16 */
17 T readValue(T)(ref File file) {
18     T value = bigEndianToNative!T(file.rawRead(new ubyte[T.sizeof])[0 .. T.sizeof]);
19     return value;
20 }
21 
22 /**
23     Reads file value in big endian fashion
24 */
25 T peekValue(T)(ref File file) {
26     T val = file.readValue!T;
27     file.skip(-cast(ptrdiff_t)T.sizeof);
28     return val;
29 }
30 
31 /**
32     Reads a string
33 */
34 string readStr(ref File file, uint length) {
35     return cast(string) file.rawRead(new ubyte[length]);
36 }
37 
38 /**
39     Peeks a string
40 */
41 string peekStr(ref File file, uint length) {
42     string val = file.readStr(length);
43     file.seek(-(cast(int)length), SEEK_CUR);
44     return val;
45 }
46 
47 /**
48     Reads values
49 */
50 ubyte[] read(ref File file, size_t length) {
51     return file.rawRead(new ubyte[length]);
52 }
53 
54 /**
55     Peeks values
56 */
57 ubyte[] peek(ref File file, ptrdiff_t length) {
58     ubyte[] result = file.read(length);
59     file.seek(-cast(ptrdiff_t)length, SEEK_CUR);
60     return result;
61 }
62 
63 /**
64     Skips bytes
65 */
66 void skip(ref File file, ptrdiff_t length) {
67     file.seek(cast(ptrdiff_t)length, SEEK_CUR);
68 }