From 978c7ca1ccda4fc1138c607b2ffaa9fede60bf08 Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Sat, 23 Mar 2024 17:35:44 -0300 Subject: change formatting + refactor string code --- plugins/libre_redirect/libre_redirect.c | 29 +++++++------ plugins/libre_redirect/libre_redirect.h | 1 - plugins/libre_redirect/str_init.c | 6 --- plugins/libre_redirect/str_init.h | 1 - plugins/libre_redirect/str_replace_start.c | 65 ------------------------------ plugins/libre_redirect/str_replace_start.h | 4 -- 6 files changed, 14 insertions(+), 92 deletions(-) delete mode 100644 plugins/libre_redirect/str_init.c delete mode 100644 plugins/libre_redirect/str_init.h delete mode 100644 plugins/libre_redirect/str_replace_start.c delete mode 100644 plugins/libre_redirect/str_replace_start.h (limited to 'plugins/libre_redirect') diff --git a/plugins/libre_redirect/libre_redirect.c b/plugins/libre_redirect/libre_redirect.c index b8194e1..e6ec5fc 100644 --- a/plugins/libre_redirect/libre_redirect.c +++ b/plugins/libre_redirect/libre_redirect.c @@ -1,14 +1,13 @@ -#include #include #include -#include "str_init.h" -#include "str_replace_start.h" +#include "../strings/strings.h" #define LIBRE_N 50 /* Inspired by https://libredirect.github.io/, but in C. */ +// Use string manipulation over urls int libre_redirect(const char* uri, char* output) { int len_uri = strlen(uri); @@ -49,18 +48,18 @@ int libre_redirect(const char* uri, char* output) str_init(output, len_output); int replace_check = str_replace_start(uri, annoying_sites[i], alternatives[i], output); - switch(replace_check){ - case 0: // no match found - break; - case 1: // str_replace_start somehow failed - printf("str_replace_start failed\n"); - return 1; - break; - case 2: // match succeeded - return 2; - break; - default: - printf("Unreachable state"); + switch (replace_check) { + case 0: // no match found + break; + case 1: // str_replace_start somehow failed + printf("str_replace_start failed\n"); + return 1; + break; + case 2: // match succeeded + return 2; + break; + default: + printf("Unreachable state"); } } strcpy(output, uri); diff --git a/plugins/libre_redirect/libre_redirect.h b/plugins/libre_redirect/libre_redirect.h index 2c61397..7addeb0 100644 --- a/plugins/libre_redirect/libre_redirect.h +++ b/plugins/libre_redirect/libre_redirect.h @@ -3,4 +3,3 @@ #define LIBRE_N 50 int libre_redirect(const char* uri, char* uri_filtered); -void str_init(char* str, int n); diff --git a/plugins/libre_redirect/str_init.c b/plugins/libre_redirect/str_init.c deleted file mode 100644 index 3fc548c..0000000 --- a/plugins/libre_redirect/str_init.c +++ /dev/null @@ -1,6 +0,0 @@ -void str_init(char* str, int n) -{ - for (int i = 0; i < n; i++) - str[i] = ' '; - str[n] = '\0'; -} // could also use diff --git a/plugins/libre_redirect/str_init.h b/plugins/libre_redirect/str_init.h deleted file mode 100644 index b282b7f..0000000 --- a/plugins/libre_redirect/str_init.h +++ /dev/null @@ -1 +0,0 @@ -void str_init(char* str, int n); diff --git a/plugins/libre_redirect/str_replace_start.c b/plugins/libre_redirect/str_replace_start.c deleted file mode 100644 index d1fe083..0000000 --- a/plugins/libre_redirect/str_replace_start.c +++ /dev/null @@ -1,65 +0,0 @@ -#include -#include -#include - -#define DEBUG false - -/* -See also: -* -* https://github.com/irl/la-cucina/blob/master/str_replace.c -*/ - -int str_replace_start(const char* string, const char* target, const char* replacement, char* output) -{ - int l1 = strlen(string); - int l2 = strlen(target); - int l3 = strlen(replacement); - int l4 = strlen(output); - if (DEBUG) - printf("%d,%d,%d,%d\n", l1, l2, l3, l4); - // if(DEBUG) printf("%s,%s,%s,%s\n", string, target, replacement, output); - - if ((l4 < (l1 - l2 + l3)) || l4 < l1) { - // Not enough memory in output string. - if (DEBUG) - printf("String not long enough.\n"); - return 1; - } - /* else if(l1 < l2){ - // Not even possible that there is a match. - if(DEBUG) printf("Target larger than string.\n"); - strcpy(output, string); - } */ - else { - if (DEBUG) - printf("Looking for a match for %s in %s.\n", target, string); - int match = true; - for (int i = 0; i < l2; i++) { - if (string[i] != target[i]) { - match = false; - break; - } - } - if (match) { - if (DEBUG) - printf("Found match.\n"); - for (int i = 0; i < l3; i++) { - output[i] = replacement[i]; - } - int counter = l3; - for (int i = l2; i < l1; i++) { - output[counter] = string[i]; - counter++; - } - output[counter] = '\0'; - return 2; // success - } else { - if (DEBUG) - printf("Did not find match.\n"); - strcpy(output, string); - } - } - - return 0; -} diff --git a/plugins/libre_redirect/str_replace_start.h b/plugins/libre_redirect/str_replace_start.h deleted file mode 100644 index 78c79b5..0000000 --- a/plugins/libre_redirect/str_replace_start.h +++ /dev/null @@ -1,4 +0,0 @@ -#pragma once - -int str_replace_start(const char* string, const char* target, - const char* replacement, char* output); -- cgit v1.2.3