diff --git a/include/cosms-core/file.h b/include/cosms-core/file.h index cdd5adc..eef6e6b 100644 --- a/include/cosms-core/file.h +++ b/include/cosms-core/file.h @@ -47,6 +47,8 @@ struct cosms_core_file { CosmsCoreFileError cosms_core_file_open(struct cosms_core_file *file, const char *path, int mode); CosmsCoreFileError cosms_core_file_close(struct cosms_core_file *file); +CosmsCoreFileError cosms_core_file_get_size(struct cosms_core_file *file, unsigned long long *size); + CosmsCoreFileError cosms_core_file_read(const char *path, char **content, unsigned long long *size); CosmsCoreFileError cosms_core_file_write(const char *path, const char *content, unsigned long long size, int mode); diff --git a/src/file.c b/src/file.c index 381427a..2b871e2 100644 --- a/src/file.c +++ b/src/file.c @@ -135,6 +135,28 @@ CosmsCoreFileError cosms_core_file_close(struct cosms_core_file *file) { return COSMS_CORE_FILE_OK; } +CosmsCoreFileError cosms_core_file_get_size(unsigned long long *size) { + #if defined(__GNUC__) + struct stat file_stat; + if (stat(path, &file_stat) != 0) { + return COSMS_CORE_FILE_COULD_NOT_READ_SIZE; + } + + (*size) = file_stat.st_size; + #elif defined(_WIN32) + DWORD high_size; + DWORD low_size = GetFileSize(file, &high_size); + if (low_size == INVALID_FILE_SIZE && GetLastError() != NO_ERROR) { + CloseHandle(file); + return COSMS_CORE_FILE_COULD_NOT_READ_SIZE; + } + + (*size) = ((unsigned long long)high_size << 32) | low_size; + #endif + + return COSMS_CORE_FILE_OK; +} + CosmsCoreFileError cosms_core_file_read(const char *path, char **content, unsigned long long *size) { #if defined(__GNUC__) struct stat file_stat; @@ -225,7 +247,6 @@ CosmsCoreFileError cosms_core_file_read(const char *path, char **content, unsign } unsigned long long file_size = ((unsigned long long)high_size << 32) | low_size; - if (size != NULL) { *size = file_size; }