aboutsummaryrefslogtreecommitdiff
path: root/src/utils
diff options
context:
space:
mode:
authorA404M <ahmadmahmoudiprogrammer@gmail.com>2024-09-25 19:47:29 +0330
committerA404M <ahmadmahmoudiprogrammer@gmail.com>2024-09-25 19:47:29 +0330
commitc409b8aaf6b6f63bd68a3356e146ab80b2ec8c4b (patch)
tree65ea5801fd910fc6bcff3f2e8f06b5fd7d249c78 /src/utils
parentf79290084948f3cf140395c270c07cf29ca58e8d (diff)
fixed multiple variable definition bug
tried to implement import
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/file.c14
-rw-r--r--src/utils/file.h5
-rw-r--r--src/utils/memory/memory.c13
-rw-r--r--src/utils/memory/memory.h3
-rw-r--r--src/utils/types.h3
5 files changed, 29 insertions, 9 deletions
diff --git a/src/utils/file.c b/src/utils/file.c
index 410c218..cdb2dfe 100644
--- a/src/utils/file.c
+++ b/src/utils/file.c
@@ -1,8 +1,10 @@
#include "file.h"
+
#include <stdio.h>
+#include <string.h>
#include <utils/memory/memory.h>
-char *read_whole_file(const char *path) {
+Code *read_whole_file(const char *path) {
FILE *file = fopen(path, "r");
if (!file) {
fprintf(stderr, "could not open file at path '%s'\n", path);
@@ -18,5 +20,13 @@ char *read_whole_file(const char *path) {
str[file_size] = '\0';
fclose(file);
- return str;
+ Code *code = a404m_malloc(sizeof(*code));
+
+ size_t pathLen = strlen(path);
+ code->code = str;
+ code->filePath = a404m_malloc(pathLen+1);
+
+ memcpy(code->filePath, path, pathLen+1);
+
+ return code;
}
diff --git a/src/utils/file.h b/src/utils/file.h
index 541e69d..104aec2 100644
--- a/src/utils/file.h
+++ b/src/utils/file.h
@@ -1,3 +1,6 @@
#pragma once
-extern char *read_whole_file(const char *path);
+#include <utils/types.h>
+#include <compiler/source_code/source_code.h>
+
+extern Code *read_whole_file(const char *path);
diff --git a/src/utils/memory/memory.c b/src/utils/memory/memory.c
index d793bc7..cde2995 100644
--- a/src/utils/memory/memory.c
+++ b/src/utils/memory/memory.c
@@ -1,6 +1,7 @@
#include "memory.h"
#include <stdlib.h>
+#include <malloc.h>
void *a404m_malloc(size_t size) {
if (size == 0) {
@@ -14,9 +15,17 @@ void *a404m_realloc(void *restrict pointer, size_t size) {
if (size == 0) {
free(pointer);
return NULL;
- } else if(pointer != NULL) {
+ } else if (pointer != NULL) {
return realloc(pointer, size);
- }else{
+ } else {
return malloc(size);
}
}
+
+size_t a404m_malloc_usable_size(void *pointer) {
+ if (pointer == NULL) {
+ return 0;
+ } else {
+ return malloc_usable_size(pointer);
+ }
+}
diff --git a/src/utils/memory/memory.h b/src/utils/memory/memory.h
index 1c5017f..29bd3db 100644
--- a/src/utils/memory/memory.h
+++ b/src/utils/memory/memory.h
@@ -3,4 +3,5 @@
#include <stddef.h>
extern void *a404m_malloc(size_t size);
-extern void *a404m_realloc(void *restrict pointer,size_t size);
+extern void *a404m_realloc(void *restrict pointer, size_t size);
+extern size_t a404m_malloc_usable_size(void *pointer);
diff --git a/src/utils/types.h b/src/utils/types.h
index e2d8053..b75d64d 100644
--- a/src/utils/types.h
+++ b/src/utils/types.h
@@ -5,13 +5,10 @@
#ifndef __cplusplus
#if (__STDC_VERSION__ < 202000L)
- #include <stdint.h>
typedef enum bool : uint8_t { false = 0, true = 1 } bool;
#endif
#endif
-typedef const char *const SourceCode;
-
typedef struct SizedString {
char *str;
size_t size;