tweak usage of str replacement code

min
NunoSempere 2 months ago
parent 5097a1982b
commit e322621698

@ -6,15 +6,13 @@
#define LIBRE_N 50 #define LIBRE_N 50
/* Inspired by https://libredirect.github.io/, but in C. */ /* Inspired by https://libredirect.github.io/, but in C. */
// Use string manipulation over urls
int libre_redirect(const char* uri, char* output) int libre_redirect(const char* uri, char* output)
{ {
int len_uri = strlen(uri); int len_uri = strlen(uri);
int len_output = strlen(output); int len_output = strlen(output);
if ((len_output - len_uri) < LIBRE_N) { if ((len_output - len_uri) < LIBRE_N) {
printf("Not enough memory\n"); fprintf(stderr, "Not enough memory\n");
return 1; // not enough memory. return 1; // not enough memory.
} else { } else {
char* annoying_sites[] = { char* annoying_sites[] = {
@ -52,14 +50,14 @@ int libre_redirect(const char* uri, char* output)
case 0: // no match found case 0: // no match found
break; break;
case 1: // str_replace_start somehow failed case 1: // str_replace_start somehow failed
printf("str_replace_start failed\n"); fprintf(stderr, "str_replace_start failed\n");
return 1; return 1;
break; break;
case 2: // match succeeded case 2: // match succeeded
return 2; return 2;
break; break;
default: default:
printf("Unreachable state"); fprintf(stderr, "Unreachable state\n");
} }
} }
strcpy(output, uri); strcpy(output, uri);

@ -5,30 +5,18 @@
#include "../strings/strings.h" #include "../strings/strings.h"
#define SHORTCUT_N 41 #define SHORTCUT_N 41
#define DEBUG false
/* Uncomment for debug */
/* #define DEBUG */
/* Inspired by https://duckduckgo.com/bangs */ /* Inspired by https://duckduckgo.com/bangs */
int shortcut_expand(const char* uri, char* output) int shortcut_expand(const char* uri, char* output)
{ {
printf("SHORTCUT EXPAND!\n"); int len_uri = strlen(uri);
int l1 = strlen(uri); int len_output = strlen(output);
int l2 = strlen(output);
int len;
char tmp_uri[l2++];
char tmp_output[l2++];
if ((l2 - l1) < SHORTCUT_N) { if ((len_output - len_uri) < SHORTCUT_N) {
#ifdef DEBUG fprintf(stderr, "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* shortcuts[] = { char* shortcuts[] = {
"!aa", "!aa",
"!blog", "!blog",
@ -50,30 +38,28 @@ int shortcut_expand(const char* uri, char* output)
}; };
// len = sizeof(shortcuts) / sizeof(shortcuts[0]); // len = sizeof(shortcuts) / sizeof(shortcuts[0]);
len = sizeof(shortcuts) / sizeof(char*); int len = sizeof(shortcuts) / sizeof(char*);
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
int replace_check = str_replace_start(tmp_uri, shortcuts[i], str_init(output, len_output);
int replace_check = str_replace_start(uri, shortcuts[i],
expansions[i], output); expansions[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 fprintf(stderr, "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:
fprintf(stderr, "Unreachable state\n");
} }
strcpy(tmp_uri, output);
str_init(output, l2);
} }
strcpy(output, tmp_uri); strcpy(output, uri);
} }
#ifdef DEBUG if(DEBUG) printf("No match found\n\n");
printf("No match found\n\n");
#endif
return 0; return 0;
} }

Loading…
Cancel
Save