feat(file): implemented getting file size

This commit is contained in:
Mineplay5780 2025-12-17 20:40:01 +01:00
parent 84b4f0dc59
commit 79e9bf2a8c
2 changed files with 24 additions and 1 deletions

View file

@ -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);

View file

@ -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;
}