28 return fread(data,
sizeof(*data), n, fp) == n;
32 return fread(data,
sizeof(*data), n, fp) == n;
35 bool DeSerialize(FILE* fp, int8_t* data,
size_t n) {
36 return fread(data,
sizeof(*data), n, fp) == n;
39 bool DeSerialize(FILE* fp, int16_t* data,
size_t n) {
40 return fread(data,
sizeof(*data), n, fp) == n;
43 bool DeSerialize(FILE* fp, int32_t* data,
size_t n) {
44 return fread(data,
sizeof(*data), n, fp) == n;
47 bool DeSerialize(FILE* fp, uint8_t* data,
size_t n) {
48 return fread(data,
sizeof(*data), n, fp) == n;
51 bool DeSerialize(FILE* fp, uint16_t* data,
size_t n) {
52 return fread(data,
sizeof(*data), n, fp) == n;
55 bool DeSerialize(FILE* fp, uint32_t* data,
size_t n) {
56 return fread(data,
sizeof(*data), n, fp) == n;
59 bool Serialize(FILE* fp,
const char* data,
size_t n) {
60 return fwrite(data,
sizeof(*data), n, fp) == n;
63 bool Serialize(FILE* fp,
const float* data,
size_t n) {
64 return fwrite(data,
sizeof(*data), n, fp) == n;
67 bool Serialize(FILE* fp,
const int8_t* data,
size_t n) {
68 return fwrite(data,
sizeof(*data), n, fp) == n;
71 bool Serialize(FILE* fp,
const int16_t* data,
size_t n) {
72 return fwrite(data,
sizeof(*data), n, fp) == n;
75 bool Serialize(FILE* fp,
const int32_t* data,
size_t n) {
76 return fwrite(data,
sizeof(*data), n, fp) == n;
79 bool Serialize(FILE* fp,
const uint8_t* data,
size_t n) {
80 return fwrite(data,
sizeof(*data), n, fp) == n;
83 bool Serialize(FILE* fp,
const uint16_t* data,
size_t n) {
84 return fwrite(data,
sizeof(*data), n, fp) == n;
87 bool Serialize(FILE* fp,
const uint32_t* data,
size_t n) {
88 return fwrite(data,
sizeof(*data), n, fp) == n;
94 data_is_owned_(false),
197 if (!data_is_owned_) {
199 data_is_owned_ =
true;
204 if (reader ==
nullptr)
207 return (*reader)(filename.
c_str(), data_);
212 if (!data_is_owned_) {
214 data_is_owned_ =
true;
219 memcpy(&(*data_)[0], data, size);
225 auto current_pos = std::ftell(fp);
226 if (current_pos < 0) {
230 if (end_offset < 0) {
231 if (fseek(fp, 0, SEEK_END))
233 end_offset = ftell(fp);
234 if (fseek(fp, current_pos, SEEK_SET))
237 int size = end_offset - current_pos;
240 if (!data_is_owned_) {
242 data_is_owned_ =
true;
245 return static_cast<int>(fread(&(*data_)[0], 1, size, fp)) == size;
251 while (size + 1 < buffer_size && offset_ < data_->size()) {
252 buffer[size++] = (*data_)[offset_++];
253 if ((*data_)[offset_ - 1] ==
'\n')
break;
255 if (size < buffer_size) buffer[size] =
'\0';
256 return size > 0 ? buffer :
nullptr;
262 char* char_buffer = static_cast<char*>(buffer);
263 for (
int i = 0; i < num_read; ++i, char_buffer += size) {
274 size_t required_size;
275 if (SIZE_MAX / size <=
count) {
277 required_size = data_->
size() - offset_;
279 required_size = size *
count;
280 if (data_->
size() - offset_ < required_size) {
281 required_size = data_->
size() - offset_;
284 if (required_size > 0 && buffer !=
nullptr)
285 memcpy(buffer, &(*data_)[offset_], required_size);
286 offset_ += required_size;
287 return required_size / size;
297 if (data !=
nullptr) {
298 if (data_is_owned_)
delete data_;
300 data_is_owned_ =
false;
301 }
else if (!data_is_owned_) {
303 data_is_owned_ =
true;
312 if (writer ==
nullptr)
315 return (*writer)(*data_, filename.
c_str());
323 size_t total = size *
count;
324 const char* buf = static_cast<const char*>(buffer);
327 for (
size_t i = 0; i < total; ++i)