aboutsummaryrefslogtreecommitdiff
path: root/src/Utility/Utility.c
diff options
context:
space:
mode:
authorfrosty <gabriel@bwaaa.monster>2026-04-01 05:49:18 +0300
committerfrosty <gabriel@bwaaa.monster>2026-04-01 05:49:18 +0300
commit614bd26cb34b9b340e327d4b80927353bb5a5d0a (patch)
tree8d03df6a3f65eb13fa8d9ce8cb16cc27f46faaf2 /src/Utility/Utility.c
parentc6bdeecb2a8869fd7684fc56ed0047611d327cfc (diff)
downloadomnisearch-614bd26cb34b9b340e327d4b80927353bb5a5d0a.tar.gz
refactor: internationalise pagination and clean up related code
Diffstat (limited to 'src/Utility/Utility.c')
-rw-r--r--src/Utility/Utility.c99
1 files changed, 99 insertions, 0 deletions
diff --git a/src/Utility/Utility.c b/src/Utility/Utility.c
index 0d7a28e..bffda4d 100644
--- a/src/Utility/Utility.c
+++ b/src/Utility/Utility.c
@@ -1,5 +1,6 @@
#include "Utility.h"
#include <beaker.h>
+#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -32,3 +33,101 @@ char *get_locale(const char *default_locale) {
free(cookie);
return strdup(default_locale);
}
+
+int add_link_to_collection(const char *href, const char *label,
+ const char *class_name, char ****collection,
+ int **inner_counts, int current_count) {
+ char ***old_collection = *collection;
+ int *old_inner_counts = *inner_counts;
+ char ***new_collection =
+ (char ***)malloc(sizeof(char **) * (current_count + 1));
+ int *new_inner_counts =
+ (int *)malloc(sizeof(int) * (current_count + 1));
+
+ if (!new_collection || !new_inner_counts) {
+ free(new_collection);
+ free(new_inner_counts);
+ return current_count;
+ }
+
+ if (*collection && current_count > 0) {
+ memcpy(new_collection, *collection, sizeof(char **) * current_count);
+ }
+ if (*inner_counts && current_count > 0) {
+ memcpy(new_inner_counts, *inner_counts, sizeof(int) * current_count);
+ }
+
+ *collection = new_collection;
+ *inner_counts = new_inner_counts;
+
+ (*collection)[current_count] =
+ (char **)malloc(sizeof(char *) * LINK_FIELD_COUNT);
+ if (!(*collection)[current_count]) {
+ *collection = old_collection;
+ *inner_counts = old_inner_counts;
+ free(new_collection);
+ free(new_inner_counts);
+ return current_count;
+ }
+
+ (*collection)[current_count][0] = strdup(href ? href : "");
+ (*collection)[current_count][1] = strdup(label ? label : "");
+ (*collection)[current_count][2] = strdup(class_name ? class_name : "");
+
+ if (!(*collection)[current_count][0] || !(*collection)[current_count][1] ||
+ !(*collection)[current_count][2]) {
+ free((*collection)[current_count][0]);
+ free((*collection)[current_count][1]);
+ free((*collection)[current_count][2]);
+ free((*collection)[current_count]);
+ *collection = old_collection;
+ *inner_counts = old_inner_counts;
+ free(new_collection);
+ free(new_inner_counts);
+ return current_count;
+ }
+
+ (*inner_counts)[current_count] = LINK_FIELD_COUNT;
+
+ free(old_collection);
+ free(old_inner_counts);
+ return current_count + 1;
+}
+
+int build_pagination(int page, const char *locale,
+ char *(*href_builder)(int page, void *data), void *data,
+ char ****out_matrix, int **out_inner_counts) {
+ enum { PAGER_WINDOW_SIZE = 5 };
+
+ *out_matrix = NULL;
+ *out_inner_counts = NULL;
+ int count = 0;
+
+ int pager_start = page <= 3 ? 1 : page - 2;
+ int pager_end = pager_start + PAGER_WINDOW_SIZE - 1;
+
+ if (page > 1) {
+ char *href = href_builder(page - 1, data);
+ count = add_link_to_collection(href, "<", "pagination-btn prev",
+ out_matrix, out_inner_counts, count);
+ free(href);
+ }
+
+ for (int i = pager_start; i <= pager_end; i++) {
+ char label[16];
+ snprintf(label, sizeof(label), "%d", i);
+ char *href = href_builder(i, data);
+ count = add_link_to_collection(
+ href, label,
+ i == page ? "pagination-btn pagination-current" : "pagination-btn",
+ out_matrix, out_inner_counts, count);
+ free(href);
+ }
+
+ char *href = href_builder(page + 1, data);
+ count = add_link_to_collection(href, ">", "pagination-btn next",
+ out_matrix, out_inner_counts, count);
+ free(href);
+
+ return count;
+}