feat(file): made function to convert error codes to string
This commit is contained in:
parent
6a5a80b3f5
commit
87fb7556c3
3 changed files with 40 additions and 6 deletions
|
|
@ -26,4 +26,6 @@ typedef enum {
|
||||||
CosmsFileError cosms_core_file_read(const char *path, char **content, unsigned long long *size);
|
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);
|
CosmsFileError cosms_core_file_write(const char *path, const char *content, unsigned long long size, bool override);
|
||||||
|
|
||||||
|
const char *cosms_core_file_error_string(CosmsFileError error);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
34
src/file.c
34
src/file.c
|
|
@ -230,3 +230,37 @@ CosmsFileError cosms_core_file_write(const char *path, const char *content, unsi
|
||||||
|
|
||||||
return COSMS_FILE_OK;
|
return COSMS_FILE_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const char *cosms_core_file_error_string(CosmsFileError error) {
|
||||||
|
switch(error) {
|
||||||
|
case COSMS_FILE_OK:
|
||||||
|
return "cosms-file everything ok";
|
||||||
|
|
||||||
|
case COSMS_FILE_NOT_FOUND:
|
||||||
|
return "cosms-file path not found";
|
||||||
|
|
||||||
|
case COSMS_FILE_NO_ACCESS:
|
||||||
|
return "cosms-file no access";
|
||||||
|
|
||||||
|
case COSMS_FILE_LIMIT_REACHED:
|
||||||
|
return "cosms-file to many open files";
|
||||||
|
|
||||||
|
case COSMS_FILE_COULD_NOT_READ_SIZE:
|
||||||
|
return "cosms-file failed to read size";
|
||||||
|
|
||||||
|
case COSMS_FILE_UNKOWN_ERROR:
|
||||||
|
return "cosms-file unkown error occured";
|
||||||
|
|
||||||
|
case COSMS_FILE_FAILED_TO_ALLOCATE:
|
||||||
|
return "cosms-file failed to allocate memory";
|
||||||
|
|
||||||
|
case COSMS_FILE_FAILED_TO_READ:
|
||||||
|
return "cosms-file failed to read content of file";
|
||||||
|
|
||||||
|
case COSMS_FILE_FAILED_TO_WRITE:
|
||||||
|
return "cosms-file failed to write content to file";
|
||||||
|
|
||||||
|
default:
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
10
tests/main.c
10
tests/main.c
|
|
@ -7,17 +7,15 @@ int main(void) {
|
||||||
char *buffer;
|
char *buffer;
|
||||||
CosmsFileError error = cosms_core_file_read("test.txt", &buffer, &buffer_size);
|
CosmsFileError error = cosms_core_file_read("test.txt", &buffer, &buffer_size);
|
||||||
if (error != COSMS_FILE_OK) {
|
if (error != COSMS_FILE_OK) {
|
||||||
fprintf(stderr, "failed to read file exited with error code: %d\n", error);
|
fprintf(stderr, "error(%d): %s\n", error, cosms_core_file_error_string(error));
|
||||||
return -1;
|
} else {
|
||||||
|
printf("file read with content:\n%s\n", buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("File read with content:\n%s\n", buffer);
|
|
||||||
|
|
||||||
char *temp_buffer = "Hello, World!";
|
char *temp_buffer = "Hello, World!";
|
||||||
error = cosms_core_file_write("write-file.txt", temp_buffer, 13, true);
|
error = cosms_core_file_write("write-file.txt", temp_buffer, 13, true);
|
||||||
if (error != COSMS_FILE_OK) {
|
if (error != COSMS_FILE_OK) {
|
||||||
fprintf(stderr, "failed to write file exited with error code: %d\n", error);
|
fprintf(stderr, "error(%d): %s\n", error, cosms_core_file_error_string(error));
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue