Debian Codegraph @

terps/bocfel/io.h

raw symbols SCIP
1 // vim: set ft=cpp:
2
3 #ifndef ZTERP_IO_H
4 #define ZTERP_IO_H
5
6 #include <cstdio>
7 #include <exception>
8 #include <functional>
9 #include <memory>
10 #include <string>
11 #include <type_traits>
12 #include <utility>
13 #include <vector>
14
15 #ifdef ZTERP_GLK
16 extern "C" {
17 #include <glk.h>
18 }
19 #endif
20
21 #include "types.h"
22
23 enum class StreamRock : uint32_t {
24 None = 0,
25 BlorbStream = 1,
26 TranscriptStream = 2,
27 };
28
29 class IO {
30 public:
31 class Error : public std::exception {
32 };
33
34 class OpenError : public Error {
35 };
36
37 class IOError : public Error {
38 };
39
40 class EndOfFile : public Error {
41 };
42
43 enum class Mode {
44 ReadOnly,
45 WriteOnly,
46 Append,
47 };
48
49 enum class Purpose {
50 Data,
51 Save,
52 Transcript,
53 Input,
54 };
55
56 enum class SeekFrom {
57 Start,
58 End,
59 Current,
60 };
61
62 IO(const std::string *filename, Mode mode, Purpose purpose, StreamRock namedglkrock = StreamRock::None);
63 IO(std::vector<uint8_t> buf, Mode mode);
64 #ifdef ZTERP_GLK
65 IO(Mode mode, Purpose purpose, strid_t stream) : m_file(stream), m_type(Type::Glk), m_mode(mode), m_purpose(purpose) {
66 }
67 #endif
68
69 void operator=(IO const &) = delete;
70 IO(const IO &) = delete;
71 IO(IO &&) = default;
72
73 static IO standard_in() {
74 return IO(stdin, Mode::ReadOnly, Purpose::Input, false);
75 }
76
77 static IO standard_out() {
78 return IO(stdout, Mode::WriteOnly, Purpose::Transcript, false);
79 }
80
81 const std::vector<uint8_t> &get_memory() const;
82
83 void seek(long offset, SeekFrom whence);
84 long tell() const;
85 size_t read(void *buf, size_t n);
86 void read_exact(void *buf, size_t n);
87 size_t write(const void *buf, size_t n);
88 void write_exact(const void *buf, size_t n);
89 uint8_t read8();
90 uint16_t read16();
91 uint32_t read32();
92 void write8(uint8_t v);
93 void write16(uint16_t v);
94 void write32(uint32_t v);
95 long getc(bool limit16);
96 void putc(uint32_t c);
97 std::vector<uint16_t> readline();
98 long filesize() const;
99 void flush();
100
101 private:
102 struct Backing {
103 std::vector<uint8_t> memory;
104 long offset = 0;
105
106 Backing() = default;
107
108 explicit Backing(std::vector<uint8_t> buf) : memory(std::move(buf)) {
109 }
110 };
111
112 enum class Type {
113 StandardIO,
114 Memory,
115 #ifdef ZTERP_GLK
116 Glk,
117 #endif
118 };
119
120 using StdioType = std::unique_ptr<std::FILE, std::function<void(std::FILE *)>>;
121
122 #ifdef ZTERP_GLK
123 using GlkType = std::unique_ptr<std::remove_pointer_t<strid_t>, std::function<void(strid_t)>>;
124 #endif
125
126 struct File {
127 StdioType stdio;
128 Backing backing;
129 #ifdef ZTERP_GLK
130 GlkType glk;
131 #endif
132
133 File() = default;
134
135 File(std::FILE *fp, bool close) : stdio(StdioType(fp, [close](std::FILE *fp_) { if (close) { std::fclose(fp_); } })) {
136 }
137
138 explicit File(std::vector<uint8_t> buf) : backing(std::move(buf)) {
139 }
140
141 #ifdef ZTERP_GLK
142 explicit File(strid_t stream) : glk(GlkType(stream, [](strid_t str) { glk_stream_close(str, nullptr); })) {
143 }
144 #endif
145 };
146
147 IO(std::FILE *fp, Mode mode, Purpose purpose, bool close) : m_file(fp, close), m_type(Type::StandardIO), m_mode(mode), m_purpose(purpose) {
148 }
149
150 [[noreturn]] void bad_type() const;
151 bool textmode() const;
152
153 #ifdef ZTERP_GLK
154 void open_as_glk(const std::function<frefid_t(glui32 usage, glui32 filemode)> &create_fref, StreamRock rock);
155 #endif
156
157 File m_file;
158 Type m_type;
159 Mode m_mode;
160 Purpose m_purpose;
161 };
162
163 #endif