summaryrefslogtreecommitdiff
path: root/src/utils
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/file.c26
-rw-r--r--src/utils/file.h10
-rw-r--r--src/utils/log.c5
3 files changed, 35 insertions, 6 deletions
diff --git a/src/utils/file.c b/src/utils/file.c
index f2244bf..89ccfa8 100644
--- a/src/utils/file.c
+++ b/src/utils/file.c
@@ -4,12 +4,32 @@
#include "utils/memory.h"
#include <stddef.h>
#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
size_t fileCodes_capacity = 0;
char **fileCodes = NULL;
-const char **fileCodes_names = 0;
+char **fileCodes_names = NULL;
size_t fileCodes_length = 0;
+void fileInit() {
+ fileCodes_capacity = 0;
+ fileCodes = a404m_malloc(fileCodes_capacity * sizeof(*fileCodes));
+ fileCodes_names = a404m_malloc(fileCodes_capacity * sizeof(*fileCodes_names));
+ fileCodes_length = 0;
+}
+
+void fileDelete() {
+ fileCodes_capacity = 0;
+ for (size_t i = 0; i < fileCodes_length; ++i) {
+ free(fileCodes[i]);
+ free(fileCodes_names[i]);
+ }
+ free(fileCodes);
+ free(fileCodes_names);
+ fileCodes_length = 0;
+}
+
char *readWholeFile(const char *filePath) {
FILE *file = fopen(filePath, "r");
@@ -36,7 +56,9 @@ char *readWholeFile(const char *filePath) {
fileCodes_names, fileCodes_capacity * sizeof(*fileCodes_names));
}
fileCodes[fileCodes_length] = str;
- fileCodes_names[fileCodes_length] = filePath;
+ fileCodes_names[fileCodes_length] =
+ a404m_malloc((strlen(filePath) + 1) * sizeof(**fileCodes_names));
+ strcpy(fileCodes_names[fileCodes_length], filePath);
fileCodes_length += 1;
return str;
diff --git a/src/utils/file.h b/src/utils/file.h
index 6f9753f..4a904e4 100644
--- a/src/utils/file.h
+++ b/src/utils/file.h
@@ -1,3 +1,13 @@
#pragma once
+#include <stddef.h>
+
+extern size_t fileCodes_capacity;
+extern char **fileCodes;
+extern char **fileCodes_names;
+extern size_t fileCodes_length;
+
+void fileInit();
+void fileDelete();
+
char *readWholeFile(const char *filePath);
diff --git a/src/utils/log.c b/src/utils/log.c
index e84f3b4..e1822c0 100644
--- a/src/utils/log.c
+++ b/src/utils/log.c
@@ -1,5 +1,6 @@
#include "log.h"
+#include "utils/file.h"
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
@@ -16,10 +17,6 @@ void _printLogBack(const char *format, const char *file, int line, ...) {
free(errorStr);
}
-extern char **fileCodes;
-extern const char **fileCodes_names;
-extern size_t fileCodes_length;
-
void _printErrorBack(const char *format, const char *file, int line,
char *begin, char *end, ...) {
va_list args;