feat: configure search engines in user settings
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user