summaryrefslogtreecommitdiff
path: root/ini
diff options
context:
space:
mode:
authorAndrew Opalach <andrew@akon.city> 2026-07-18 13:34:49 -0400
committerAndrew Opalach <andrew@akon.city> 2026-07-18 13:45:01 -0400
commit72f5ec3ce777fc05150856f1841e60b0f5c9f8f7 (patch)
tree9b26fd3b314bcf3a2259704c076d819212f172f0 /ini
parente6b1631d27f9fe54f3cdd3d7ee1e65d726927e3c (diff)
downloadcetris-72f5ec3ce777fc05150856f1841e60b0f5c9f8f7.tar.gz
cetris-72f5ec3ce777fc05150856f1841e60b0f5c9f8f7.tar.bz2
cetris-72f5ec3ce777fc05150856f1841e60b0f5c9f8f7.zip
restore old frontends as they were
Minimal changes, just enough to get them building and running. Signed-off-by: Andrew Opalach <andrew@akon.city>
Diffstat (limited to 'ini')
-rw-r--r--ini/bench.c52
-rw-r--r--ini/gen_ini_file.py35
-rw-r--r--ini/ini.h114
-rw-r--r--ini/test.ini15
4 files changed, 216 insertions, 0 deletions
diff --git a/ini/bench.c b/ini/bench.c
new file mode 100644
index 0000000..00dc170
--- /dev/null
+++ b/ini/bench.c
@@ -0,0 +1,52 @@
+#include <time.h>
+#include <stdio.h>
+
+#include "ini.h"
+
+ini_parser parser;
+
+#define TEST_FILE "test.ini"
+
+__attribute__((optimize("unroll-loops")))
+double run_bench(int count) {
+ clock_t start, end;
+ start = clock();
+
+ for (int i = 0; i < count; i++) {
+ get_ini_value(&parser, "Test1", "Test");
+ get_ini_value(&parser, "Test1", "String");
+ get_ini_value(&parser, "Test1", "Int");
+ get_ini_value(&parser, "Test2", "Test");
+ get_ini_value(&parser, "Test2", "String");
+ get_ini_value(&parser, "Test2", "Int");
+ }
+
+ end = clock();
+
+ return ((double) (end - start)) / CLOCKS_PER_SEC;
+}
+
+int main(void) {
+ if (!load_ini_file(&parser, TEST_FILE)) {
+ printf("failed to load file\n");
+ }
+
+ printf("Test results:\n");
+
+ printf("'%s'\n", get_ini_value(&parser, "Test1", "Test"));
+ printf("'%s'\n", get_ini_value(&parser, "Test1", "String"));
+ printf("'%s'\n", get_ini_value(&parser, "Test1", "Int"));
+ printf("'%s'\n", get_ini_value(&parser, "Test2", "Test"));
+ printf("'%s'\n", get_ini_value(&parser, "Test2", "String"));
+ printf("'%s'\n", get_ini_value(&parser, "Test2", "Int"));
+
+ printf("Test on file '%s' took %f seconds for 60 queries\n", TEST_FILE, run_bench(10));
+ printf("Test on file '%s' took %f seconds for 600 queries\n", TEST_FILE, run_bench(100));
+ printf("Test on file '%s' took %f seconds for 6000 queries\n", TEST_FILE, run_bench(1000));
+ printf("Test on file '%s' took %f seconds for 60000 queries\n", TEST_FILE, run_bench(10000));
+ printf("Test on file '%s' took %f seconds for 600000 queries\n", TEST_FILE, run_bench(100000));
+
+ unload_ini_file(&parser);
+
+ return 0;
+}
diff --git a/ini/gen_ini_file.py b/ini/gen_ini_file.py
new file mode 100644
index 0000000..acf078d
--- /dev/null
+++ b/ini/gen_ini_file.py
@@ -0,0 +1,35 @@
+import base64
+from random import randint
+
+MAX_LINES = 12
+
+def random_line():
+ num = randint(0, 99999999);
+ keyb64 = base64.b64encode(bytes(ascii(num), encoding="ASCII")) # i have no idea
+ key = str(keyb64, encoding="utf8")
+ key = key[:len(key)-2]
+
+ valueb64 = base64.b32encode(bytes(ascii(num), encoding="ASCII")) # yeah
+ value = str(valueb64, encoding="utf8")
+ value = value[:len(value)-3]
+ return key + "=" + value + "\n"
+
+def random_section():
+ num = randint(0, 99999999);
+ secb64 = base64.b64encode(bytes(ascii(num), encoding="ASCII"))
+ sec = str(secb64, encoding="utf8")
+ return '[' + sec[:len(sec)-2] + ']\n'
+
+f = open("random.ini", "w")
+
+lines = 0
+
+f.write(random_section())
+while(lines <= MAX_LINES):
+ if randint(0, 5) == 1:
+ f.write('\n')
+ f.write(random_section())
+ f.write(random_line())
+ lines += 1
+
+f.close()
diff --git a/ini/ini.h b/ini/ini.h
new file mode 100644
index 0000000..975df60
--- /dev/null
+++ b/ini/ini.h
@@ -0,0 +1,114 @@
+#ifndef INI_H
+#define INI_H
+
+
+#define _CRT_SECURE_NO_WARNINGS
+#include <stdio.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define MAX_LINE_SIZE 256
+#define MAX_VALUE_SIZE 128
+
+typedef struct {
+ char* key;
+ char* value;
+} pair;
+
+typedef struct {
+ FILE* file;
+} ini_parser;
+
+/* loads the file at the given path into memory */
+static bool load_ini_file(ini_parser* p, const char* path) {
+ p->file = fopen(path, "r");
+ if (p->file == NULL)
+ return false;
+ return true;
+}
+
+/* unload file */
+void unload_ini_file(ini_parser* p) {
+ fclose(p->file);
+}
+
+static bool parse_line(char* line, pair* c) {
+ int32_t equal_index = strchr(line, '=')-line;
+ if (equal_index < 0) return false;
+
+ size_t len = strlen(line);
+ c->key = (char*)malloc(equal_index);
+ c->value = (char*)malloc(len - equal_index);
+
+ uint32_t key_start = 0; uint32_t value_start = 0;
+ bool start_value = false; bool start_key = false;
+ uint32_t prev_space = 0;
+ for (uint32_t i = 0; i < len; i++) {
+ if (i < (uint32_t)equal_index) {
+ if (line[i] == ' ' && !prev_space) {
+ prev_space = key_start;
+ } else if (line[i] != ' ') {
+ start_key = true;
+ prev_space = 0;
+ }
+ if (start_key)
+ c->key[key_start++] = line[i];
+ } else if (i > (uint32_t)equal_index) {
+ if (line[i] != ' ')
+ start_value = true;
+ if (start_value)
+ c->value[value_start++] = line[i];
+ }
+ }
+ if (prev_space) key_start = prev_space;
+ c->key[key_start] = '\0';
+ c->value[value_start] = '\0';
+ return true;
+}
+
+/* return value of matching pair with the given key and section */
+char* get_ini_value(ini_parser* p, const char* sec, const char* key) {
+ rewind(p->file); // reset file
+
+ char* value = (char*)malloc(MAX_VALUE_SIZE);
+
+ char* current_section = "root";
+ bool wait_till_next_section = true;
+
+ char* line = (char*)malloc(MAX_LINE_SIZE);
+ while (fgets(line, MAX_LINE_SIZE, p->file) != NULL) {
+ size_t len = strcspn(line, "\r\n");
+ line[len] = '\0';
+ if (len < 3) continue;
+ char front = *line; // first character for the line
+ if (front == ';' || front == '#') continue;
+ else if (front == '[') {
+ line++;
+ current_section = (char*)malloc(len + 1);
+ strncpy(current_section, line, len);
+ current_section[len-2] = '\0';
+ wait_till_next_section = false;
+ continue;
+ }
+
+ if (wait_till_next_section) continue;
+
+ if (strcmp(current_section, sec) == 0) {
+ pair p;
+ if (!parse_line(line, &p))
+ continue;
+ if (strcmp(p.key, key) == 0) {
+ strcpy(value, p.value);
+ free(p.key); free(p.value);
+ free(current_section);
+ return value;
+ }
+ } else wait_till_next_section = true;
+ }
+ free(current_section);
+ return NULL;
+}
+
+#endif /* INI_H */
diff --git a/ini/test.ini b/ini/test.ini
new file mode 100644
index 0000000..4d6bc0a
--- /dev/null
+++ b/ini/test.ini
@@ -0,0 +1,15 @@
+#comment1
+;comment2
+
+[Test1]
+Test=true
+String ="aaaaa"
+Int=111111
+
+; comment3
+# comment4
+
+[Test2]
+ Test= true
+String = "aaaaa"
+Int =111111