diff options
| author | frosty <gabriel@bwaaa.monster> | 2026-04-01 22:39:22 +0300 |
|---|---|---|
| committer | frosty <gabriel@bwaaa.monster> | 2026-04-01 22:39:22 +0300 |
| commit | 8176078105d3ce0331cf5deb3ff2e564d5221d05 (patch) | |
| tree | da0a33cf66b7c76c276e0cc3c2511f61b3558435 /src/Utility | |
| parent | 614bd26cb34b9b340e327d4b80927353bb5a5d0a (diff) | |
| download | omnisearch-8176078105d3ce0331cf5deb3ff2e564d5221d05.tar.gz | |
feat: configure search engines in user settings
Diffstat (limited to 'src/Utility')
| -rw-r--r-- | src/Utility/Utility.c | 84 | ||||
| -rw-r--r-- | src/Utility/Utility.h | 4 |
2 files changed, 88 insertions, 0 deletions
diff --git a/src/Utility/Utility.c b/src/Utility/Utility.c index bffda4d..16656bb 100644 --- a/src/Utility/Utility.c +++ b/src/Utility/Utility.c @@ -1,4 +1,5 @@ #include "Utility.h" +#include "../Scraping/Scraping.h" #include <beaker.h> #include <stdio.h> #include <stdlib.h> @@ -34,6 +35,89 @@ char *get_locale(const char *default_locale) { return strdup(default_locale); } +static int engine_id_casecmp(const char *a, const char *b) { + while (*a && *b) { + char la = *a; + char lb = *b; + if (la >= 'A' && la <= 'Z') la = la - 'A' + 'a'; + if (lb >= 'A' && lb <= 'Z') lb = lb - 'A' + 'a'; + if (la != lb) return 0; + a++; + b++; + } + return *a == *b; +} + +int is_engine_id_enabled(const char *engine_id) { + if (!engine_id) return 0; + for (int i = 0; i < ENGINE_COUNT; i++) { + if (ENGINE_REGISTRY[i].enabled && + engine_id_casecmp(ENGINE_REGISTRY[i].id, engine_id)) { + return 1; + } + } + return 0; +} + +int get_user_engines(char ***out_ids, int *out_count) { + *out_ids = NULL; + *out_count = 0; + + char *cookie = get_cookie("engines"); + if (!cookie || cookie[0] == '\0') { + free(cookie); + return -1; + } + + char **ids = NULL; + int count = 0; + + char *copy = strdup(cookie); + if (!copy) { + free(cookie); + return -1; + } + + char *saveptr; + char *token = strtok_r(copy, ",", &saveptr); + while (token) { + while (*token == ' ' || *token == '\t') + token++; + + if (token[0] != '\0' && is_engine_id_enabled(token)) { + char **new_ids = realloc(ids, sizeof(char *) * (count + 1)); + if (new_ids) { + ids = new_ids; + ids[count] = strdup(token); + count++; + } + } + + token = strtok_r(NULL, ",", &saveptr); + } + + free(copy); + free(cookie); + + if (count == 0) { + free(ids); + return -1; + } + + *out_ids = ids; + *out_count = count; + return 0; +} + +int user_engines_contains(const char *engine_id, char **ids, int count) { + if (!engine_id || !ids) return 0; + for (int i = 0; i < count; i++) { + if (engine_id_casecmp(ids[i], engine_id)) + return 1; + } + return 0; +} + int add_link_to_collection(const char *href, const char *label, const char *class_name, char ****collection, int **inner_counts, int current_count) { diff --git a/src/Utility/Utility.h b/src/Utility/Utility.h index 3863bc2..822c972 100644 --- a/src/Utility/Utility.h +++ b/src/Utility/Utility.h @@ -9,6 +9,10 @@ int hex_to_int(char c); char *get_theme(const char *default_theme); char *get_locale(const char *default_locale); +int is_engine_id_enabled(const char *engine_id); +int get_user_engines(char ***out_ids, int *out_count); +int user_engines_contains(const char *engine_id, char **ids, int count); + int add_link_to_collection(const char *href, const char *label, const char *class_name, char ****collection, int **inner_counts, int current_count); |
