mirror of
https://github.com/tbamud/tbamud.git
synced 2026-02-11 02:24:21 +01:00
Bug/drink containers (#94)
* Bugfix for name_from_drinkcon * Newline at end of utils.c
This commit is contained in:
parent
c0fb6f8a71
commit
dceb563a9b
3 changed files with 73 additions and 37 deletions
65
src/utils.c
65
src/utils.c
|
|
@ -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--;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue