Skip to content
This repository has been archived by the owner on Sep 15, 2024. It is now read-only.

Commit

Permalink
documented the public interface and switched around a few functions (…
Browse files Browse the repository at this point in the history
…file.h)
  • Loading branch information
israfiel-a committed Sep 6, 2024
1 parent 9b6613a commit 93c2191
Show file tree
Hide file tree
Showing 6 changed files with 358 additions and 61 deletions.
6 changes: 4 additions & 2 deletions Source/Assets/Shaders.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,10 @@ shader_t* LetoLoadShader(const char* name)
return NULL;
}

char* vraw = LetoReadFilePCV(ASSET_DIR "/Shaders/%s/vert.vs", name);
char* fraw = LetoReadFilePCV(ASSET_DIR "/Shaders/%s/frag.fs", name);
char* vraw =
(char*)LetoReadFilePV(true, ASSET_DIR "/Shaders/%s/vert.vs", name);
char* fraw =
(char*)LetoReadFilePV(true, ASSET_DIR "/Shaders/%s/frag.fs", name);
const char *vcode = vraw, *fcode = fraw;

unsigned int vid = glCreateShader(GL_VERTEX_SHADER),
Expand Down
2 changes: 1 addition & 1 deletion Source/Diagnostic/Platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
* workings, like a slight optimization. This value will reset to 0
* every new minor version.
*/
#define __LETO__VERSION__TWEAK__ 11
#define __LETO__VERSION__TWEAK__ 12

/**
* @brief This is the string represenation of Leto's base version,
Expand Down
3 changes: 3 additions & 0 deletions Source/Output/Reporter.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ static const reported_message_t errors[error_code_count] = {
"file_positioner_get_failed",
"failed to get file position marker"},
[file_read_failed] = {{os}, "file_read_failed", "failed to read file"},
[file_write_failed] = {{os},
"file_write_failed",
"failed to write to file"},
[glfw_init_failed] = {{glfw}, "glfw_init_failed", "glfw init failed"},
[glfw_window_create_failed] = {{glfw},
"glfw_window_create_failed",
Expand Down
1 change: 1 addition & 0 deletions Source/Output/Reporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ typedef enum
file_positioner_set_failed,
file_positioner_get_failed,
file_read_failed,
file_write_failed,
glfw_init_failed,
glfw_window_create_failed,
glfw_monitor_get_failed,
Expand Down
103 changes: 52 additions & 51 deletions Source/Utilities/Files.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@
*
* WARNINGS
*
* One warning can be thrown by this function.
* @warning null_string -- If either @param path or @param mode is passed
* to the function as NULL, this warning is thrown and NULL is returned.
* Nothing of note.
*
* ERRORS
*
Expand All @@ -56,12 +54,6 @@
*/
static FILE* OpenFile_(const char* path, const char* mode)
{
if (path == NULL || mode == NULL)
{
LetoReportWarning(null_string);
return NULL;
}

FILE* opened_file = NULL;

// MSVC complains if we use fopen instead of fopen_s, so we just compile
Expand Down Expand Up @@ -93,9 +85,7 @@ static FILE* OpenFile_(const char* path, const char* mode)
*
* WARNINGS
*
* One warning can be thrown by this function.
* @warning null_object -- If the file handle passed to the function is
* NULL, this error is thrown and 0 is returned.
* Nothing of note.
*
* ERRORS
*
Expand All @@ -111,17 +101,12 @@ static FILE* OpenFile_(const char* path, const char* mode)
*/
static size_t GetFileSize_(FILE* file)
{
if (file == NULL)
{
LetoReportWarning(null_object);
return 0;
}

if (fseek(file, 0L, SEEK_END) == -1)
LetoReportError(file_positioner_set_failed);

long size = ftell(file);
if (size == -1) LetoReportError(file_positioner_get_failed);
// Reset the file positioner.
if (fseek(file, 0L, SEEK_SET) == -1)
LetoReportError(file_positioner_set_failed);

Expand Down Expand Up @@ -170,11 +155,32 @@ void LetoCloseFile(file_t* file)
}

if (fclose(file->handle) == EOF) LetoReportError(file_close_failed);
LetoStringFree(&file->path);
if (file->contents != NULL) free(file->contents);

char* temp_path_storage = (char*)file->path;
LetoStringFree(&temp_path_storage);

free(file);
}

