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,29 +1,37 @@
#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++){ {
for(int i = 0; i < n; i++)
str[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 l1 = strlen(uri);
int l2 = strlen(output); int l2 = strlen(output);
int len;
if((l2 - l1) < LIBRE_N){
if(DEBUG) printf("Not enough memory\n");
return 1; // not enough memory.
}else{
char tmp_uri[l2++]; char tmp_uri[l2++];
char tmp_output[l2++]; char tmp_output[l2++];
if ((l2 - l1) < LIBRE_N) {
#ifdef DEBUG
printf("Not enough memory\n");
#endif
return 1; // not enough memory.
} else {
strcpy(tmp_uri, uri); // strcpy also copies the terminating '\0' strcpy(tmp_uri, uri); // strcpy also copies the terminating '\0'
strcpy(tmp_output, output); strcpy(tmp_output, output);
@ -36,6 +44,7 @@ int libre_redirect(const char* uri, char* output){
"https://www.bloomberg.com", "https://www.bloomberg.com",
"https://twitter.com" "https://twitter.com"
}; };
char* alternatives[] = { char* alternatives[] = {
"https://yt.artemislena.eu", "https://yt.artemislena.eu",
"https://teddit.nunosempere.com", "https://teddit.nunosempere.com",
@ -45,17 +54,22 @@ int libre_redirect(const char* uri, char* output){
"https://archive.is/https://www.bloomberg.com", "https://archive.is/https://www.bloomberg.com",
"https://nitter.net" "https://nitter.net"
}; };
int n = sizeof(annoying_sites)/sizeof(annoying_sites[0]);
for(int i=0; i<n ; i++){ len = sizeof(annoying_sites) / sizeof(annoying_sites[0]);
int replace_check = str_replace_start(tmp_uri, annoying_sites[i], alternatives[i], output);
if(replace_check == 2){ for (int i = 0; i < len; i++) {
if(DEBUG) printf("tmp_uri: %s\n", tmp_uri); int replace_check = str_replace_start(tmp_uri, annoying_sites[i],
if(DEBUG) printf("output: %s\n", output); alternatives[i], output);
// strcpy(output, tmp_uri); if (replace_check == 2) {
// break; #ifdef DEBUG
printf("tmp_uri: %s\n", tmp_uri);
printf("output: %s\n", output);
#endif
return 2; return 2;
}else if(replace_check == 1){ } else if (replace_check == 1) {
if(DEBUG) printf("replace_check failed\n"); #ifdef DEBUG
printf("replace_check failed\n");
#endif
return 1; return 1;
} }
strcpy(tmp_uri, output); strcpy(tmp_uri, output);
@ -63,7 +77,8 @@ int libre_redirect(const char* uri, char* output){
} }
strcpy(output, tmp_uri); strcpy(output, tmp_uri);
} }
if(DEBUG) printf("No match found\n\n"); #ifdef DEBUG
printf("No match found\n\n");
#endif
return 0; 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

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

@ -1,6 +1,7 @@
#include <string.h> #include <string.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#define DEBUG false #define DEBUG false
/* /*
@ -9,8 +10,8 @@ See also:
* 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);

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

@ -9,12 +9,10 @@ int main(){
char uri_filtered[l]; char uri_filtered[l];
str_init(uri_filtered, l); str_init(uri_filtered, l);
if(!libre_redirect(uri, uri_filtered)){ if (!libre_redirect(uri, uri_filtered)) {
printf("Filtered uri: %s\n", uri_filtered); printf("Filtered uri: %s\n", uri_filtered);
}else{ } else {
printf("Uri: %s\n", uri); printf("Uri: %s\n", uri);
// failure; do something with the original uri. // failure; do something with the original uri.
} }
} }