aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorbonmas14 <bonmas14@gmail.com>2025-08-03 19:24:03 +0000
committerbonmas14 <bonmas14@gmail.com>2025-08-03 19:24:03 +0000
commit471b539bdbf658ff7924b7500f89fd237df8be9b (patch)
treea6a0b1d8a7a37ebe288cd7e1accf9b16dee203aa /src
parenta4d37d76512c293b12aab1f77961f96d572557b7 (diff)
downloadungrateful-471b539bdbf658ff7924b7500f89fd237df8be9b.tar.gz
ungrateful-471b539bdbf658ff7924b7500f89fd237df8be9b.zip
Reordering of stuff + plans
Diffstat (limited to 'src')
-rw-r--r--src/cyn_log.c73
-rw-r--r--src/cynic.c4
-rw-r--r--src/cynic.h63
-rw-r--r--src/cynic_internal.h31
-rw-r--r--src/un_list.c5
-rw-r--r--src/un_log.c71
-rw-r--r--src/un_memory.c7
-rw-r--r--src/un_strings.c38
-rw-r--r--src/ungrateful.c1
-rw-r--r--src/ungrateful.h33
10 files changed, 207 insertions, 119 deletions
diff --git a/src/cyn_log.c b/src/cyn_log.c
new file mode 100644
index 0000000..d2d18f2
--- /dev/null
+++ b/src/cyn_log.c
@@ -0,0 +1,73 @@
+#include <stdio.h>
+
+Log_Level cyn_current_log_level = UN_LOG_INFO;
+
+void cyn_log_write_internal(Log_Level level, String format, va_list vaptr) {
+ Allocator talloc;
+ String output;
+
+ if (level < cyn_current_log_level) return;
+
+
+ talloc = un_allocator_get_temporary();
+
+ switch (level) {
+ case UN_LOG_TRACE:
+ output = UN_STR("[TRACE] ");
+ break;
+ case UN_LOG_DEBUG:
+ output = UN_STR("[DEBUG] ");
+ break;
+ case UN_LOG_INFO:
+ output = UN_STR("[INFO] ");
+ break;
+ case UN_LOG_WARNING:
+ output = UN_STR("[WARNING] ");
+ break;
+ case UN_LOG_ERROR:
+ output = UN_STR("[ERROR] ");
+ break;
+ case UN_LOG_FATAL:
+ output = UN_STR("[FATAL] ");
+ break;
+ default:
+ break;
+ }
+
+ output = un_string_concat(output, un_string_vformat(talloc, format, vaptr), talloc);
+
+ switch (level) {
+ case UN_LOG_RAW:
+ break;
+ case UN_LOG_FATAL: // @todo, logging should be part of Cynic.
+ default:
+ output = un_string_concat(output, UN_STR("\n"), talloc);
+ break;
+ }
+
+ fprintf(stderr, CSTR un_string_to_cstring(output, talloc));
+}
+
+extern void cyn_log_write_cstring(Log_Level level, u8 *format, ...) {
+ va_list vaptr;
+ String temp;
+
+ if (level < cyn_current_log_level) return;
+
+ temp.size = un_string_get_length(format);
+ temp.data = format;
+
+ va_start(vaptr, format);
+ cyn_log_write_internal(level, temp, vaptr);
+ va_end(vaptr);
+}
+
+void cyn_log_write(Log_Level level, String format, ...) {
+ va_list vaptr;
+
+ if (level < cyn_current_log_level) return;
+
+ va_start(vaptr, format);
+ cyn_log_write_internal(level, format, vaptr);
+ va_end(vaptr);
+}
diff --git a/src/cynic.c b/src/cynic.c
new file mode 100644
index 0000000..e0831e7
--- /dev/null
+++ b/src/cynic.c
@@ -0,0 +1,4 @@
+#include "cynic.h"
+#include "cynic_internal.h"
+
+#include "cyn_log.c"
diff --git a/src/cynic.h b/src/cynic.h
new file mode 100644
index 0000000..2aeb6f9
--- /dev/null
+++ b/src/cynic.h
@@ -0,0 +1,63 @@
+#if !defined(CYNIC_H)
+# define CYNIC_H
+/*
+ Cynic - platform layer for games and programs.
+
+ LICENSE:
+
+ Copyright (C) 2025 Bogdan Masyutin (bonmas14)
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+
+ Bogdan Masyutin - bonmas14@gmail.com
+*/
+
+#include <ungrateful.h>
+
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
+/* ---- Library loading API ---- */
+/* ------- Threading API ------- */
+/* -------- Logging API -------- */
+
+typedef enum {
+ UN_LOG_TRACE,
+ UN_LOG_DEBUG,
+ UN_LOG_INFO,
+ UN_LOG_WARNING,
+ UN_LOG_ERROR,
+ UN_LOG_FATAL,
+ UN_LOG_RAW
+} Log_Level;
+
+extern Log_Level cyn_current_log_level;
+
+extern void cyn_log_write(Log_Level level, String format, ...);
+extern void cyn_log_write_cstring(Log_Level level, u8 *format, ...);
+
+/* --------- Timer API --------- */
+/* --------- File API ---------- */
+/* ------ Filesystem API ------- */
+/* --------- Config API -------- */
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif // CYNIC_H
diff --git a/src/cynic_internal.h b/src/cynic_internal.h
new file mode 100644
index 0000000..1c1e8bb
--- /dev/null
+++ b/src/cynic_internal.h
@@ -0,0 +1,31 @@
+#if !defined(CYNIC_INTERNAL_H)
+# define CYNIC_INTERNAL_H
+/*
+ Internal header for Cynic, do not use as it defines platform specific code
+
+ LICENCE:
+
+ Copyright (C) 2025 Bogdan Masyutin (bonmas14)
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+
+ Bogdan Masyutin - bonmas14@gmail.com
+*/
+
+/* Windows threading, loading libraries and so on. */
+
+#endif // CYNIC_INTERNAL_H
diff --git a/src/un_list.c b/src/un_list.c
index a670de0..133dc12 100644
--- a/src/un_list.c
+++ b/src/un_list.c
@@ -47,15 +47,16 @@ static b32 list_grow_if_needed(List *list) {
return true;
}
-void un_list_append(List *list, void *data) {
+b32 un_list_append(List *list, void *data) {
void *addr;
if (list_grow_if_needed(list)) {
addr = (u8*)list->data + list->count * list->element_size;
un_memory_copy(addr, data, list->element_size);
list->count++;
+ return true;
} else {
- un_log_write_cstring(UN_LOG_ERROR, UN_CSTR "Failed to grow list");
+ return false;
}
}
diff --git a/src/un_log.c b/src/un_log.c
deleted file mode 100644
index ef5bb02..0000000
--- a/src/un_log.c
+++ /dev/null
@@ -1,71 +0,0 @@
-#include <stdio.h>
-
-Log_Level un_current_log_level = UN_LOG_INFO;
-
-u8 log_buffer[UN_KB(4)];
-
-void un_log_write_internal(Log_Level level, String format, va_list vaptr) {
- if (level < un_current_log_level) return;
-
- switch (level) {
- case UN_LOG_TRACE:
- fprintf(stderr, "[TRACE] ");
- break;
- case UN_LOG_DEBUG:
- fprintf(stderr, "[DEBUG] ");
- break;
- case UN_LOG_INFO:
- fprintf(stderr, "[INFO] ");
- break;
- case UN_LOG_WARNING:
- fprintf(stderr, "[WARNING] ");
- break;
- case UN_LOG_ERROR:
- fprintf(stderr, "[ERROR] ");
- break;
- case UN_LOG_FATAL:
- fprintf(stderr, "[FATAL] ");
- break;
- default:
- break;
- }
-
- sprintf(CSTR log_buffer, "%.*s", (int)format.size, format.data);
- vfprintf(stderr, CSTR log_buffer, vaptr);
-
- switch (level) {
- case UN_LOG_RAW:
- break;
- case UN_LOG_FATAL:
- fprintf(stderr, "\n");
- assert(false);
- break;
- default:
- fprintf(stderr, "\n");
- break;
- }
-}
-
-extern void un_log_write_cstring(Log_Level level, u8 *format, ...) {
- va_list vaptr;
- String temp;
-
- if (level < un_current_log_level) return;
-
- temp.size = un_string_get_length(format);
- temp.data = format;
-
- va_start(vaptr, format);
- un_log_write_internal(level, temp, vaptr);
- va_end(vaptr);
-}
-
-void un_log_write(Log_Level level, String format, ...) {
- va_list vaptr;
-
- if (level < un_current_log_level) return;
-
- va_start(vaptr, format);
- un_log_write_internal(level, format, vaptr);
- va_end(vaptr);
-}
diff --git a/src/un_memory.c b/src/un_memory.c
index 9ff316b..84f5f14 100644
--- a/src/un_memory.c
+++ b/src/un_memory.c
@@ -111,12 +111,12 @@ static void temp_reset(void) {
static void *temp_alloc(u64 size) {
if ((temp.index + size) > UN_TEMP_SIZE) {
- un_log_write_cstring(UN_LOG_ERROR, UN_CSTR "Temp allocator wrapped!");
+ // @todo: Decice what is the best behaviour for wrapping
+ assert(false);
temp_reset();
}
if ((temp.index + size) > UN_TEMP_SIZE) {
- un_log_write_cstring(UN_LOG_ERROR, UN_CSTR "Too much space requested!");
return NULL;
}
@@ -164,7 +164,6 @@ static Arena *arena_create(u64 size) {
arena = CLITERAL(Arena*)un_memory_alloc(size, alloc);
if (!arena) {
- un_log_write_cstring(UN_LOG_FATAL, UN_CSTR "Buy mem, failed to create arena!");
return NULL;
}
@@ -206,10 +205,8 @@ static ALLOCATOR_PROC_SIGNATURE(un_arena_alloc_proc) {
case UN_ALLOC_MSG_ALLOCATE:
return arena_allocate(size, (Arena*)data);
case UN_ALLOC_MSG_REALLOCATE:
- un_log_write_cstring(UN_LOG_ERROR, UN_CSTR "Arena doesn't reallocate.");
break;
case UN_ALLOC_MSG_FREE:
- un_log_write_cstring(UN_LOG_ERROR, UN_CSTR "Arena doesn't free it's memory, please destroy arena itself.");
break;
case UN_ALLOC_MSG_SELF_DELETE:
arena_delete((Arena*)data);
diff --git a/src/un_strings.c b/src/un_strings.c
index 7d73d0b..ff74c39 100644
--- a/src/un_strings.c
+++ b/src/un_strings.c
@@ -32,8 +32,6 @@ String un_string_copy(String source, Allocator alloc) {
u8 *mem;
String result;
- assert(source.size >= 0);
-
mem = (u8*)un_memory_alloc(source.size, alloc);
un_memory_copy(mem, source.data, (u64)source.size);
@@ -64,8 +62,6 @@ s32 un_string_compare(String left, String right) {
String un_string_concat(String left, String right, Allocator alloc) {
String result = { 0 };
- assert(left.size >= 0 && right.size >= 0);
-
if (left.size == right.size && left.size == 0) {
return result;
}
@@ -89,7 +85,7 @@ String un_string_concat(String left, String right, Allocator alloc) {
}
String un_string_swap(String input, u8 from, u8 to, Allocator alloc) {
- s64 i;
+ u64 i;
String output;
output = un_string_copy(input, alloc);
@@ -107,7 +103,7 @@ String un_string_swap(String input, u8 from, u8 to, Allocator alloc) {
List un_string_split(String input, String pattern, Allocator alloc) {
List splits;
String string;
- s64 i, start;
+ u64 i, start;
u64 matches, size;
u8 *buffer;
@@ -196,18 +192,17 @@ String un_string_join(List string_list, String separator, Allocator alloc) {
return un_string_copy(cont, alloc);
}
-String un_string_substring(String input, s64 start, s64 max_size) {
+String un_string_substring(String input, u64 start, u64 max_size) {
String slice;
- assert(start >= 0);
assert(max_size > 0);
- slice.size = UN_MIN(max_size, (input.size - (s64)max_size));
+ slice.size = UN_MIN(max_size, (input.size - (u64)max_size));
slice.data = input.data + start;
if (start > (input.size - max_size)) {
- slice.size = UN_MIN(max_size, (input.size - (s64)max_size));
+ slice.size = UN_MIN(max_size, (input.size - (u64)max_size));
} else {
slice.size = max_size;
}
@@ -217,7 +212,7 @@ String un_string_substring(String input, s64 start, s64 max_size) {
}
s64 un_string_index_of(String input, u8 value, u64 skip_count) {
- s64 i;
+ u64 i;
for (i = 0; i < input.size; i++) {
if (input.data[i] != value) {
@@ -233,12 +228,12 @@ s64 un_string_index_of(String input, u8 value, u64 skip_count) {
return -1;
}
-extern s64 un_string_index_of_last(String input, u8 value) {
+s64 un_string_index_of_last(String input, u8 value) {
s64 i, index;
index = -1;
- for (i = 0; i < input.size; i++) {
+ for (i = 0; i < (s64)input.size; i++) {
if (input.data[i] == value) index = i;
}
@@ -319,16 +314,23 @@ static String format_s64(s64 value) {
String un_string_format(Allocator alloc, String buffer, ...) {
va_list args;
- String s;
+ String result;
+ va_start(args, buffer);
+ result = un_string_vformat(alloc, buffer, args);
+ va_end(args);
+ return result;
+}
+
+String un_string_vformat(Allocator alloc, String buffer, va_list args) {
List output;
- s64 i, j;
+ String s;
+ u64 i, j;
u32 b;
Allocator talloc = un_allocator_get_temporary();
+ // @todo @bug: this would fail on realloc, because we need to realloc
output = un_list_create(UN_KB(1), sizeof(u8), talloc);
- va_start(args, buffer);
-
for (i = 0; i < buffer.size; i++) {
if (buffer.data[i] != '%') {
un_list_append(&output, buffer.data + i);
@@ -374,8 +376,6 @@ String un_string_format(Allocator alloc, String buffer, ...) {
}
}
- va_end(args);
-
s.size = (s64)output.count;
s.data = output.data;
diff --git a/src/ungrateful.c b/src/ungrateful.c
index 1ceefd6..f0c2346 100644
--- a/src/ungrateful.c
+++ b/src/ungrateful.c
@@ -3,4 +3,3 @@
#include "un_memory.c"
#include "un_strings.c"
#include "un_list.c"
-#include "un_log.c"
diff --git a/src/ungrateful.h b/src/ungrateful.h
index 8d1e70e..e3c52cf 100644
--- a/src/ungrateful.h
+++ b/src/ungrateful.h
@@ -146,14 +146,14 @@ extern List un_list_create(u64 start_capacity, u64 element_size, Allocator alloc
extern void un_list_destroy(List *list);
extern List un_list_clone(List *list, Allocator alloc);
-extern void un_list_append(List *list, void *data);
+extern b32 un_list_append(List *list, void *data); /* Returns true if succeed. */
extern void *un_list_get(List *list, u64 index);
extern void un_list_remove(List *list, u64 index);
/* ---- no-wide string API ---- */
typedef struct {
- s64 size;
+ u64 size;
u8 *data;
} String;
@@ -170,30 +170,21 @@ extern String un_string_swap(String input, u8 from, u8 to, Allocator alloc);
extern List un_string_split(String input, String pattern, Allocator alloc);
extern String un_string_join(List string_list, String separator, Allocator alloc);
-extern String un_string_substring(String input, s64 start, s64 max_size);
+extern String un_string_substring(String input, u64 start, u64 max_size);
extern s64 un_string_index_of(String input, u8 value, u64 skip_count);
extern s64 un_string_index_of_last(String input, u8 value);
+/* Simplest formatter, supports:
+ *
+ * %% - %
+ * %c - char (internally u32)
+ * %u - u64
+ * %d - s64
+ * %s - String
+ * */
extern String un_string_format(Allocator alloc, String buffer, ...);
-extern String un_string_tformat(String buffer, ...);
-
-/* ---- Logging API ---- */
-
-typedef enum {
- UN_LOG_TRACE,
- UN_LOG_DEBUG,
- UN_LOG_INFO,
- UN_LOG_WARNING,
- UN_LOG_ERROR,
- UN_LOG_FATAL,
- UN_LOG_RAW
-} Log_Level;
-
-extern Log_Level un_current_log_level;
-
-extern void un_log_write(Log_Level level, String format, ...);
-extern void un_log_write_cstring(Log_Level level, u8 *format, ...);
+extern String un_string_vformat(Allocator alloc, String buffer, va_list args);
#if defined(__cplusplus)
}