From 84b4f0dc59712e3c821977f960f348e424f386fa Mon Sep 17 00:00:00 2001 From: Mineplay5780 Date: Wed, 17 Dec 2025 20:36:35 +0100 Subject: [PATCH] refactor(file): removed boolean override for mode in write function --- include/cosms-core/file.h | 4 +--- src/file.c | 14 +++++++------- tests/unit/file.c | 8 ++++---- 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/include/cosms-core/file.h b/include/cosms-core/file.h index 09c7ec5..cdd5adc 100644 --- a/include/cosms-core/file.h +++ b/include/cosms-core/file.h @@ -9,8 +9,6 @@ #ifndef COSMS_CORE_FILE #define COSMS_CORE_FILE -#include - #if defined(_WIN32) #include #endif @@ -50,7 +48,7 @@ CosmsCoreFileError cosms_core_file_open(struct cosms_core_file *file, const char CosmsCoreFileError cosms_core_file_close(struct cosms_core_file *file); 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, bool override); +CosmsCoreFileError cosms_core_file_write(const char *path, const char *content, unsigned long long size, int mode); CosmsCoreFileError cosms_core_file_delete(const char *path); diff --git a/src/file.c b/src/file.c index bdf18fe..381427a 100644 --- a/src/file.c +++ b/src/file.c @@ -261,13 +261,13 @@ CosmsCoreFileError cosms_core_file_read(const char *path, char **content, unsign return COSMS_CORE_FILE_OK; } -CosmsCoreFileError cosms_core_file_write(const char *path, const char *content, unsigned long long size, bool override) { +CosmsCoreFileError cosms_core_file_write(const char *path, const char *content, unsigned long long size, int mode) { #if defined(__GNUC__) int flags = O_WRONLY | O_CREAT; - if (override) { - file |= O_TRUNC; - } else { + if (mode & COSMS_CORE_FILE_MODE_APPEND) { file |= O_APPEND; + } else if (mode) { + file |= O_TRUNC; } if (size > COSMS_CORE_FILE_LARGE) { @@ -313,10 +313,10 @@ CosmsCoreFileError cosms_core_file_write(const char *path, const char *content, close(file); #elif defined(_WIN32) HANDLE file; - if (override) { - file = CreateFile(path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); - } else { + if (mode & COSMS_CORE_FILE_MODE_APPEND) { file = CreateFile(path, FILE_APPEND_DATA, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); + } else { + file = CreateFile(path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); } if (file == INVALID_HANDLE_VALUE) { diff --git a/tests/unit/file.c b/tests/unit/file.c index 3c418af..c6e4a42 100644 --- a/tests/unit/file.c +++ b/tests/unit/file.c @@ -71,7 +71,7 @@ COSMS_CORE_TEST_TEST(file_read, COSMS_CORE_TEST_TEST(file_write, COSMS_CORE_TEST_SUB_TEST(writting small file, char *file_buffer = "Hello, World!"; - CosmsCoreFileError error = cosms_core_file_write("tests/data/small-write-file.txt", file_buffer, strlen(file_buffer), true); + CosmsCoreFileError error = cosms_core_file_write("tests/data/small-write-file.txt", file_buffer, strlen(file_buffer), COSMS_CORE_FILE_MODE_WRITE); if (error == COSMS_CORE_FILE_OK) { char *file_content = NULL; error = cosms_core_file_read("tests/data/small-write-file.txt", &file_content, NULL); @@ -103,7 +103,7 @@ COSMS_CORE_TEST_TEST(file_write, memset(large_file_buffer, 'a', large_file_size); large_file_buffer[large_file_size] = '\0'; - CosmsCoreFileError error = cosms_core_file_write("tests/data/large-write-file.txt", large_file_buffer, large_file_size, true); + CosmsCoreFileError error = cosms_core_file_write("tests/data/large-write-file.txt", large_file_buffer, large_file_size, COSMS_CORE_FILE_MODE_WRITE); if (error == COSMS_CORE_FILE_OK) { char *file_content = NULL; error = cosms_core_file_read("tests/data/large-write-file.txt", &file_content, NULL); @@ -127,7 +127,7 @@ COSMS_CORE_TEST_TEST(file_write, COSMS_CORE_TEST_SUB_TEST(appending file, char *file_buffer = "Hello, World!"; - CosmsCoreFileError error = cosms_core_file_write("tests/data/small-write-file.txt", file_buffer, strlen(file_buffer), false); + CosmsCoreFileError error = cosms_core_file_write("tests/data/small-write-file.txt", file_buffer, strlen(file_buffer), COSMS_CORE_FILE_MODE_APPEND); if (error == COSMS_CORE_FILE_OK) { char *file_content = NULL; error = cosms_core_file_read("tests/data/small-write-file.txt", &file_content, NULL); @@ -150,7 +150,7 @@ COSMS_CORE_TEST_TEST(file_write, COSMS_CORE_TEST_SUB_TEST(appending to non existing file, char *file_buffer = "Hello, World!"; - CosmsCoreFileError error = cosms_core_file_write("tests/data/append-write-file.txt", file_buffer, strlen(file_buffer), false); + CosmsCoreFileError error = cosms_core_file_write("tests/data/append-write-file.txt", file_buffer, strlen(file_buffer), COSMS_CORE_FILE_MODE_APPEND); if (error == COSMS_CORE_FILE_OK) { char *file_content = NULL; error = cosms_core_file_read("tests/data/append-write-file.txt", &file_content, NULL);