Fixed formatting in some libre_redirect files

This commit is contained in:
fenze 2022-12-18 16:54:07 +00:00
parent d97045bf47
commit 584a5d68a8
6 changed files with 96 additions and 87 deletions

View File

@ -1,69 +1,84 @@
#include "str_replace_start.h"
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include <stdbool.h> #include <stdbool.h>
#include "str_replace_start.h"
#define LIBRE_N 19 #define LIBRE_N 19
#define DEBUG false
/* Uncomment for debug */
/* #define DEBUG */
/* Inspired by https://libredirect.github.io/, but in C. */ /* Inspired by https://libredirect.github.io/, but in C. */
void str_init(char* str, int n){ void str_init(char* str, int n)
for(int i=0; i<n; i++){ {
str[i] = ' '; for(int i = 0; i < n; i++)
} str[i] = ' ';
str[n] = '\0'; str[n] = '\0';
} // could also use <https://manpages.ubuntu.com/manpages/impish/man3/strinit.3pub.html> } // could also use <https://manpages.ubuntu.com/manpages/impish/man3/strinit.3pub.html>
int libre_redirect(const char* uri, char* output){ int libre_redirect(const char* uri, char* output)
int l1 = strlen(uri); {
int l2 = strlen(output); int l1 = strlen(uri);
int l2 = strlen(output);
if((l2 - l1) < LIBRE_N){ int len;
if(DEBUG) printf("Not enough memory\n"); char tmp_uri[l2++];
return 1; // not enough memory. char tmp_output[l2++];
}else{
char tmp_uri[l2++]; if ((l2 - l1) < LIBRE_N) {
char tmp_output[l2++]; #ifdef DEBUG
strcpy(tmp_uri, uri); // strcpy also copies the terminating '\0' printf("Not enough memory\n");
strcpy(tmp_output, output); #endif
return 1; // not enough memory.
char* annoying_sites[] = { } else {
"https://www.youtube.com", strcpy(tmp_uri, uri); // strcpy also copies the terminating '\0'
"https://www.reddit.com", strcpy(tmp_output, output);
"https://medium.com",
"https://translate.google.com", char* annoying_sites[] = {
"https://forum.effectivealtruism.org", "https://www.youtube.com",
"https://www.bloomberg.com", "https://www.reddit.com",
"https://twitter.com" "https://medium.com",
}; "https://translate.google.com",
char* alternatives[] = { "https://forum.effectivealtruism.org",
"https://yt.artemislena.eu", "https://www.bloomberg.com",
"https://teddit.nunosempere.com", "https://twitter.com"
"https://scribe.rip", };
"https://simplytranslate.org/",
"https://ea.greaterwrong.com", char* alternatives[] = {
"https://archive.is/https://www.bloomberg.com", "https://yt.artemislena.eu",
"https://nitter.net" "https://teddit.nunosempere.com",
}; "https://scribe.rip",
int n = sizeof(annoying_sites)/sizeof(annoying_sites[0]); "https://simplytranslate.org/",
for(int i=0; i<n ; i++){ "https://ea.greaterwrong.com",
int replace_check = str_replace_start(tmp_uri, annoying_sites[i], alternatives[i], output); "https://archive.is/https://www.bloomberg.com",
if(replace_check == 2){ "https://nitter.net"
if(DEBUG) printf("tmp_uri: %s\n", tmp_uri); };
if(DEBUG) printf("output: %s\n", output);
// strcpy(output, tmp_uri); len = sizeof(annoying_sites) / sizeof(annoying_sites[0]);
// break;
return 2; for (int i = 0; i < len; i++) {
}else if(replace_check == 1){ int replace_check = str_replace_start(tmp_uri, annoying_sites[i],
if(DEBUG) printf("replace_check failed\n"); alternatives[i], output);
return 1; if (replace_check == 2) {
} #ifdef DEBUG
strcpy(tmp_uri, output); printf("tmp_uri: %s\n", tmp_uri);
str_init(output, l2); printf("output: %s\n", output);
} #endif
strcpy(output, tmp_uri); return 2;
} } else if (replace_check == 1) {
if(DEBUG) printf("No match found\n\n"); #ifdef DEBUG
return 0; printf("replace_check failed\n");
#endif
return 1;
}
strcpy(tmp_uri, output);
str_init(output, l2);
}
strcpy(output, tmp_uri);
}
#ifdef DEBUG
printf("No match found\n\n");
#endif
return 0;
} }

5
plugins/libre_redirect/libre_redirect.h Executable file → Normal file
View File

@ -1,9 +1,6 @@
#ifndef LIBRE_REDIRECT #pragma once
#define LIBRE_REDIRECT
#define LIBRE_N 19 #define LIBRE_N 19
int libre_redirect(const char* uri, char* uri_filtered); int libre_redirect(const char* uri, char* uri_filtered);
void str_init(char* str, int n); void str_init(char* str, int n);
#endif

13
plugins/libre_redirect/str_replace_start.c Executable file → Normal file
View File

@ -1,28 +1,29 @@
#include <string.h> #include <string.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#define DEBUG false #define DEBUG false
/* /*
See also: See also:
* <https://web.archive.org/web/20160201212501/coding.debuntu.org/c-implementing-str_replace-replace-all-occurrences-substring> * <https://web.archive.org/web/20160201212501/coding.debuntu.org/c-implementing-str_replace-replace-all-occurrences-substring>
* https://github.com/irl/la-cucina/blob/master/str_replace.c * 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 str_replace_start(const char* string, const char* target, const char* replacement, char* output)
{
int l1 = strlen(string); int l1 = strlen(string);
int l2 = strlen(target); int l2 = strlen(target);
int l3 = strlen(replacement); int l3 = strlen(replacement);
int l4 = strlen(output); int l4 = strlen(output);
if(DEBUG) printf("%d,%d,%d,%d\n", l1, l2, l3, l4); 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(DEBUG) printf("%s,%s,%s,%s\n", string, target, replacement, output);
if((l4 < (l1 - l2 + l3)) || l4 < l1 ){ if((l4 < (l1 - l2 + l3)) || l4 < l1 ){
// Not enough memory in output string. // Not enough memory in output string.
if(DEBUG) printf("String not long enough.\n"); if(DEBUG) printf("String not long enough.\n");
return 1; return 1;
} }
/* else if(l1 < l2){ /* else if(l1 < l2){
// Not even possible that there is a match. // Not even possible that there is a match.
if(DEBUG) printf("Target larger than string.\n"); if(DEBUG) printf("Target larger than string.\n");

8
plugins/libre_redirect/str_replace_start.h Executable file → Normal file
View File

@ -1,6 +1,4 @@
#ifndef STR_REPLACE_H_ #pragma once
#define STR_REPLACE_H_
int str_replace_start(const char* string, const char* target, const char* replacement, char* output); int str_replace_start(const char* string, const char* target,
const char* replacement, char* output);
#endif

View File

@ -3,18 +3,16 @@
#include <stdio.h> #include <stdio.h>
int main(){ int main(){
char uri[] = "https://reddit.com/r/blah"; char uri[] = "https://reddit.com/r/blah";
int l = LIBRE_N + strlen(uri) + 1;
char uri_filtered[l];
str_init(uri_filtered, l);
if(!libre_redirect(uri, uri_filtered)){ int l = LIBRE_N + strlen(uri) + 1;
printf("Filtered uri: %s\n", uri_filtered); char uri_filtered[l];
}else{ str_init(uri_filtered, l);
printf("Uri: %s\n", uri);
// failure; do something with the original uri. if (!libre_redirect(uri, uri_filtered)) {
} printf("Filtered uri: %s\n", uri_filtered);
} else {
printf("Uri: %s\n", uri);
// failure; do something with the original uri.
}
} }