From 17f88c3adc1f0cbec23a65dd22dd25c3c0e25cfa Mon Sep 17 00:00:00 2001 From: Al Date: Wed, 27 May 2015 16:03:36 -0400 Subject: [PATCH] [utils] using unsigned ints in file_utils, adding doubles --- src/file_utils.c | 48 ++++++++++++++++++++++++++++++++---------------- src/file_utils.h | 19 +++++++++++-------- src/trie.h | 4 ++-- 3 files changed, 45 insertions(+), 26 deletions(-) diff --git a/src/file_utils.c b/src/file_utils.c index 93736d27..23afb77a 100644 --- a/src/file_utils.c +++ b/src/file_utils.c @@ -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); } diff --git a/src/file_utils.h b/src/file_utils.h index b0108598..450224b2 100644 --- a/src/file_utils.h +++ b/src/file_utils.h @@ -34,17 +34,20 @@ char *file_getline(FILE * f); bool is_relative_path(struct dirent *ent); -bool file_read_int64(FILE *file, int64_t *value); -bool file_write_int64(FILE *file, int64_t value); +bool file_read_uint64(FILE *file, uint64_t *value); +bool file_write_uint64(FILE *file, uint64_t value); -bool file_read_int32(FILE *file, int32_t *value); -bool file_write_int32(FILE *file, int32_t value); +bool file_read_double(FILE *file, double *value); +bool file_write_double(FILE *file, double value); -bool file_read_int16(FILE *file, int16_t *value); -bool file_write_int16(FILE *file, int16_t value); +bool file_read_uint32(FILE *file, uint32_t *value); +bool file_write_uint32(FILE *file, uint32_t value); -bool file_read_int8(FILE *file, int8_t *value); -bool file_write_int8(FILE *file, int8_t value); +bool file_read_uint16(FILE *file, uint16_t *value); +bool file_write_uint16(FILE *file, uint16_t value); + +bool file_read_uint8(FILE *file, uint8_t *value); +bool file_write_uint8(FILE *file, uint8_t value); bool file_read_chars(FILE *file, char *buf, size_t len); bool file_write_chars(FILE *file, const char *buf, size_t len); diff --git a/src/trie.h b/src/trie.h index d404dab7..ef38c3ca 100644 --- a/src/trie.h +++ b/src/trie.h @@ -110,11 +110,11 @@ typedef struct trie_prefix_result { size_t tail_pos; } trie_prefix_result_t; -#define NULL_PREFIX_RESULT (trie_prefix_result_t) {NULL_NODE_ID, 0}; +#define NULL_PREFIX_RESULT (trie_prefix_result_t) {NULL_NODE_ID, 0} trie_prefix_result_t trie_get_prefix(trie_t *self, char *key); trie_prefix_result_t trie_get_prefix_len(trie_t *self, char *key, size_t len); -trie_prefix_result_t trie_get_prefix_from_index(trie_t *self, char *key, size_t len, uint32_t i, size_t tail_pos); +trie_prefix_result_t trie_get_prefix_from_index(trie_t *self, char *key, size_t len, uint32_t start_index, size_t tail_pos); void trie_print(trie_t *self);