aboutsummaryrefslogtreecommitdiff
path: root/src/Utility
diff options
context:
space:
mode:
authorfrosty <gabriel@bwaaa.monster>2026-03-13 13:33:30 -0400
committerfrosty <gabriel@bwaaa.monster>2026-03-13 13:33:30 -0400
commit4539593e6ad442dc2547ccab38a3c642ccd635e1 (patch)
treef291a5f603e0468650167f8aaba82d8c8135028e /src/Utility
parentef1cd7cc6060df0f5dcfada136dd99a695e8a0fc (diff)
downloadomnisearch-4539593e6ad442dc2547ccab38a3c642ccd635e1.tar.gz
feature: added currency conversion infobox
Diffstat (limited to 'src/Utility')
-rw-r--r--src/Utility/JsonHelper.c139
-rw-r--r--src/Utility/JsonHelper.h17
2 files changed, 156 insertions, 0 deletions
diff --git a/src/Utility/JsonHelper.c b/src/Utility/JsonHelper.c
new file mode 100644
index 0000000..1eb4098
--- /dev/null
+++ b/src/Utility/JsonHelper.c
@@ -0,0 +1,139 @@
+#include "JsonHelper.h"
+#include <math.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+static int is_whitespace_json(char c) {
+ return c == ' ' || c == '\t' || c == '\n' || c == '\r';
+}
+
+int json_parse_float_map(const char *json, const char *target_key,
+ JsonFloatMap *result) {
+ memset(result, 0, sizeof(JsonFloatMap));
+
+ if (!json || !target_key)
+ return 0;
+
+ const char *obj_start = strchr(json, '{');
+ if (!obj_start)
+ return 0;
+
+ const char *rates_start = strstr(obj_start, target_key);
+ if (!rates_start)
+ return 0;
+
+ rates_start = strchr(rates_start, '{');
+ if (!rates_start)
+ return 0;
+
+ const char *p = rates_start + 1;
+ while (*p && *p != '}') {
+ while (*p && (*p == ' ' || *p == '\n' || *p == '\t' || *p == ','))
+ p++;
+
+ if (*p == '}')
+ break;
+
+ if (*p != '"')
+ break;
+ p++;
+
+ char key[32] = {0};
+ size_t key_len = 0;
+ while (*p && *p != '"' && key_len < 31) {
+ key[key_len++] = *p++;
+ }
+ if (*p != '"')
+ break;
+ p++;
+
+ while (*p && *p != ':')
+ p++;
+ if (*p != ':')
+ break;
+ p++;
+
+ while (*p && is_whitespace_json(*p))
+ p++;
+
+ double value = 0;
+ int has_digit = 0;
+ while (*p >= '0' && *p <= '9') {
+ value = value * 10 + (*p - '0');
+ has_digit = 1;
+ p++;
+ }
+ if (*p == '.') {
+ p++;
+ double frac = 0.1;
+ while (*p >= '0' && *p <= '9') {
+ value += (*p - '0') * frac;
+ frac *= 0.1;
+ has_digit = 1;
+ p++;
+ }
+ }
+
+ if (has_digit && key_len > 0) {
+ memcpy(result->keys[result->count], key, key_len);
+ result->keys[result->count][key_len] = '\0';
+ result->values[result->count] = value;
+ result->count++;
+ if (result->count >= 256)
+ break;
+ }
+ }
+
+ return result->count > 0;
+}
+
+double json_get_float(const char *json, const char *key) {
+ if (!json || !key)
+ return 0;
+
+ const char *key_pos = strstr(json, key);
+ if (!key_pos)
+ return 0;
+
+ const char *colon = strchr(key_pos, ':');
+ if (!colon)
+ return 0;
+
+ colon++;
+ while (*colon && is_whitespace_json(*colon))
+ colon++;
+
+ return strtod(colon, NULL);
+}
+
+char *json_get_string(const char *json, const char *key) {
+ if (!json || !key)
+ return NULL;
+
+ static char buffer[256];
+
+ const char *key_pos = strstr(json, key);
+ if (!key_pos)
+ return NULL;
+
+ const char *colon = strchr(key_pos, ':');
+ if (!colon)
+ return NULL;
+
+ colon++;
+ while (*colon && is_whitespace_json(*colon))
+ colon++;
+
+ if (*colon != '"')
+ return NULL;
+ colon++;
+
+ size_t len = 0;
+ while (*colon && *colon != '"' && len < 255) {
+ buffer[len++] = *colon++;
+ }
+ buffer[len] = '\0';
+
+ return buffer;
+}
diff --git a/src/Utility/JsonHelper.h b/src/Utility/JsonHelper.h
new file mode 100644
index 0000000..62034b8
--- /dev/null
+++ b/src/Utility/JsonHelper.h
@@ -0,0 +1,17 @@
+#ifndef JSONHELPER_H
+#define JSONHELPER_H
+
+#include <stddef.h>
+
+typedef struct {
+ double values[256];
+ char keys[256][32];
+ size_t count;
+} JsonFloatMap;
+
+int json_parse_float_map(const char *json, const char *target_key,
+ JsonFloatMap *result);
+double json_get_float(const char *json, const char *key);
+char *json_get_string(const char *json, const char *key);
+
+#endif