Debian Codegraph @

src/engine/safeReader.h

raw
1 /**
2 * Furnace Tracker - multi-system chiptune tracker
3 * Copyright (C) 2021-2025 tildearrow and contributors
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef _SAFEREADER_H
21 #define _SAFEREADER_H
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stdint.h>
25 #include "../ta-utils.h"
26
27 enum Endianness {
28 LittleEndian=0,
29 BigEndian
30 };
31
32 enum DivStringEncoding {
33 DIV_ENCODING_NONE=0,
34 DIV_ENCODING_UTF8,
35 DIV_ENCODING_LATIN1,
36 DIV_ENCODING_LATIN1_SPECIAL,
37 DIV_ENCODING_SHIFT_JIS
38 };
39
40 class SafeReader;
41
42 struct EndOfFileException {
43 SafeReader* reader;
44 size_t finalSize;
45 EndOfFileException(SafeReader* r, size_t fs):
46 reader(r),
47 finalSize(fs) {}
48 };
49
50 class SafeReader {
51 const unsigned char* buf;
52 size_t len;
53
54 size_t curSeek;
55
56 public:
57 bool seek(ssize_t where, int whence);
58 size_t tell();
59 size_t size();
60
61 int read(void* where, size_t count);
62
63 // these functions may throw EndOfFileException.
64 signed char readC();
65 short readS();
66 short readS_BE();
67 int readI();
68 int readI_BE();
69 int64_t readL();
70 int64_t readL_BE();
71 float readF();
72 float readF_BE();
73 double readD();
74 double readD_BE();
75 String readStringWithEncoding(DivStringEncoding encoding);
76 String readStringWithEncoding(DivStringEncoding encoding, size_t len);
77 String readString();
78 String readString(size_t len);
79 String readStringLatin1();
80 String readStringLatin1(size_t len);
81 String readStringLatin1Special();
82 String readStringLatin1Special(size_t len);
83 String readStringLine();
84 String readStringToken(unsigned char delim, bool stripContiguous);
85 String readStringToken();
86 inline bool isEOF() { return curSeek >= len; };
87
88 SafeReader(const void* b, size_t l):
89 buf((const unsigned char*)b),
90 len(l),
91 curSeek(0) {}
92 };
93
94 #endif