refactor(file): removed boolean override for mode in write function

This commit is contained in:
Mineplay5780 2025-12-17 20:36:35 +01:00
parent 5498b060b8
commit 84b4f0dc59
3 changed files with 12 additions and 14 deletions

View file

@ -9,8 +9,6 @@
#ifndef COSMS_CORE_FILE #ifndef COSMS_CORE_FILE
#define COSMS_CORE_FILE #define COSMS_CORE_FILE
#include <stdbool.h>
#if defined(_WIN32) #if defined(_WIN32)
#include <windows.h> #include <windows.h>
#endif #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_close(struct cosms_core_file *file);
CosmsCoreFileError cosms_core_file_read(const char *path, char **content, 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, 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); CosmsCoreFileError cosms_core_file_delete(const char *path);

View file

@ -261,13 +261,13 @@ CosmsCoreFileError cosms_core_file_read(const char *path, char **content, unsign
return COSMS_CORE_FILE_OK; 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__) #if defined(__GNUC__)
int flags = O_WRONLY | O_CREAT; int flags = O_WRONLY | O_CREAT;
if (override) { if (mode & COSMS_CORE_FILE_MODE_APPEND) {
file |= O_TRUNC;
} else {
file |= O_APPEND; file |= O_APPEND;
} else if (mode) {
file |= O_TRUNC;
} }
if (size > COSMS_CORE_FILE_LARGE) { if (size > COSMS_CORE_FILE_LARGE) {
@ -313,10 +313,10 @@ CosmsCoreFileError cosms_core_file_write(const char *path, const char *content,
close(file); close(file);
#elif defined(_WIN32) #elif defined(_WIN32)
HANDLE file; HANDLE file;
if (override) { if (mode & COSMS_CORE_FILE_MODE_APPEND) {
file = CreateFile(path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
} else {
file = CreateFile(path, FILE_APPEND_DATA, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); 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) { if (file == INVALID_HANDLE_VALUE) {

View file

@ -71,7 +71,7 @@ COSMS_CORE_TEST_TEST(file_read,
COSMS_CORE_TEST_TEST(file_write, COSMS_CORE_TEST_TEST(file_write,
COSMS_CORE_TEST_SUB_TEST(writting small file, COSMS_CORE_TEST_SUB_TEST(writting small file,
char *file_buffer = "Hello, World!"; 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) { if (error == COSMS_CORE_FILE_OK) {
char *file_content = NULL; char *file_content = NULL;
error = cosms_core_file_read("tests/data/small-write-file.txt", &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); memset(large_file_buffer, 'a', large_file_size);
large_file_buffer[large_file_size] = '\0'; 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) { if (error == COSMS_CORE_FILE_OK) {
char *file_content = NULL; char *file_content = NULL;
error = cosms_core_file_read("tests/data/large-write-file.txt", &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, COSMS_CORE_TEST_SUB_TEST(appending file,
char *file_buffer = "Hello, World!"; 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) { if (error == COSMS_CORE_FILE_OK) {
char *file_content = NULL; char *file_content = NULL;
error = cosms_core_file_read("tests/data/small-write-file.txt", &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, COSMS_CORE_TEST_SUB_TEST(appending to non existing file,
char *file_buffer = "Hello, World!"; 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) { if (error == COSMS_CORE_FILE_OK) {
char *file_content = NULL; char *file_content = NULL;
error = cosms_core_file_read("tests/data/append-write-file.txt", &file_content, NULL); error = cosms_core_file_read("tests/data/append-write-file.txt", &file_content, NULL);