aboutsummaryrefslogtreecommitdiff
path: root/src/utils
diff options
context:
space:
mode:
authorA404M <ahmadmahmoudiprogrammer@gmail.com>2024-09-18 19:46:38 +0330
committerA404M <ahmadmahmoudiprogrammer@gmail.com>2024-09-18 19:57:20 +0330
commitd6ba30b94a24607bce5db5e706eb20cc051a98f0 (patch)
tree146e74b0bc2e1636451257015210e3c7d1a0ecab /src/utils
initial commit
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/file.c22
-rw-r--r--src/utils/file.h3
-rw-r--r--src/utils/memory/memory.c22
-rw-r--r--src/utils/memory/memory.h6
-rw-r--r--src/utils/time.c9
-rw-r--r--src/utils/time.h7
-rw-r--r--src/utils/types.h18
7 files changed, 87 insertions, 0 deletions
diff --git a/src/utils/file.c b/src/utils/file.c
new file mode 100644
index 0000000..410c218
--- /dev/null
+++ b/src/utils/file.c
@@ -0,0 +1,22 @@
+#include "file.h"
+#include <stdio.h>
+#include <utils/memory/memory.h>
+
+char *read_whole_file(const char *path) {
+ FILE *file = fopen(path, "r");
+ if (!file) {
+ fprintf(stderr, "could not open file at path '%s'\n", path);
+ return NULL;
+ }
+ fseek(file, 0, SEEK_END);
+ const size_t file_size = ftell(file);
+ fseek(file, 0, SEEK_SET);
+
+ char *str = a404m_malloc((file_size + 1) * sizeof(char));
+
+ fread(str, file_size, 1, file);
+ str[file_size] = '\0';
+
+ fclose(file);
+ return str;
+}
diff --git a/src/utils/file.h b/src/utils/file.h
new file mode 100644
index 0000000..541e69d
--- /dev/null
+++ b/src/utils/file.h
@@ -0,0 +1,3 @@
+#pragma once
+
+extern char *read_whole_file(const char *path);
diff --git a/src/utils/memory/memory.c b/src/utils/memory/memory.c
new file mode 100644
index 0000000..d793bc7
--- /dev/null
+++ b/src/utils/memory/memory.c
@@ -0,0 +1,22 @@
+#include "memory.h"
+
+#include <stdlib.h>
+
+void *a404m_malloc(size_t size) {
+ if (size == 0) {
+ return NULL;
+ } else {
+ return malloc(size);
+ }
+}
+
+void *a404m_realloc(void *restrict pointer, size_t size) {
+ if (size == 0) {
+ free(pointer);
+ return NULL;
+ } else if(pointer != NULL) {
+ return realloc(pointer, size);
+ }else{
+ return malloc(size);
+ }
+}
diff --git a/src/utils/memory/memory.h b/src/utils/memory/memory.h
new file mode 100644
index 0000000..1c5017f
--- /dev/null
+++ b/src/utils/memory/memory.h
@@ -0,0 +1,6 @@
+#pragma once
+
+#include <stddef.h>
+
+extern void *a404m_malloc(size_t size);
+extern void *a404m_realloc(void *restrict pointer,size_t size);
diff --git a/src/utils/time.c b/src/utils/time.c
new file mode 100644
index 0000000..729e26f
--- /dev/null
+++ b/src/utils/time.c
@@ -0,0 +1,9 @@
+#include "time.h"
+#include <bits/time.h>
+#include <time.h>
+
+Clock getTimeInNano(){
+ struct timespec t;
+ clock_gettime(CLOCK_MONOTONIC, &t);
+ return t.tv_sec*1000000000+t.tv_nsec;
+}
diff --git a/src/utils/time.h b/src/utils/time.h
new file mode 100644
index 0000000..691b26b
--- /dev/null
+++ b/src/utils/time.h
@@ -0,0 +1,7 @@
+#pragma once
+
+#include <stdint.h>
+
+typedef uint64_t Clock;
+
+extern Clock getTimeInNano();
diff --git a/src/utils/types.h b/src/utils/types.h
new file mode 100644
index 0000000..df4d8b3
--- /dev/null
+++ b/src/utils/types.h
@@ -0,0 +1,18 @@
+#pragma once
+
+#include <stdint.h>
+#include <stdio.h>
+
+#ifndef __cplusplus
+ #if (__STDC_VERSION__ < 202000L)
+ #include <stdint.h>
+typedef enum bool : uint8_t { false = 0, true = 1 } bool;
+ #endif
+#endif
+
+typedef struct SizedString {
+ char *str;
+ size_t size;
+} SizedString;
+
+#define ERROR_SIZE INT64_MAX