Compare commits
4 commits
bcbe5c8d0f
...
66ab9fbbdf
| Author | SHA1 | Date | |
|---|---|---|---|
| 66ab9fbbdf | |||
| 531a4d03cb | |||
| 59928a9a55 | |||
| 5ea177d928 |
5 changed files with 157 additions and 11 deletions
4
.gitignore
vendored
4
.gitignore
vendored
|
|
@ -65,5 +65,7 @@ compile_commands.json
|
||||||
CTestTestfile.cmake
|
CTestTestfile.cmake
|
||||||
_deps
|
_deps
|
||||||
CMakeUserPresets.json
|
CMakeUserPresets.json
|
||||||
|
|
||||||
build
|
build
|
||||||
|
|
||||||
|
# clangd
|
||||||
|
.cache/
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ target_include_directories(cosms-core PUBLIC ${PROJECT_SOURCE_DIR}/include)
|
||||||
if (MSVC)
|
if (MSVC)
|
||||||
target_compile_options(cosms-core PRIVATE /W4)
|
target_compile_options(cosms-core PRIVATE /W4)
|
||||||
else()
|
else()
|
||||||
target_compile_options(cosms-core PRIVATE -wall -Wextra -pedantic)
|
target_compile_options(cosms-core PRIVATE -Wall -Wextra -pedantic)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
add_executable(cosms-core-test ${PROJECT_SOURCE_DIR}/tests/main.c)
|
add_executable(cosms-core-test ${PROJECT_SOURCE_DIR}/tests/main.c)
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,29 @@
|
||||||
|
/*
|
||||||
|
* Copyright (C) Tristan Franssen, <tristanfranssen@strawhats.nl>.
|
||||||
|
*
|
||||||
|
* This software is licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* You may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License in the file LICENSE or at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*/
|
||||||
#ifndef COSMS_CORE_FILE
|
#ifndef COSMS_CORE_FILE
|
||||||
#define COSMS_CORE_FILE
|
#define COSMS_CORE_FILE
|
||||||
|
|
||||||
void cosms_core_file_read(char *path);
|
#include <stdbool.h>
|
||||||
void cosms_core_file_write(char *path);
|
|
||||||
|
typedef enum {
|
||||||
|
COSMS_FILE_OK = 0,
|
||||||
|
COSMS_FILE_NOT_FOUND = -1,
|
||||||
|
COSMS_FILE_NO_ACCESS = -2,
|
||||||
|
COSMS_FILE_LIMIT_REACHED = -3,
|
||||||
|
COSMS_FILE_COULD_NOT_READ_SIZE = -4,
|
||||||
|
COSMS_FILE_UNKOWN_ERROR = -5,
|
||||||
|
COSMS_FILE_FAILED_TO_ALLOCATE = -6,
|
||||||
|
COSMS_FILE_FAILED_TO_READ = -7,
|
||||||
|
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);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
116
src/file.c
116
src/file.c
|
|
@ -1,11 +1,115 @@
|
||||||
#include <cosms-core/file.h>
|
/*
|
||||||
|
* Copyright (C) Tristan Franssen, <tristanfranssen@strawhats.nl>.
|
||||||
|
*
|
||||||
|
* This software is licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* You may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License in the file LICENSE or at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*/
|
||||||
|
#include "cosms-core/file.h"
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
void cosms_core_file_read(char *path) {
|
#if defined(__GNUC__)
|
||||||
printf("Hello, World!");
|
#include <unistd.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#elif defined(_WIN32)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
CosmsFileError cosms_core_file_read(const char *path, char **content, unsigned int *size) {
|
||||||
|
#if defined(__GNUC__)
|
||||||
|
int file = open(path, O_RDONLY);
|
||||||
|
if (file == -1) {
|
||||||
|
switch(errno) {
|
||||||
|
case ENOENT:
|
||||||
|
return COSMS_FILE_NOT_FOUND;
|
||||||
|
|
||||||
|
case EACCES:
|
||||||
|
return COSMS_FILE_NO_ACCESS;
|
||||||
|
|
||||||
|
case EMFILE:
|
||||||
|
case ENFILE:
|
||||||
|
return COSMS_FILE_LIMIT_REACHED;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return COSMS_FILE_UNKOWN_ERROR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct stat file_stat;
|
||||||
|
if (stat(path, &file_stat) != 0) {
|
||||||
|
close(file);
|
||||||
|
return COSMS_FILE_COULD_NOT_READ_SIZE;
|
||||||
|
}
|
||||||
|
|
||||||
|
*size = file_stat.st_size;
|
||||||
|
*content = (char*)malloc(file_stat.st_size * sizeof(char));
|
||||||
|
if (content == NULL) {
|
||||||
|
close(file);
|
||||||
|
return COSMS_FILE_FAILED_TO_ALLOCATE;
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int remaining_bytes = file_stat.st_size;
|
||||||
|
while (remaining_bytes != 0) {
|
||||||
|
int read_bytes = read(file, (*content) + (file_stat.st_size - remaining_bytes), remaining_bytes);
|
||||||
|
if (read_bytes == -1) {
|
||||||
|
free(content);
|
||||||
|
close(file);
|
||||||
|
return COSMS_FILE_FAILED_TO_READ;
|
||||||
|
}
|
||||||
|
|
||||||
|
remaining_bytes -= read_bytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
close(file);
|
||||||
|
#elif defined(_WIN32)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return COSMS_FILE_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
void cosms_core_file_write(char *path) {
|
CosmsFileError cosms_core_file_write(const char *path, const char *content, unsigned int size, bool override) {
|
||||||
printf("Hello, World");
|
#if defined(__GNUC__)
|
||||||
|
int file;
|
||||||
|
if (override) {
|
||||||
|
file = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
|
||||||
|
} else {
|
||||||
|
file = open(path, O_WRONLY | O_APPEND);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (file == -1) {
|
||||||
|
switch (errno) {
|
||||||
|
case ENOENT:
|
||||||
|
return COSMS_FILE_NOT_FOUND;
|
||||||
|
|
||||||
|
case EACCES:
|
||||||
|
return COSMS_FILE_NO_ACCESS;
|
||||||
|
|
||||||
|
case EMFILE:
|
||||||
|
case ENFILE:
|
||||||
|
return COSMS_FILE_LIMIT_REACHED;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return COSMS_FILE_UNKOWN_ERROR;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int remaining_bytes = size;
|
||||||
|
while (remaining_bytes != 0) {
|
||||||
|
int written_bytes = write(file, content + (size - remaining_bytes), remaining_bytes);
|
||||||
|
if (written_bytes == -1) {
|
||||||
|
close(file);
|
||||||
|
return COSMS_FILE_FAILED_TO_WRITE;
|
||||||
|
}
|
||||||
|
|
||||||
|
remaining_bytes -= written_bytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
close(file);
|
||||||
|
#elif defined(_WIN32)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return COSMS_FILE_OK;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
20
tests/main.c
20
tests/main.c
|
|
@ -1,6 +1,24 @@
|
||||||
#include <cosms-core/file.h>
|
#include <cosms-core/file.h>
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
cosms_core_file_read("test.txt");
|
unsigned int buffer_size;
|
||||||
|
char *buffer;
|
||||||
|
CosmsFileError error = cosms_core_file_read("test.txt", &buffer, &buffer_size);
|
||||||
|
if (error != COSMS_FILE_OK) {
|
||||||
|
fprintf(stderr, "failed to read file exited with error code: %d\n", error);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("File read with content:\n %s\n", buffer);
|
||||||
|
|
||||||
|
char *temp_buffer = "Hello, World!";
|
||||||
|
error = cosms_core_file_write("write-file.txt", temp_buffer, 13, true);
|
||||||
|
if (error != COSMS_FILE_OK) {
|
||||||
|
fprintf(stderr, "failed to write file exited with error code: %d\n", error);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue