-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathpz_io.h
110 lines (87 loc) · 2.25 KB
/
pz_io.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
* IO Utils.
* vim: ts=4 sw=4 et
*
* Copyright (C) Plasma Team
* Distributed under the terms of the MIT license, see ../LICENSE.code
*/
#ifndef IO_UTILS_H
#define IO_UTILS_H
#include <string>
#include "pz_cxx_future.h"
#include "pz_gc_util.h"
#include "pz_string.h"
namespace pz {
/*
* A binary input file, this is a wrapper around a FILE pointer. Internally
* we use the C API rather than C++ since the C one is simple to use for
* binary data.
*
* Since it wraps the C FILE structure a failing operation will set errno.
* Callers should check errno directly.
*/
class BinaryInput
{
private:
FILE * m_file;
std::string m_filename;
public:
BinaryInput() : m_file(nullptr), m_filename() {}
/*
* For normal/happy paths, you must call close() before the destructor
* runs. The destructor will treat the file being open as an error and
* report information about the file's state.
*/
~BinaryInput();
/*
* Open a file.
*/
bool open(const std::string & filename);
/*
* Close the file.
*/
void close();
/*
* The current file's name.
*/
const std::string & filename() const;
const char * filename_c() const;
/*
* Read an 8bit unsigned integer.
*/
bool read_uint8(uint8_t * value);
/*
* Read a 16bit unsigned integer.
*/
bool read_uint16(uint16_t * value);
/*
* Read a 32bit unsigned integer.
*/
bool read_uint32(uint32_t * value);
/*
* Read a 64bit unsigned integer.
*/
bool read_uint64(uint64_t * value);
/*
* Read a length (16 bits) followed by a string of that length.
*/
Optional<String> read_len_string(GCCapability & gc_cap);
/*
* Read a string of the given length from the stream.
*/
Optional<String> read_string(GCCapability & gc_cap, uint16_t len);
/*
* seek relative to beginning of file.
*/
bool seek_set(long pos);
/*
* seek relative to current position.
*/
bool seek_cur(long pos);
Optional<unsigned long> tell() const;
bool is_at_eof();
BinaryInput(const BinaryInput &) = delete;
void operator=(const BinaryInput &) = delete;
};
} // namespace pz
#endif /* ! IO_UTILS_H */