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

@ -779,50 +779,19 @@ void weight_change_object(struct obj_data *obj, int weight)
void name_from_drinkcon(struct obj_data *obj)
{
char *new_name, *cur_name, *next;
const char *liqname;
int liqlen, cpylen, maxlen;
char *new_name;
if (!obj || (GET_OBJ_TYPE(obj) != ITEM_DRINKCON && GET_OBJ_TYPE(obj) != ITEM_FOUNTAIN))
return;
liqname = drinknames[GET_OBJ_VAL(obj, 2)];
if (!isname(liqname, obj->name)) {
log("SYSERR: Can't remove liquid '%s' from '%s' (%d) item.", liqname, obj->name, obj->item_number);
/* SYSERR_DESC: From name_from_drinkcon(), this error comes about if the
* object noted (by keywords and item vnum) does not contain the liquid
* string being searched for. */
return;
}
liqlen = strlen(liqname);
maxlen = strlen(obj->name) - strlen(liqname); /* +1 for NUL, -1 for space */
CREATE(new_name, char, maxlen);
for (cur_name = obj->name; cur_name; cur_name = next) {
if (*cur_name == ' ')
cur_name++;
if ((next = strchr(cur_name, ' ')))
cpylen = next - cur_name;
else
cpylen = strlen(cur_name);
if (!strn_cmp(cur_name, liqname, liqlen))
continue;
if (*new_name) {
strcat(new_name, " "); /* strcat: OK (size precalculated) */
maxlen--;
}
strncat(new_name, cur_name, maxlen); /* strncat: OK (size precalculated) */
maxlen -= cpylen;
}
if (GET_OBJ_RNUM(obj) == NOTHING || obj->name != obj_proto[GET_OBJ_RNUM(obj)].name)
free(obj->name);
remove_from_string(obj->name, liqname);
new_name = right_trim_whitespace(obj->name);
free(obj->name);
obj->name = new_name;
}
void name_to_drinkcon(struct obj_data *obj, int type)

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--;
}
}
}

View file

@ -70,6 +70,8 @@ void new_affect(struct affected_type *af);
int get_class_by_name(char *classname);
char * convert_from_tabs(char * string);
int count_non_protocol_chars(char * str);
char *right_trim_whitespace(const char *string);
void remove_from_string(char *string, const char *to_remove);
/* Public functions made available form weather.c */
void weather_and_time(int mode);