Bug/drink containers (#94)

* Bugfix for name_from_drinkcon

* Newline at end of utils.c
This commit is contained in:
MBourne 2020-06-07 13:52:22 +01:00 committed by GitHub
parent c0fb6f8a71
commit dceb563a9b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 73 additions and 37 deletions

View file

@ -1489,3 +1489,68 @@ char * convert_from_tabs(char * string)
parse_tab(buf);
return(buf);
}
/* This removes all trailing whitespace from the end of a string */
char *right_trim_whitespace(const char *string)
{
char *r = strdup(string);
if (r != NULL)
{
char *fr = r + strlen(string) - 1;
while( (isspace(*fr) || !isprint(*fr) || *fr == 0) && fr >= r) --fr;
*++fr = 0;
}
return r;
}
/**
* Remove all occurrences of a given word in string.
*/
void remove_from_string(char *string, const char *to_remove)
{
int i, j, string_len, to_remove_len;
int found;
string_len = strlen(string); // Length of string
to_remove_len = strlen(to_remove); // Length of word to remove
for(i=0; i <= string_len - to_remove_len; i++)
{
/* Match word with string */
found = 1;
for(j=0; j<to_remove_len; j++)
{
if(string[i + j] != to_remove[j])
{
found = 0;
break;
}
}
/* If it is not a word */
if(string[i + j] != ' ' && string[i + j] != '\t' && string[i + j] != '\n' && string[i + j] != '\0')
{
found = 0;
}
/*
* If word is found then shift all characters to left
* and decrement the string length
*/
if(found == 1)
{
for(j=i; j<=string_len - to_remove_len; j++)
{
string[j] = string[j + to_remove_len];
}
string_len = string_len - to_remove_len;
// We will match next occurrence of word from current index.
i--;
}
}
}