feat(file): implemented read file for linux
This commit is contained in:
parent
59928a9a55
commit
531a4d03cb
3 changed files with 79 additions and 8 deletions
|
|
@ -9,12 +9,20 @@
|
||||||
#ifndef COSMS_CORE_FILE
|
#ifndef COSMS_CORE_FILE
|
||||||
#define COSMS_CORE_FILE
|
#define COSMS_CORE_FILE
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
COSMS_FILE_OK = 0,
|
COSMS_FILE_OK = 0,
|
||||||
COSMS_FILE_NOT_FOUND = -1,
|
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,
|
||||||
} CosmsFileError;
|
} CosmsFileError;
|
||||||
|
|
||||||
CosmsFileError cosms_core_file_read(char *path);
|
CosmsFileError cosms_core_file_read(const char *path, char **content, unsigned int *size);
|
||||||
CosmsFileError cosms_core_file_write(char *path);
|
CosmsFileError cosms_core_file_write(const char *path, const char *content, unsigned int size, bool override);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
63
src/file.c
63
src/file.c
|
|
@ -8,14 +8,67 @@
|
||||||
*/
|
*/
|
||||||
#include "cosms-core/file.h"
|
#include "cosms-core/file.h"
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
CosmsFileError 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, 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;
|
return COSMS_FILE_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
CosmsFileError 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");
|
|
||||||
return COSMS_FILE_OK;
|
return COSMS_FILE_OK;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
12
tests/main.c
12
tests/main.c
|
|
@ -1,6 +1,16 @@
|
||||||
#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);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue