simplify libre_redirect code
This commit is contained in:
parent
8f4b4b9abf
commit
43cac7f3f1
|
@ -7,28 +7,18 @@
|
||||||
|
|
||||||
#define LIBRE_N 50
|
#define LIBRE_N 50
|
||||||
|
|
||||||
/* Uncomment for debug */
|
|
||||||
/* #define DEBUG */
|
|
||||||
|
|
||||||
/* Inspired by https://libredirect.github.io/, but in C. */
|
/* Inspired by https://libredirect.github.io/, but in C. */
|
||||||
|
|
||||||
int libre_redirect(const char* uri, char* output)
|
int libre_redirect(const char* uri, char* output)
|
||||||
{
|
{
|
||||||
int l1 = strlen(uri);
|
int len_uri = strlen(uri);
|
||||||
int l2 = strlen(output);
|
int len_output = strlen(output);
|
||||||
int len;
|
char tmp_uri[len_output++];
|
||||||
char tmp_uri[l2++];
|
|
||||||
char tmp_output[l2++];
|
|
||||||
|
|
||||||
if ((l2 - l1) < LIBRE_N) {
|
if ((len_output - len_uri) < LIBRE_N) {
|
||||||
#ifdef DEBUG
|
|
||||||
printf("Not enough memory\n");
|
printf("Not enough memory\n");
|
||||||
#endif
|
|
||||||
return 1; // not enough memory.
|
return 1; // not enough memory.
|
||||||
} else {
|
} else {
|
||||||
strcpy(tmp_uri, uri); // strcpy also copies the terminating '\0'
|
|
||||||
strcpy(tmp_output, output);
|
|
||||||
|
|
||||||
char* annoying_sites[] = {
|
char* annoying_sites[] = {
|
||||||
"https://www.reddit.com",
|
"https://www.reddit.com",
|
||||||
"https://www.youtube.com",
|
"https://www.youtube.com",
|
||||||
|
@ -55,30 +45,27 @@ int libre_redirect(const char* uri, char* output)
|
||||||
// "https://nitter.net"
|
// "https://nitter.net"
|
||||||
};
|
};
|
||||||
|
|
||||||
len = sizeof(annoying_sites) / sizeof(annoying_sites[0]);
|
int len = sizeof(annoying_sites) / sizeof(annoying_sites[0]);
|
||||||
|
|
||||||
for (int i = 0; i < len; i++) {
|
for (int i = 0; i < len; i++) {
|
||||||
|
strcpy(tmp_uri, uri);
|
||||||
|
str_init(output, len_output);
|
||||||
int replace_check = str_replace_start(tmp_uri, annoying_sites[i],
|
int replace_check = str_replace_start(tmp_uri, annoying_sites[i],
|
||||||
alternatives[i], output);
|
alternatives[i], output);
|
||||||
if (replace_check == 2) {
|
switch(replace_check){
|
||||||
#ifdef DEBUG
|
case 0: // no match found
|
||||||
printf("tmp_uri: %s\n", tmp_uri);
|
break;
|
||||||
printf("output: %s\n", output);
|
case 1: // str_replace_start somehow failed
|
||||||
#endif
|
printf("str_replace_start failed\n");
|
||||||
return 2;
|
return 1;
|
||||||
} else if (replace_check == 1) {
|
break;
|
||||||
#ifdef DEBUG
|
case 2: // match succeeded
|
||||||
printf("replace_check failed\n");
|
return 2;
|
||||||
#endif
|
break;
|
||||||
return 1;
|
default:
|
||||||
|
printf("Unreachable state");
|
||||||
}
|
}
|
||||||
strcpy(tmp_uri, output);
|
|
||||||
str_init(output, l2);
|
|
||||||
}
|
}
|
||||||
strcpy(output, tmp_uri);
|
strcpy(output, uri);
|
||||||
}
|
}
|
||||||
#ifdef DEBUG
|
|
||||||
printf("No match found\n\n");
|
|
||||||
#endif
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user