feat(file): implemented file reading and writting for windows

This commit is contained in:
Mineplay5780 2025-12-08 23:04:51 +01:00
parent 66ab9fbbdf
commit d44d460cad
3 changed files with 109 additions and 7 deletions

View file

@ -23,7 +23,7 @@ typedef enum {
COSMS_FILE_FAILED_TO_WRITE = -8,
} CosmsFileError;
CosmsFileError cosms_core_file_read(const char *path, char **content, unsigned int *size);
CosmsFileError cosms_core_file_write(const char *path, const char *content, unsigned int size, bool override);
CosmsFileError cosms_core_file_read(const char *path, char **content, unsigned long long *size);
CosmsFileError cosms_core_file_write(const char *path, const char *content, unsigned long long size, bool override);
#endif

View file

@ -9,6 +9,7 @@
#include "cosms-core/file.h"
#include <stdlib.h>
#include <stdint.h>
#if defined(__GNUC__)
#include <unistd.h>
@ -16,9 +17,10 @@
#include <sys/stat.h>
#include <errno.h>
#elif defined(_WIN32)
#include <windows.h>
#endif
CosmsFileError cosms_core_file_read(const char *path, char **content, unsigned int *size) {
CosmsFileError cosms_core_file_read(const char *path, char **content, unsigned long long *size) {
#if defined(__GNUC__)
int file = open(path, O_RDONLY);
if (file == -1) {
@ -55,7 +57,7 @@ CosmsFileError cosms_core_file_read(const char *path, char **content, unsigned i
while (remaining_bytes != 0) {
int read_bytes = read(file, (*content) + (file_stat.st_size - remaining_bytes), remaining_bytes);
if (read_bytes == -1) {
free(content);
free(*content);
close(file);
return COSMS_FILE_FAILED_TO_READ;
}
@ -65,12 +67,68 @@ CosmsFileError cosms_core_file_read(const char *path, char **content, unsigned i
close(file);
#elif defined(_WIN32)
HANDLE file = CreateFile(path, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (file == INVALID_HANDLE_VALUE) {
switch(GetLastError()) {
case ERROR_FILE_NOT_FOUND:
case ERROR_PATH_NOT_FOUND:
return COSMS_FILE_NOT_FOUND;
case ERROR_ACCESS_DENIED:
case ERROR_SHARING_VIOLATION:
case ERROR_LOCK_VIOLATION:
return COSMS_FILE_NO_ACCESS;
case ERROR_TOO_MANY_OPEN_FILES:
return COSMS_FILE_LIMIT_REACHED;
default:
return COSMS_FILE_UNKOWN_ERROR;
}
}
DWORD high_size;
DWORD low_size = GetFileSize(file, &high_size);
if (low_size == INVALID_FILE_SIZE && GetLastError() != NO_ERROR) {
CloseHandle(file);
return COSMS_FILE_COULD_NOT_READ_SIZE;
}
unsigned long long file_size = ((unsigned long long)high_size << 32) | low_size;
*size = file_size;
*content = (char*)malloc((file_size + 1) * sizeof(char));
if (content == NULL) {
CloseHandle(file);
return COSMS_FILE_FAILED_TO_ALLOCATE;
}
unsigned long long remaining_bytes = file_size;
while (remaining_bytes != 0) {
DWORD bytes_to_read, read_bytes;
if (remaining_bytes > UINT_MAX) {
bytes_to_read = UINT_MAX;
} else {
bytes_to_read = (DWORD)remaining_bytes;
}
if (!ReadFile(file, (*content) + (file_size - remaining_bytes), bytes_to_read, &read_bytes, NULL)) {
free(*content);
CloseHandle(file);
return COSMS_FILE_FAILED_TO_READ;
}
remaining_bytes -= read_bytes;
}
(*content)[file_size] = '\0';
CloseHandle(file);
#endif
return COSMS_FILE_OK;
}
CosmsFileError cosms_core_file_write(const char *path, const char *content, unsigned int size, bool override) {
CosmsFileError cosms_core_file_write(const char *path, const char *content, unsigned long long size, bool override) {
#if defined(__GNUC__)
int file;
if (override) {
@ -109,6 +167,50 @@ CosmsFileError cosms_core_file_write(const char *path, const char *content, unsi
close(file);
#elif defined(_WIN32)
HANDLE file;
if (override) {
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);
}
if (file == INVALID_HANDLE_VALUE) {
switch(GetLastError()) {
case ERROR_FILE_NOT_FOUND:
case ERROR_PATH_NOT_FOUND:
return COSMS_FILE_NOT_FOUND;
case ERROR_ACCESS_DENIED:
case ERROR_SHARING_VIOLATION:
case ERROR_LOCK_VIOLATION:
return COSMS_FILE_NO_ACCESS;
case ERROR_TOO_MANY_OPEN_FILES:
return COSMS_FILE_LIMIT_REACHED;
default:
return COSMS_FILE_UNKOWN_ERROR;
}
}
unsigned long long remaining_bytes = size;
while (remaining_bytes != 0) {
DWORD bytes_to_write, written_bytes;
if (remaining_bytes > UINT_MAX) {
bytes_to_write = UINT_MAX;
} else {
bytes_to_write = (DWORD)remaining_bytes;
}
if (!WriteFile(file, content + (size - remaining_bytes), bytes_to_write, &written_bytes, NULL)) {
CloseHandle(file);
return COSMS_FILE_FAILED_TO_WRITE;
}
remaining_bytes -= written_bytes;
}
CloseHandle(file);
#endif
return COSMS_FILE_OK;

View file

@ -3,7 +3,7 @@
#include <stdio.h>
int main(void) {
unsigned int buffer_size;
unsigned long long buffer_size;
char *buffer;
CosmsFileError error = cosms_core_file_read("test.txt", &buffer, &buffer_size);
if (error != COSMS_FILE_OK) {