74 lines
1.9 KiB
C
74 lines
1.9 KiB
C
/*
|
|
* 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 <stdlib.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, 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;
|
|
}
|
|
|
|
CosmsFileError cosms_core_file_write(const char *path, const char *content, unsigned int size, bool override) {
|
|
return COSMS_FILE_OK;
|
|
}
|