From 2d49369e7874aedd75708e52493b56362c234805 Mon Sep 17 00:00:00 2001 From: Al Date: Wed, 13 May 2015 17:51:03 -0400 Subject: [PATCH] [utils] Adding read/write for 64-bit ints to file_utils --- src/file_utils.c | 30 ++++++++++++++++++++++++++++++ src/file_utils.h | 3 +++ 2 files changed, 33 insertions(+) diff --git a/src/file_utils.c b/src/file_utils.c index 9cd2d52e..93736d27 100644 --- a/src/file_utils.c +++ b/src/file_utils.c @@ -36,6 +36,36 @@ 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) { + unsigned char buf[8]; + + if (fread(buf, 8, 1, file) == 1) { + *value = ((uint64_t)buf[0] << 56) | + ((uint64_t)buf[1] << 48) | + ((uint64_t)buf[2] << 40) | + ((uint64_t)buf[3] << 32) | + ((uint64_t)buf[4] << 24) | + ((uint64_t)buf[5] << 16) | + ((uint64_t)buf[6] << 8) | + (uint64_t)buf[7]; + return true; + } + return false; +} + +bool file_write_int64(FILE *file, int64_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; + + return (fwrite(buf, 8, 1, file) == 1); +} bool file_read_int32(FILE *file, int32_t *value) { unsigned char buf[4]; diff --git a/src/file_utils.h b/src/file_utils.h index cfa4cc25..b0108598 100644 --- a/src/file_utils.h +++ b/src/file_utils.h @@ -34,6 +34,9 @@ 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_int32(FILE *file, int32_t *value); bool file_write_int32(FILE *file, int32_t value);