28 return fread(data,
sizeof(*data), n, fp) == n;
32 return fread(data,
sizeof(*data), n, fp) == n;
36 return fread(data,
sizeof(*data), n, fp) == n;
40 return fread(data,
sizeof(*data), n, fp) == n;
44 return fread(data,
sizeof(*data), n, fp) == n;
48 return fread(data,
sizeof(*data), n, fp) == n;
52 return fread(data,
sizeof(*data), n, fp) == 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, data_);
212 if (!data_is_owned_) {
214 data_is_owned_ =
true;
219 memcpy(&(*data_)[0], data, size);
225 long current_pos = 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);
323 size_t total = size *
count;
324 const char* buf =
static_cast<const char*
>(buffer);
327 for (
size_t i = 0; i < total; ++i)
int FWrite(const void *buffer, size_t size, int count)
void resize_no_init(int size)
int FRead(void *buffer, size_t size, int count)
void OpenWrite(GenericVector< char > *data)
bool SaveDataToFile(const GenericVector< char > &data, const STRING &filename)
bool DeSerialize(char *data, size_t count=1)
char * FGets(char *buffer, int buffer_size)
bool Serialize(FILE *fp, const char *data, size_t n)
void ReverseN(void *ptr, int num_bytes)
int FReadEndian(void *buffer, size_t size, int count)
bool Serialize(const char *data, size_t count=1)
bool(* FileReader)(const STRING &filename, GenericVector< char > *data)
bool Open(const STRING &filename, FileReader reader)
bool CloseWrite(const STRING &filename, FileWriter writer)
bool LoadDataFromFile(const char *filename, GenericVector< char > *data)
bool DeSerialize(FILE *fp, char *data, size_t n)
bool(* FileWriter)(const GenericVector< char > &data, const STRING &filename)