cleaup: cleanup code.
This commit is contained in:
parent
7c155525fd
commit
618262d2a2
Binary file not shown.
|
@ -4,8 +4,6 @@
|
||||||
|
|
||||||
/* Inspired by https://libredirect.github.io/, but in C. */
|
/* Inspired by https://libredirect.github.io/, but in C. */
|
||||||
|
|
||||||
// int str_replace_start(const char* string, const char* target, const char* replacement, char* output){
|
|
||||||
|
|
||||||
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] = ' ';
|
||||||
|
@ -14,8 +12,6 @@ void str_init(char* str, int n){
|
||||||
} // 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){
|
||||||
/* inv.riverside.rocks */
|
|
||||||
// max length
|
|
||||||
int l1 = strlen(uri);
|
int l1 = strlen(uri);
|
||||||
int l2 = strlen(output);
|
int l2 = strlen(output);
|
||||||
|
|
||||||
|
@ -23,7 +19,7 @@ int libre_redirect(const char* uri, char* output){
|
||||||
return 1; // not enough memory.
|
return 1; // not enough memory.
|
||||||
}else{
|
}else{
|
||||||
char tmp_uri[l2++];
|
char tmp_uri[l2++];
|
||||||
strcpy(tmp_uri, uri); // includes terminating '\0'
|
strcpy(tmp_uri, uri); // strcpy also copies the terminating '\0'
|
||||||
|
|
||||||
char* sites[] = {
|
char* sites[] = {
|
||||||
"https://youtube.com",
|
"https://youtube.com",
|
||||||
|
|
|
@ -10,7 +10,7 @@ See also:
|
||||||
*/
|
*/
|
||||||
|
|
||||||
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){
|
||||||
/* Checks */
|
|
||||||
int l1 = strlen(string);
|
int l1 = strlen(string);
|
||||||
int l2 = strlen(target);
|
int l2 = strlen(target);
|
||||||
int l3 = strlen(replacement);
|
int l3 = strlen(replacement);
|
||||||
|
@ -19,16 +19,16 @@ int str_replace_start(const char* string, const char* target, const char* replac
|
||||||
|
|
||||||
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("Case 1.\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("Case 2.\n");
|
if(DEBUG) printf("Target larger than string.\n");
|
||||||
strcpy(output, string);
|
strcpy(output, string);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if(DEBUG) printf("Case 3.\n");
|
if(DEBUG) printf("Looking for a match.\n");
|
||||||
int match = true;
|
int match = true;
|
||||||
for(int i=0; i<l2; i++){
|
for(int i=0; i<l2; i++){
|
||||||
if(string[i] != target[i]){
|
if(string[i] != target[i]){
|
||||||
|
@ -37,7 +37,7 @@ int str_replace_start(const char* string, const char* target, const char* replac
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(match){
|
if(match){
|
||||||
if(DEBUG) printf("Case 3.a.\n");
|
if(DEBUG) printf("Found match.\n");
|
||||||
for(int i=0; i<l3; i++){
|
for(int i=0; i<l3; i++){
|
||||||
output[i] = replacement[i];
|
output[i] = replacement[i];
|
||||||
}
|
}
|
||||||
|
@ -49,7 +49,7 @@ int str_replace_start(const char* string, const char* target, const char* replac
|
||||||
output[counter] = '\0';
|
output[counter] = '\0';
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if(DEBUG) printf("Case 3.b.\n");
|
if(DEBUG) printf("Did not find match.\n");
|
||||||
strcpy(output, string);
|
strcpy(output, string);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user