about summary refs log tree commit diff
path: root/boot/common.c
diff options
context:
space:
mode:
authorMel <mel@rnrd.eu>2025-05-31 03:29:51 +0200
committerMel <mel@rnrd.eu>2025-05-31 03:29:51 +0200
commita422f9aead499a526179ba2df2aff1aa44fe48d6 (patch)
treef597bed2231e3ee05ce1d4f6c9901ed5b6a3882b /boot/common.c
parent41e0e31b8586c1f93b5e65cd62ef910227a6677d (diff)
downloadcatskill-a422f9aead499a526179ba2df2aff1aa44fe48d6.tar.zst
catskill-a422f9aead499a526179ba2df2aff1aa44fe48d6.zip
Keyword-less variable declaration parsing
Signed-off-by: Mel <mel@rnrd.eu>
Diffstat (limited to 'boot/common.c')
-rw-r--r--boot/common.c41
1 files changed, 41 insertions, 0 deletions
diff --git a/boot/common.c b/boot/common.c
index d814538..65dc780 100644
--- a/boot/common.c
+++ b/boot/common.c
@@ -166,6 +166,47 @@ string_print(struct String s)
     printf("%.*s", (int32)s.length, s.data);
 }
 
+#define STRING_ARRAY_MAX 8
+
+// a string array, used for storing multiple strings.
+// if we ever need more strings, just bump `STRING_ARRAY_MAX` up.
+struct String_Array
+{
+    struct String strings[STRING_ARRAY_MAX];
+    uint count;
+};
+
+// initializes a string array with no strings.
+struct String_Array
+string_array_new()
+{
+    return (struct String_Array){
+        .strings = { 0 },
+        .count = 0,
+    };
+}
+
+// adds a string to the string array.
+bool
+string_array_add(struct String_Array* array, struct String string)
+{
+    if (array->count >= STRING_ARRAY_MAX) return false;
+
+    array->strings[array->count++] = string;
+    return true;
+}
+
+struct String
+string_array_at(const struct String_Array* array, uint index)
+{
+    check(index < array->count, "index out of bounds");
+    return array->strings[index];
+}
+
+#define STRING_ARRAY_FOR_EACH(cursor, str, array) \
+    struct String str = array.strings[0]; \
+    for (uint cursor = 0; cursor < array.count; str = array.strings[++cursor])
+
 // single iteration of the CRC32 checksum algorithm
 // described in POSIX.
 // see: https://pubs.opengroup.org/onlinepubs/9799919799/utilities/cksum.html