change formatting + refactor string code
This commit is contained in:
parent
c8ff246cc2
commit
978c7ca1cc
2
makefile
2
makefile
|
@ -19,7 +19,7 @@ include plugins/plugins.mk
|
||||||
# PLUGINS=./plugins/stand_in/stand_in.c
|
# PLUGINS=./plugins/stand_in/stand_in.c
|
||||||
|
|
||||||
## Formatter
|
## Formatter
|
||||||
STYLE_BLUEPRINT=webkit
|
STYLE_BLUEPRINT="{BasedOnStyle: webkit, AllowShortIfStatementsOnASingleLine: true}"
|
||||||
FORMATTER=clang-format -i -style=$(STYLE_BLUEPRINT)
|
FORMATTER=clang-format -i -style=$(STYLE_BLUEPRINT)
|
||||||
|
|
||||||
# Runtime files
|
# Runtime files
|
||||||
|
|
|
@ -1,14 +1,13 @@
|
||||||
#include <stdbool.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "str_init.h"
|
#include "../strings/strings.h"
|
||||||
#include "str_replace_start.h"
|
|
||||||
|
|
||||||
#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);
|
||||||
|
@ -49,7 +48,7 @@ int libre_redirect(const char* uri, char* output)
|
||||||
str_init(output, len_output);
|
str_init(output, len_output);
|
||||||
int replace_check = str_replace_start(uri, annoying_sites[i],
|
int replace_check = str_replace_start(uri, annoying_sites[i],
|
||||||
alternatives[i], output);
|
alternatives[i], output);
|
||||||
switch(replace_check){
|
switch (replace_check) {
|
||||||
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
|
||||||
|
|
|
@ -3,4 +3,3 @@
|
||||||
#define LIBRE_N 50
|
#define LIBRE_N 50
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
|
@ -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 <https://manpages.ubuntu.com/manpages/impish/man3/strinit.3pub.html>
|
|
|
@ -1 +0,0 @@
|
||||||
void str_init(char* str, int n);
|
|
|
@ -1,65 +0,0 @@
|
||||||
#include <stdbool.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#define DEBUG false
|
|
||||||
|
|
||||||
/*
|
|
||||||
See also:
|
|
||||||
* <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
|
|
||||||
*/
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
|
@ -1,4 +0,0 @@
|
||||||
#pragma once
|
|
||||||
|
|
||||||
int str_replace_start(const char* string, const char* target,
|
|
||||||
const char* replacement, char* output);
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
#include "strings/strings.h"
|
||||||
#include "libre_redirect/libre_redirect.h"
|
#include "libre_redirect/libre_redirect.h"
|
||||||
#include "readability/readability.h"
|
#include "readability/readability.h"
|
||||||
#include "shortcuts/shortcuts.h"
|
#include "shortcuts/shortcuts.h"
|
||||||
|
|
|
@ -1,14 +1,17 @@
|
||||||
|
## Shared
|
||||||
|
COMMON_CODE=./plugins/strings/strings.c
|
||||||
|
|
||||||
## Plugins
|
## Plugins
|
||||||
CUSTOM_STYLES=./plugins/style/style.c
|
CUSTOM_STYLES=./plugins/style/style.c
|
||||||
SHORTCUTS=./plugins/shortcuts/shortcuts.c
|
SHORTCUTS=./plugins/shortcuts/shortcuts.c
|
||||||
READABILITY=./plugins/readability/readability.c
|
READABILITY=./plugins/readability/readability.c
|
||||||
LIBRE_REDIRECT=./plugins/libre_redirect/libre_redirect.c ./plugins/libre_redirect/str_replace_start.c ./plugins/libre_redirect/str_init.c
|
LIBRE_REDIRECT=./plugins/libre_redirect/libre_redirect.c
|
||||||
|
|
||||||
ADBLOCK='-L/usr/lib/wyebrowser/adblock.so' # optional adblocking; depends on https://github.com/jun7/wyebadblock
|
ADBLOCK='-L/usr/lib/wyebrowser/adblock.so' # optional adblocking; depends on https://github.com/jun7/wyebadblock
|
||||||
|
|
||||||
STAND_IN=./plugins/stand_in/stand_in.c # gives function definitions for the above, which do nothing
|
STAND_IN=./plugins/stand_in/stand_in.c # gives function definitions for the above, which do nothing
|
||||||
|
|
||||||
PLUGINS=$(CUSTOM_STYLES) $(SHORTCUTS) $(READABILITY) $(LIBRE_REDIRECT)
|
PLUGINS=$(COMMON_CODE) $(CUSTOM_STYLES) $(SHORTCUTS) $(READABILITY) $(LIBRE_REDIRECT)
|
||||||
# PLUGINS=$(STAND_IN)
|
# PLUGINS=$(STAND_IN)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -2,8 +2,7 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
#include "../libre_redirect/str_init.h"
|
#include "../strings/strings.h"
|
||||||
#include "../libre_redirect/str_replace_start.h"
|
|
||||||
|
|
||||||
#define SHORTCUT_N 41
|
#define SHORTCUT_N 41
|
||||||
|
|
||||||
|
|
61
plugins/strings/strings.c
Normal file
61
plugins/strings/strings.c
Normal file
|
@ -0,0 +1,61 @@
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#define DEBUG false
|
||||||
|
|
||||||
|
// String manipulation
|
||||||
|
void str_init(char* str, int n)
|
||||||
|
{
|
||||||
|
// could also use <https://manpages.ubuntu.com/manpages/impish/man3/strinit.3pub.html>
|
||||||
|
for (int i = 0; i < n; i++)
|
||||||
|
str[i] = ' ';
|
||||||
|
str[n] = '\0';
|
||||||
|
}
|
||||||
|
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("string: %s, target: %s, replacement: %s, output: %s\n", string, target, replacement, output);
|
||||||
|
printf("%d,%d,%d,%d\n", l1, l2, l3, l4);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((l4 < (l1 - l2 + l3)) || l4 < l1) {
|
||||||
|
printf("Not enough memory in output string.\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
See also:
|
||||||
|
* <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
|
||||||
|
*/
|
2
plugins/strings/strings.h
Normal file
2
plugins/strings/strings.h
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
void str_init(char* str, int n);
|
||||||
|
int str_replace_start(const char* string, const char* target, const char* replacement, char* output);
|
Loading…
Reference in New Issue
Block a user