[utils] using unsigned ints in file_utils, adding doubles
This commit is contained in:
@@ -36,7 +36,7 @@ bool is_relative_path(struct dirent *ent) {
|
||||
return strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0;
|
||||
}
|
||||
|
||||
bool file_read_int64(FILE *file, int64_t *value) {
|
||||
bool file_read_uint64(FILE *file, uint64_t *value) {
|
||||
unsigned char buf[8];
|
||||
|
||||
if (fread(buf, 8, 1, file) == 1) {
|
||||
@@ -53,21 +53,37 @@ bool file_read_int64(FILE *file, int64_t *value) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool file_write_int64(FILE *file, int64_t value) {
|
||||
bool file_write_uint64(FILE *file, uint64_t value) {
|
||||
unsigned char buf[8];
|
||||
buf[0] = ((value >> 56) & 0xff);
|
||||
buf[1] = ((value >> 48) & 0xff);
|
||||
buf[2] = ((value >> 40) & 0xff);
|
||||
buf[3] = ((value >> 32) & 0xff);
|
||||
buf[4] = ((value >> 24) & 0xff);
|
||||
buf[5] = ((value >> 16) & 0xff);
|
||||
buf[6] = ((value >> 8) & 0xff);
|
||||
buf[7] = value & 0xff;
|
||||
buf[0] = ((uint8_t)(value >> 56) & 0xff);
|
||||
buf[1] = ((uint8_t)(value >> 48) & 0xff);
|
||||
buf[2] = ((uint8_t)(value >> 40) & 0xff);
|
||||
buf[3] = ((uint8_t)(value >> 32) & 0xff);
|
||||
buf[4] = ((uint8_t)(value >> 24) & 0xff);
|
||||
buf[5] = ((uint8_t)(value >> 16) & 0xff);
|
||||
buf[6] = ((uint8_t)(value >> 8) & 0xff);
|
||||
buf[7] = (uint8_t)(value & 0xff);
|
||||
|
||||
return (fwrite(buf, 8, 1, file) == 1);
|
||||
}
|
||||
|
||||
bool file_read_int32(FILE *file, int32_t *value) {
|
||||
|
||||
bool file_read_double(FILE *file, double *value) {
|
||||
uint64_t int_val;
|
||||
if (!file_read_uint64(file, &int_val)) {
|
||||
return false;
|
||||
}
|
||||
memcpy(value, &int_val, sizeof(*value));
|
||||
return true;
|
||||
}
|
||||
|
||||
bool file_write_double(FILE *file, double value) {
|
||||
uint64_t int_val;
|
||||
memcpy(&int_val, &value, sizeof(value));
|
||||
return file_write_uint64(file, int_val);
|
||||
}
|
||||
|
||||
bool file_read_uint32(FILE *file, uint32_t *value) {
|
||||
unsigned char buf[4];
|
||||
|
||||
if (fread(buf, 4, 1, file) == 1) {
|
||||
@@ -77,7 +93,7 @@ bool file_read_int32(FILE *file, int32_t *value) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool file_write_int32(FILE *file, int32_t value) {
|
||||
bool file_write_uint32(FILE *file, uint32_t value) {
|
||||
unsigned char buf[4];
|
||||
buf[0] = (value >> 24) & 0xff;
|
||||
buf[1] = (value >> 16) & 0xff;
|
||||
@@ -87,7 +103,7 @@ bool file_write_int32(FILE *file, int32_t value) {
|
||||
return (fwrite(buf, 4, 1, file) == 1);
|
||||
}
|
||||
|
||||
bool file_read_int16(FILE *file, int16_t *value) {
|
||||
bool file_read_uint16(FILE *file, uint16_t *value) {
|
||||
unsigned char buf[2];
|
||||
|
||||
if (fread(buf, 2, 1, file) == 1) {
|
||||
@@ -98,7 +114,7 @@ bool file_read_int16(FILE *file, int16_t *value) {
|
||||
|
||||
}
|
||||
|
||||
bool file_write_int16(FILE *file, int16_t value) {
|
||||
bool file_write_uint16(FILE *file, uint16_t value) {
|
||||
unsigned char buf[2];
|
||||
|
||||
buf[0] = value >> 8;
|
||||
@@ -107,11 +123,11 @@ bool file_write_int16(FILE *file, int16_t value) {
|
||||
return (fwrite(buf, 2, 1, file) == 1);
|
||||
}
|
||||
|
||||
bool file_read_int8(FILE *file, int8_t *value) {
|
||||
bool file_read_uint8(FILE *file, uint8_t *value) {
|
||||
return (fread(value, sizeof(int8_t), 1, file) == 1);
|
||||
}
|
||||
|
||||
bool file_write_int8(FILE *file, int8_t value) {
|
||||
bool file_write_uint8(FILE *file, uint8_t value) {
|
||||
return (fwrite(&value, sizeof(int8_t), 1, file) == 1);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user