aboutsummaryrefslogtreecommitdiff
path: root/src/Config.c
diff options
context:
space:
mode:
authorfrosty <frosty@illegalfirearms.store>2026-01-21 21:41:17 -0500
committerfrosty <frosty@illegalfirearms.store>2026-01-21 21:41:17 -0500
commit12a71a892251146bfcd45002e4ce93ec9da15941 (patch)
tree131b230188998c4e83b623f2b5695fcbb00b6d8e /src/Config.c
parent50301a037c38d3197f85baf40d6b69122e33ee1b (diff)
added config
Diffstat (limited to 'src/Config.c')
-rw-r--r--src/Config.c67
1 files changed, 67 insertions, 0 deletions
diff --git a/src/Config.c b/src/Config.c
new file mode 100644
index 0000000..047bd00
--- /dev/null
+++ b/src/Config.c
@@ -0,0 +1,67 @@
+#include "Config.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+int load_config(const char *filename, Config *config) {
+ FILE *file = fopen(filename, "r");
+ if (!file) {
+ return -1;
+ }
+
+ char line[512];
+ char section[64] = "";
+
+ while (fgets(line, sizeof(line), file)) {
+
+ line[strcspn(line, "\r\n")] = 0;
+
+ if (line[0] == '\0' || line[0] == '#' || line[0] == ';') {
+ continue;
+ }
+
+ if (line[0] == '[') {
+ char *end = strchr(line, ']');
+ if (end) {
+ *end = '\0';
+ snprintf(section, sizeof(section), "%s", line + 1);
+ section[sizeof(section) - 1] = '\0';
+ }
+ continue;
+ }
+
+ char *delimiter = strchr(line, '=');
+ if (delimiter) {
+ *delimiter = '\0';
+ char *key = line;
+ char *value = delimiter + 1;
+
+ while (*key == ' ' || *key == '\t') key++;
+ while (*value == ' ' || *value == '\t') value++;
+
+ char *key_end = key + strlen(key) - 1;
+ while (key_end > key && (*key_end == ' ' || *key_end == '\t')) {
+ *key_end = '\0';
+ key_end--;
+ }
+
+ char *value_end = value + strlen(value) - 1;
+ while (value_end > value && (*value_end == ' ' || *value_end == '\t')) {
+ *value_end = '\0';
+ value_end--;
+ }
+
+ if (strcmp(section, "server") == 0) {
+ if (strcmp(key, "host") == 0) {
+ strncpy(config->host, value, sizeof(config->host) - 1);
+ config->host[sizeof(config->host) - 1] = '\0';
+ } else if (strcmp(key, "port") == 0) {
+ config->port = atoi(value);
+ }
+ }
+ }
+ }
+
+ fclose(file);
+ return 0;
+} \ No newline at end of file