Skip to content

Commit c383b1a

Browse files
committed
TFile: Add helper functions for serialization of simple data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
1 parent bdd2a7a commit c383b1a

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed

src/ccutil/serialis.cpp

+93
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,99 @@ TFile::~TFile() {
100100
delete data_;
101101
}
102102

103+
bool TFile::DeSerialize(char* buffer, size_t count) {
104+
return FRead(buffer, sizeof(*buffer), count) == count;
105+
}
106+
107+
bool TFile::DeSerialize(double* buffer, size_t count) {
108+
return FReadEndian(buffer, sizeof(*buffer), count) == count;
109+
}
110+
111+
bool TFile::DeSerialize(float* buffer, size_t count) {
112+
return FReadEndian(buffer, sizeof(*buffer), count) == count;
113+
}
114+
115+
bool TFile::DeSerialize(int8_t* buffer, size_t count) {
116+
return FRead(buffer, sizeof(*buffer), count) == count;
117+
}
118+
119+
bool TFile::DeSerialize(int16_t* buffer, size_t count) {
120+
return FReadEndian(buffer, sizeof(*buffer), count) == count;
121+
}
122+
123+
bool TFile::DeSerialize(int32_t* buffer, size_t count) {
124+
return FReadEndian(buffer, sizeof(*buffer), count) == count;
125+
}
126+
127+
bool TFile::DeSerialize(int64_t* buffer, size_t count) {
128+
return FReadEndian(buffer, sizeof(*buffer), count) == count;
129+
}
130+
131+
bool TFile::DeSerialize(uint8_t* buffer, size_t count) {
132+
return FRead(buffer, sizeof(*buffer), count) == count;
133+
}
134+
135+
bool TFile::DeSerialize(uint16_t* buffer, size_t count) {
136+
return FReadEndian(buffer, sizeof(*buffer), count) == count;
137+
}
138+
139+
bool TFile::DeSerialize(uint32_t* buffer, size_t count) {
140+
return FReadEndian(buffer, sizeof(*buffer), count) == count;
141+
}
142+
143+
bool TFile::DeSerialize(uint64_t* buffer, size_t count) {
144+
return FReadEndian(buffer, sizeof(*buffer), count) == count;
145+
}
146+
147+
bool TFile::Serialize(const char* buffer, size_t count) {
148+
return FWrite(buffer, sizeof(*buffer), count) == count;
149+
}
150+
151+
bool TFile::Serialize(const double* buffer, size_t count) {
152+
return FWrite(buffer, sizeof(*buffer), count) == count;
153+
}
154+
155+
bool TFile::Serialize(const float* buffer, size_t count) {
156+
return FWrite(buffer, sizeof(*buffer), count) == count;
157+
}
158+
159+
bool TFile::Serialize(const int8_t* buffer, size_t count) {
160+
return FWrite(buffer, sizeof(*buffer), count) == count;
161+
}
162+
163+
bool TFile::Serialize(const int16_t* buffer, size_t count) {
164+
return FWrite(buffer, sizeof(*buffer), count) == count;
165+
}
166+
167+
bool TFile::Serialize(const int32_t* buffer, size_t count) {
168+
return FWrite(buffer, sizeof(*buffer), count) == count;
169+
}
170+
171+
bool TFile::Serialize(const int64_t* buffer, size_t count) {
172+
return FWrite(buffer, sizeof(*buffer), count) == count;
173+
}
174+
175+
bool TFile::Serialize(const uint8_t* buffer, size_t count) {
176+
return FWrite(buffer, sizeof(*buffer), count) == count;
177+
}
178+
179+
bool TFile::Serialize(const uint16_t* buffer, size_t count) {
180+
return FWrite(buffer, sizeof(*buffer), count) == count;
181+
}
182+
183+
bool TFile::Serialize(const uint32_t* buffer, size_t count) {
184+
return FWrite(buffer, sizeof(*buffer), count) == count;
185+
}
186+
187+
bool TFile::Serialize(const uint64_t* buffer, size_t count) {
188+
return FWrite(buffer, sizeof(*buffer), count) == count;
189+
}
190+
191+
bool TFile::Skip(size_t count) {
192+
offset_ += count;
193+
return true;
194+
}
195+
103196
bool TFile::Open(const STRING& filename, FileReader reader) {
104197
if (!data_is_owned_) {
105198
data_ = new GenericVector<char>;

src/ccutil/serialis.h

+29
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,35 @@ class TFile {
9090
// Sets the value of the swap flag, so that FReadEndian does the right thing.
9191
void set_swap(bool value) { swap_ = value; }
9292

93+
// Deserialize data.
94+
bool DeSerialize(char* data, size_t count = 1);
95+
bool DeSerialize(double* data, size_t count = 1);
96+
bool DeSerialize(float* data, size_t count = 1);
97+
bool DeSerialize(int8_t* data, size_t count = 1);
98+
bool DeSerialize(int16_t* data, size_t count = 1);
99+
bool DeSerialize(int32_t* data, size_t count = 1);
100+
bool DeSerialize(int64_t* data, size_t count = 1);
101+
bool DeSerialize(uint8_t* data, size_t count = 1);
102+
bool DeSerialize(uint16_t* data, size_t count = 1);
103+
bool DeSerialize(uint32_t* data, size_t count = 1);
104+
bool DeSerialize(uint64_t* data, size_t count = 1);
105+
106+
// Serialize data.
107+
bool Serialize(const char* data, size_t count = 1);
108+
bool Serialize(const double* data, size_t count = 1);
109+
bool Serialize(const float* data, size_t count = 1);
110+
bool Serialize(const int8_t* data, size_t count = 1);
111+
bool Serialize(const int16_t* data, size_t count = 1);
112+
bool Serialize(const int32_t* data, size_t count = 1);
113+
bool Serialize(const int64_t* data, size_t count = 1);
114+
bool Serialize(const uint8_t* data, size_t count = 1);
115+
bool Serialize(const uint16_t* data, size_t count = 1);
116+
bool Serialize(const uint32_t* data, size_t count = 1);
117+
bool Serialize(const uint64_t* data, size_t count = 1);
118+
119+
// Skip data.
120+
bool Skip(size_t count);
121+
93122
// Reads a line like fgets. Returns nullptr on EOF, otherwise buffer.
94123
// Reads at most buffer_size bytes, including '\0' terminator, even if
95124
// the line is longer. Does nothing if buffer_size <= 0.

0 commit comments

Comments
 (0)