Compare commits
No commits in common. "66ab9fbbdf61730cc237c566ccc2e89813b8a19d" and "bcbe5c8d0f733f09906343c2c0a7a243a6863fd4" have entirely different histories.
66ab9fbbdf
...
bcbe5c8d0f
5 changed files with 11 additions and 157 deletions
4
.gitignore
vendored
4
.gitignore
vendored
|
|
@ -65,7 +65,5 @@ compile_commands.json
|
|||
CTestTestfile.cmake
|
||||
_deps
|
||||
CMakeUserPresets.json
|
||||
build
|
||||
|
||||
# clangd
|
||||
.cache/
|
||||
build
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ target_include_directories(cosms-core PUBLIC ${PROJECT_SOURCE_DIR}/include)
|
|||
if (MSVC)
|
||||
target_compile_options(cosms-core PRIVATE /W4)
|
||||
else()
|
||||
target_compile_options(cosms-core PRIVATE -Wall -Wextra -pedantic)
|
||||
target_compile_options(cosms-core PRIVATE -wall -Wextra -pedantic)
|
||||
endif()
|
||||
|
||||
add_executable(cosms-core-test ${PROJECT_SOURCE_DIR}/tests/main.c)
|
||||
|
|
|
|||
|
|
@ -1,29 +1,7 @@
|
|||
/*
|
||||
* 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
|
||||
#define COSMS_CORE_FILE
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
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);
|
||||
void cosms_core_file_read(char *path);
|
||||
void cosms_core_file_write(char *path);
|
||||
|
||||
#endif
|
||||
|
|
|
|||
116
src/file.c
116
src/file.c
|
|
@ -1,115 +1,11 @@
|
|||
/*
|
||||
* 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 <cosms-core/file.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#if defined(__GNUC__)
|
||||
#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_read(char *path) {
|
||||
printf("Hello, World!");
|
||||
}
|
||||
|
||||
CosmsFileError cosms_core_file_write(const char *path, const char *content, unsigned int size, bool override) {
|
||||
#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;
|
||||
void cosms_core_file_write(char *path) {
|
||||
printf("Hello, World");
|
||||
}
|
||||
|
|
|
|||
20
tests/main.c
20
tests/main.c
|
|
@ -1,24 +1,6 @@
|
|||
#include <cosms-core/file.h>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void) {
|
||||
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;
|
||||
}
|
||||
|
||||
cosms_core_file_read("test.txt");
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue