aboutsummaryrefslogtreecommitdiff
path: root/src/Utility/Utility.c
diff options
context:
space:
mode:
authorfrosty <gabriel@bwaaa.monster>2026-08-08 12:56:26 -0400
committerfrosty <gabriel@bwaaa.monster>2026-08-08 12:56:26 -0400
commitd93fead0aeaed1b7860fe132476c81c1845ce756 (patch)
tree0cbf826c0ea3e9885a1d7be722e1f7e8108ee0a8 /src/Utility/Utility.c
parent296021e0ce7e27db837638076c3316a4af35b1b7 (diff)
downloadomnisearch-d93fead0aeaed1b7860fe132476c81c1845ce756.tar.gz
refactor: centralise matrix ownership and request cleanup
Diffstat (limited to 'src/Utility/Utility.c')
-rw-r--r--src/Utility/Utility.c100
1 files changed, 53 insertions, 47 deletions
diff --git a/src/Utility/Utility.c b/src/Utility/Utility.c
index 1428722..9c05434 100644
--- a/src/Utility/Utility.c
+++ b/src/Utility/Utility.c
@@ -206,63 +206,69 @@ int user_engines_contains(const char *engine_id, char **ids, int count) {
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) {
- 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));
+int append_string_row(char ****matrix, int **inner_counts, int row_count,
+ const char *const *values, int field_count) {
+ if (!matrix || !inner_counts || !values || field_count <= 0)
+ return row_count;
- if (!new_collection || !new_inner_counts) {
- free(new_collection);
- free(new_inner_counts);
- return current_count;
- }
+ char **row = calloc((size_t)field_count, sizeof(*row));
+ if (!row)
+ return row_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);
+ for (int i = 0; i < field_count; i++) {
+ row[i] = strdup(values[i] ? values[i] : "");
+ if (!row[i]) {
+ for (int j = 0; j < i; j++)
+ free(row[j]);
+ free(row);
+ return row_count;
+ }
}
- *collection = new_collection;
- *inner_counts = new_inner_counts;
+ char ***new_matrix = malloc(sizeof(*new_matrix) * (row_count + 1));
+ int *new_counts = malloc(sizeof(*new_counts) * (row_count + 1));
+ if (!new_matrix || !new_counts) {
+ free(new_matrix);
+ free(new_counts);
+ for (int i = 0; i < field_count; i++)
+ free(row[i]);
+ free(row);
+ return row_count;
+ }
- (*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;
+ if (row_count > 0) {
+ memcpy(new_matrix, *matrix, sizeof(*new_matrix) * row_count);
+ memcpy(new_counts, *inner_counts, sizeof(*new_counts) * row_count);
}
- (*collection)[current_count][0] = strdup(href ? href : "");
- (*collection)[current_count][1] = strdup(label ? label : "");
- (*collection)[current_count][2] = strdup(class_name ? class_name : "");
+ new_matrix[row_count] = row;
+ new_counts[row_count] = field_count;
+ free(*matrix);
+ free(*inner_counts);
+ *matrix = new_matrix;
+ *inner_counts = new_counts;
+ return row_count + 1;
+}
- 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;
+void free_string_matrix(char ***matrix, int *inner_counts, int row_count) {
+ if (matrix) {
+ for (int i = 0; i < row_count; i++) {
+ int field_count = inner_counts ? inner_counts[i] : 0;
+ for (int j = 0; j < field_count; j++)
+ free(matrix[i][j]);
+ free(matrix[i]);
+ }
}
+ free(matrix);
+ free(inner_counts);
+}
- (*inner_counts)[current_count] = LINK_FIELD_COUNT;
-
- free(old_collection);
- free(old_inner_counts);
- return current_count + 1;
+int add_link_to_collection(const char *href, const char *label,
+ const char *class_name, char ****collection,
+ int **inner_counts, int current_count) {
+ const char *values[LINK_FIELD_COUNT] = {href, label, class_name};
+ return append_string_row(collection, inner_counts, current_count, values,
+ LINK_FIELD_COUNT);
}
int build_pagination(int page, char *(*href_builder)(int page, void *data),