Debian Codegraph @

nall/beat/base.hpp

raw
1 #ifndef NALL_BEAT_BASE_HPP
2 #define NALL_BEAT_BASE_HPP
3
4 namespace nall {
5
6 struct beatBase {
7 protected:
8 file fp;
9 uint32_t checksum;
10
11 void ls(lstring& list, const string& path, const string& basepath) {
12 lstring paths = directory::folders(path);
13 for(auto& pathname : paths) {
14 list.append(string{path, pathname}.ltrim<1>(basepath));
15 ls(list, {path, pathname}, basepath);
16 }
17
18 lstring files = directory::files(path);
19 for(auto& filename : files) {
20 list.append(string{path, filename}.ltrim<1>(basepath));
21 }
22 }
23
24 void write(uint8_t data) {
25 fp.write(data);
26 checksum = crc32_adjust(checksum, data);
27 }
28
29 void writeNumber(uint64_t data) {
30 while(true) {
31 uint64_t x = data & 0x7f;
32 data >>= 7;
33 if(data == 0) return write(0x80 | x);
34 write(x);
35 data--;
36 }
37 }
38
39 void writeString(const string& text) {
40 unsigned length = text.length();
41 for(unsigned n = 0; n < length; n++) write(text[n]);
42 }
43
44 void writeChecksum(uint32_t checksum) {
45 write(checksum >> 0);
46 write(checksum >> 8);
47 write(checksum >> 16);
48 write(checksum >> 24);
49 }
50
51 uint8_t read() {
52 uint8_t data = fp.read();
53 checksum = crc32_adjust(checksum, data);
54 return data;
55 }
56
57 uint64_t readNumber() {
58 uint64_t data = 0, shift = 1;
59 while(true) {
60 uint8_t x = read();
61 data += (x & 0x7f) * shift;
62 if(x & 0x80) break;
63 shift <<= 7;
64 data += shift;
65 }
66 return data;
67 }
68
69 string readString(unsigned length) {
70 string text;
71 text.reserve(length + 1);
72 for(unsigned n = 0; n < length; n++) {
73 text[n] = fp.read();
74 checksum = crc32_adjust(checksum, text[n]);
75 }
76 text[length] = 0;
77 return text;
78 }
79
80 bool readChecksum(uint32_t source) {
81 uint32_t checksum = 0;
82 checksum |= read() << 0;
83 checksum |= read() << 8;
84 checksum |= read() << 16;
85 checksum |= read() << 24;
86 return checksum == source;
87 }
88 };
89
90 }
91
92 #endif