diff options
| author | Mitja Felicijan <mitja.felicijan@gmail.com> | 2026-01-21 22:22:16 +0100 |
|---|---|---|
| committer | Mitja Felicijan <mitja.felicijan@gmail.com> | 2026-01-21 22:22:16 +0100 |
| commit | c7ab12bba64d9c20ccd79b132dac475f7bc3923e (patch) | |
| tree | abf2891f9bd1bfa549ed460b288e2c19348bc230 /file.c | |
| parent | 2b3d92e401f0065e440b51da9a6532695b37ef84 (diff) | |
| download | crep-c7ab12bba64d9c20ccd79b132dac475f7bc3923e.tar.gz | |
Re-enable multi-threading
Diffstat (limited to 'file.c')
| -rw-r--r-- | file.c | 81 |
1 files changed, 43 insertions, 38 deletions
@@ -3,42 +3,47 @@ #include "file.h" -struct FileContent read_entire_file(const char* file_path) { - struct FileContent file_data; - file_data.content = NULL; - file_data.count = 0; - - FILE* file = fopen(file_path, "rb"); - if (file == NULL) { - perror("Error opening file"); - return file_data; - } - - fseek(file, 0, SEEK_END); - long file_size = ftell(file); - fseek(file, 0, SEEK_SET); - - if (file_size == -1) { - perror("Error getting file size"); - return file_data; - } - - file_data.content = (const char*)malloc(file_size); - if (file_data.content == NULL) { - perror("Error allocating memory"); - return file_data; - } - - size_t bytes_read = fread((void*)file_data.content, 1, file_size, file); - if (bytes_read != (size_t)file_size) { - perror("Error reading file"); - free((void*)file_data.content); - file_data.content = NULL; - return file_data; - } - - file_data.count = bytes_read; - - fclose(file); - return file_data; +struct FileContent read_entire_file(const char *file_path) { + struct FileContent file_data; + file_data.content = NULL; + file_data.count = 0; + + FILE *file = fopen(file_path, "rb"); + if (file == NULL) { + perror("Error opening file"); + return file_data; + } + + fseek(file, 0, SEEK_END); + long raw_size = ftell(file); + fseek(file, 0, SEEK_SET); + + if (raw_size == -1) { + perror("Error getting file size"); + fclose(file); + return file_data; + } + + size_t file_size = (size_t)raw_size; + char *content = (char *)malloc(file_size + 1); + if (content == NULL) { + perror("Error allocating memory"); + fclose(file); + return file_data; + } + + size_t bytes_read = fread(content, 1, file_size, file); + if (bytes_read != file_size) { + perror("Error reading file"); + free(content); + fclose(file); + return file_data; + } + + content[file_size] = '\0'; + file_data.content = content; + file_data.count = file_size; + + fclose(file); + return file_data; } |
