Welcome to the Builder Academy

Question Bug in Drink containers

More
01 Jun 2020 15:05 #8739 by cunning
Replied by cunning on topic Bug in Drink containers
any word on this yet? I reverted back to the old code version and had no issue with name-xx-drinkcon. I have not had time to dig into this myself, i am blasted with work and some medical stuff.

Please Log in or Create an account to join the conversation.

More
01 Jun 2020 22:09 #8740 by thomas
Replied by thomas on topic Bug in Drink containers
I'm glad you've found a workaround. I'm a bit more busy than expacted right now, what with corona, home office and other, more private matters. I'll look into it when Io get the chance :)

Please Log in or Create an account to join the conversation.

More
01 Jun 2020 22:13 - 01 Jun 2020 22:54 #8741 by JTP
Replied by JTP on topic Bug in Drink containers
Well I just know for a fact that we have several times had a drink container that suddently weighed so Much that when someone died they couldn’t Pick it up again. But not every time. So a bit Strange. But it was some times almost like it Got heavier and heavier over time
Last edit: 01 Jun 2020 22:54 by JTP.

Please Log in or Create an account to join the conversation.

More
02 Jun 2020 16:45 #8743 by cunning
Replied by cunning on topic Bug in Drink containers
JTP,
These are not the same issues, I suggest you open another topic to discuss them. I do not want to confuse the issue here.

Please Log in or Create an account to join the conversation.

More
05 Jun 2020 16:36 #8750 by WhiskyTest
Replied by WhiskyTest on topic Bug in Drink containers
Yeah I've reproduced this in the current code.
I think the bug is in 'name_from_drinkcon()'

Manipulating strings is definitely not my strong suite..
I googled up some C code which removes duplicate words from strings, and also removes trailing whitespace, this is what I ended up with:
Code:
/** * Remove all occurrences of a given word in string. */ void removeAll(char *str, const char *toRemove) { int i, j, stringLen, toRemoveLen; int found; stringLen = strlen(str); // Length of string toRemoveLen = strlen(toRemove); // Length of word to remove for(i=0; i <= stringLen - toRemoveLen; i++) { /* Match word with string */ found = 1; for(j=0; j<toRemoveLen; j++) { if(str[i + j] != toRemove[j]) { found = 0; break; } } /* If it is not a word */ if(str[i + j] != ' ' && str[i + j] != '\t' && str[i + j] != '\n' && str[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<=stringLen - toRemoveLen; j++) { str[j] = str[j + toRemoveLen]; } stringLen = stringLen - toRemoveLen; // We will match next occurrence of word from current index. i--; } } } char *ltrim(const char *s) { char *r = strdup(s); if (r != NULL) { char *fr = r + strlen(s) - 1; while( (isspace(*fr) || !isprint(*fr) || *fr == 0) && fr >= r) --fr; *++fr = 0; } return r; } void name_from_drinkcon(struct obj_data *obj) { const char *liqname; if (!obj || (GET_OBJ_TYPE(obj) != ITEM_DRINKCON && GET_OBJ_TYPE(obj) != ITEM_FOUNTAIN)) return; liqname = drinknames[GET_OBJ_VAL(obj, 2)]; removeAll(obj->name, liqname); obj->name = ltrim(obj->name); }
I'm not sure this is the correct way to handle changing strings, I suspect I need to free the old one and create a new one? If anyone can confirm that would be great :D

Please Log in or Create an account to join the conversation.

More
05 Jun 2020 20:27 #8754 by thomas
Replied by thomas on topic Bug in Drink containers
You're not freeing the old obj->name before putting a new one in. If you change name_from_drinkcon to this, it should probably be ok:
Code:
void name_from_drinkcon(struct obj_data *obj) { const char *liqname; 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)]; removeAll(obj->name, liqname); new_name = ltrim(obj->name); free(obj->name); obj->name = new_name; }
The following user(s) said Thank You: WhiskyTest

Please Log in or Create an account to join the conversation.

Time to create page: 0.203 seconds