-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathpz_io.cpp
More file actions
163 lines (131 loc) · 3.28 KB
/
pz_io.cpp
File metadata and controls
163 lines (131 loc) · 3.28 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
* IO Utils.
* vim: ts=4 sw=4 et
*
* Copyright (C) Plasma Team
* Distributed under the terms of the MIT license, see ../LICENSE.code
*/
#include <stdio.h>
#include <stdlib.h>
#include "pz_common.h"
#include "pz_io.h"
namespace pz {
BinaryInput::~BinaryInput()
{
if (m_file) {
assert(!m_filename.empty());
if (ferror(m_file)) {
perror(m_filename.c_str());
} else if (feof(m_file)) {
fprintf(
stderr, "%s: Unexpected end of file.\n", m_filename.c_str());
}
close();
}
assert(!m_file);
assert(m_filename.empty());
}
bool BinaryInput::open(const std::string & filename)
{
assert(!m_file);
assert(m_filename.empty());
m_file = fopen(filename.c_str(), "rb");
if (m_file) {
m_filename = std::string(filename);
return true;
} else {
return false;
}
}
void BinaryInput::close()
{
assert(m_file);
fclose(m_file);
m_file = nullptr;
assert(!m_filename.empty());
m_filename.clear();
}
const std::string & BinaryInput::filename() const
{
return m_filename;
}
const char * BinaryInput::filename_c() const
{
return filename().c_str();
}
bool BinaryInput::seek_set(long pos)
{
assert(pos >= 0);
return fseek(m_file, pos, SEEK_SET) == 0;
}
bool BinaryInput::seek_cur(long pos)
{
return fseek(m_file, pos, SEEK_CUR) == 0;
}
Optional<unsigned long> BinaryInput::tell() const
{
long pos = ftell(m_file);
if (pos < 0) {
return Optional<unsigned long>::Nothing();
} else {
return Optional<unsigned long>(pos);
}
}
bool BinaryInput::is_at_eof()
{
return !!feof(m_file);
}
bool BinaryInput::read_uint8(uint8_t * value)
{
return (1 == fread(value, sizeof(uint8_t), 1, m_file));
}
bool BinaryInput::read_uint16(uint16_t * value)
{
uint8_t bytes[2];
if (!fread(bytes, sizeof(uint8_t), 2, m_file)) {
return false;
}
*value = ((uint16_t)bytes[1] << 8) | (uint16_t)bytes[0];
return true;
}
bool BinaryInput::read_uint32(uint32_t * value)
{
uint8_t bytes[4];
if (!fread(bytes, sizeof(uint8_t), 4, m_file)) {
return false;
}
*value = ((uint32_t)bytes[3] << 24) | ((uint32_t)bytes[2] << 16) |
((uint32_t)bytes[1] << 8) | (uint32_t)bytes[0];
return true;
}
bool BinaryInput::read_uint64(uint64_t * value)
{
uint8_t bytes[8];
if (!fread(bytes, sizeof(uint8_t), 8, m_file)) {
return false;
}
*value = ((uint64_t)bytes[7] << 56) | ((uint64_t)bytes[6] << 48) |
((uint64_t)bytes[5] << 40) | ((uint64_t)bytes[4] << 32) |
((uint64_t)bytes[3] << 24) | ((uint64_t)bytes[2] << 16) |
((uint64_t)bytes[1] << 8) | (uint64_t)bytes[0];
return true;
}
Optional<String> BinaryInput::read_len_string(GCCapability & gc_cap)
{
uint16_t len;
if (!read_uint16(&len)) {
return Optional<String>::Nothing();
}
return read_string(gc_cap, len);
}
Optional<String> BinaryInput::read_string(GCCapability & gc_cap, uint16_t len)
{
FlatString *str;
str = FlatString::New(gc_cap, len);
if (len != fread(str->buffer(), sizeof(char), len, m_file)) {
return Optional<String>::Nothing();
}
str->buffer()[len] = 0;
return Optional<String>(String(str));
}
} // namespace pz