size_t LetoGetFilesize(file_t* file)
{
if (file == NULL)
{
LetoReportWarning(null_object);
return 0;
}

if (file->handle == NULL)
{
LetoReportWarning(file_invalid);
return 0;
}

file->size = GetFileSize_(file->handle);
return file->size;
}

void LetoReadFile(file_t* file)
{
if (file == NULL)
Expand All @@ -190,38 +196,33 @@ void LetoReadFile(file_t* file)
}

file->contents = calloc(file->size, 1);
if (file->contents == NULL) LetoReportError(failed_allocation);

// Set our positioner to beginning of the file.
if (fseek(file->handle, 0L, SEEK_SET) == -1)
LetoReportError(file_positioner_set_failed);
if (fread(file->contents, 1, file->size, file->handle) != file->size)
LetoReportError(file_read_failed);
// Reset the positioner.
if (fseek(file->handle, 0L, SEEK_SET) == -1)
LetoReportError(file_positioner_set_failed);
}

uint8_t* LetoReadFileP(const char* path)
uint8_t* LetoReadFileP(bool terminate, const char* path)
{
file_t* opened_file = LetoOpenFile(r, path);
LetoReadFile(opened_file);

uint8_t* buffer = malloc(opened_file->size);
(void)memcpy(buffer, opened_file->contents, opened_file->size);

LetoCloseFile(opened_file);
return buffer;
}
if (buffer == NULL) LetoReportError(failed_allocation);

char* LetoReadFilePC(const char* path)
{
file_t* opened_file = LetoOpenFile(r, path);
LetoReadFile(opened_file);

char* buffer = LetoStringCalloc(opened_file->size);
(void)strncpy(buffer, (char*)opened_file->contents,
opened_file->size - 1);
(void)memcpy(buffer, opened_file->contents, opened_file->size - 1);
if (terminate) buffer[opened_file->size - 1] = 0;

LetoCloseFile(opened_file);
return buffer;
}

uint8_t* LetoReadFilePV(const char* format, ...)
uint8_t* LetoReadFilePV(bool terminate, const char* format, ...)
{
va_list args;
va_start(args, format);
Expand All @@ -230,29 +231,29 @@ uint8_t* LetoReadFilePV(const char* format, ...)

file_t* opened_file = LetoOpenFile(r, new_path);
LetoReadFile(opened_file);

uint8_t* buffer = malloc(opened_file->size);
(void)memcpy(buffer, opened_file->contents, opened_file->size);
if (buffer == NULL) LetoReportError(failed_allocation);

(void)memcpy(buffer, opened_file->contents, opened_file->size - 1);
if (terminate) buffer[opened_file->size - 1] = 0;

LetoCloseFile(opened_file);
LetoStringFree(&new_path);
return buffer;
}

char* LetoReadFilePCV(const char* format, ...)
void LetoWriteFile(file_t* file, uint8_t* buffer, size_t buffer_size)
{
va_list args;
va_start(args, format);
char* new_path = LetoStringCreateV(MAX_PATH_LENGTH, format, args);
va_end(args);

file_t* opened_file = LetoOpenFile(r, new_path);
LetoReadFile(opened_file);
if (file == NULL || buffer == NULL)
{
LetoReportWarning(null_object);
return;
}

char* buffer = LetoStringCalloc(opened_file->size);
(void)strncpy(buffer, (char*)opened_file->contents,
opened_file->size - 1);
if (file->permissions == r) return;

LetoCloseFile(opened_file);
LetoStringFree(&new_path);
return buffer;
// Don't set the file positioner off the bat, instead trusting the
// positioner to be in whatever position it should be in.
if (fwrite(buffer, 1, buffer_size, file->handle) != buffer_size)
LetoReportError(file_write_failed);
}
Loading

0 comments on commit 93c2191

Please sign in to comment.