#include #include #include #include #include "defs.h" #include "lf.h" #include "objects.h" #include "text.h" extern long curtime; char *capitalise(char *text) { if (strlen(text) > 0) { text[0] = toupper(text[0]); } return text; } char *getattrname(enum ATTRIB att) { switch (att) { case A_STR: return "strength"; case A_IQ: return "intelligence"; case A_DEX: return "dexterity"; default: break; } return "?unknown?"; } char *getpossessive(char *text) { char lastchar; // you -> your if (!strcmp(text, "you")) { return "r"; } // xxxs -> xxxs' lastchar = text[strlen(text)-1]; if (tolower(lastchar) == 's') { return "'"; } // default: 's return "'s"; } char *getsizetext(enum LFSIZE sz) { switch (sz) { case SZ_ENORMOUS: return "enormous"; case SZ_HUGE: return "huge"; case SZ_LARGE: return "large"; case SZ_HUMAN: return "human-sized"; case SZ_MEDIUM: return "medium"; case SZ_SMALL: return "small"; case SZ_MINI: case SZ_TINY: return "extremely small"; default: return "unknown-sized"; } return "unknown-sized"; } char *gettimetext(char *retbuf) { int hours,mins,secs; splittime(&hours, &mins, &secs); sprintf(retbuf, "%02d:%02d:%02d",hours,mins,secs); return retbuf; } char *gettimetextfuzzy(char *retbuf, int wantpm) { int hours,mins,secs; int pm = B_FALSE; splittime(&hours, &mins, &secs); if (hours > 12) { hours -= 12; pm = B_TRUE; } if (mins == 0) { sprintf(retbuf, "exactly %d o'clock", hours); } else if (mins <= 15) { sprintf(retbuf, "a little after %d o'clock", hours); } else if (mins <= 25) { sprintf(retbuf, "nearly half past %d", hours); } else if (mins <= 35) { sprintf(retbuf, "around half past %d", hours); } else if (mins <= 45) { sprintf(retbuf, "comung up to %d o'clock", (hours == 12) ? 1 : (hours+1)); } else { sprintf(retbuf, "nearly %d o'clock", (hours == 12) ? 1 : (hours+1)); } if (wantpm) { strcat(retbuf, " in the "); if (pm) { strcat(retbuf, "afternoon"); } else { strcat(retbuf, "morning"); } } return retbuf; } char *getweighttext(float weight, char *buf) { if (weight >= 1) { if ((int)weight == weight) { // ie. is weight an integer? sprintf(buf, "%0.0f kg",weight); } else { sprintf(buf, "%0.1f kg",weight); } } else { sprintf(buf, "%0.0f grams", weight * 1000); } return buf; } int isvowel (char c) { switch (c) { case 'a': case 'e': case 'i': case 'o': case 'u': return B_TRUE; } return B_FALSE; } // allocates and returns new string char *makeplural(char *text) { char lastlet; char *newtext; int rv; newtext = strdup(text); // scrolls newtext = strrep(newtext, "berry ", "berries ", &rv); if (rv) return newtext; newtext = strrep(newtext, "block ", "blocks ", &rv); if (rv) return newtext; newtext = strrep(newtext, "can ", "cans ", &rv); if (rv) return newtext; newtext = strrep(newtext, "chunk ", "chunks ", &rv); if (rv) return newtext; newtext = strrep(newtext, "gem ", "gems ", &rv); if (rv) return newtext; newtext = strrep(newtext, "loaf ", "loaves ", &rv); if (rv) return newtext; newtext = strrep(newtext, "lump ", "lumps ", &rv); if (rv) return newtext; newtext = strrep(newtext, "piece ", "pieces ", &rv); if (rv) return newtext; newtext = strrep(newtext, "pile ", "piles ", &rv); if (rv) return newtext; newtext = strrep(newtext, "pool ", "pools ", &rv); if (rv) return newtext; newtext = strrep(newtext, "potion ", "potions ", &rv); if (rv) return newtext; newtext = strrep(newtext, "puddle ", "puddles ", &rv); if (rv) return newtext; newtext = strrep(newtext, "ring ", "rings ", &rv); if (rv) return newtext; newtext = strrep(newtext, "scroll ", "scrolls ", &rv); if (rv) return newtext; newtext = strrep(newtext, "vial ", "vials ", &rv); if (rv) return newtext; // newtext = strrep(newtext, "pair ", "pairs ", &rv); // don't return // default lastlet = text[strlen(text)-1]; switch (lastlet) { case 's': case 'o': // append "es" asprintf(&newtext, "%ses",text); break; default: // append "s" asprintf(&newtext, "%ss",text); break; } return newtext; } char *noprefix(char *obname) { char *p; p = strchr(obname, ' '); if (p) { p++; return p; } else { return obname; } } void splittime(int *hours, int *mins, int *secs) { long left; left = curtime; *hours = left / 3600; left -= (*hours * 3600); *mins = left / 60; left -= (*mins * 60); *secs = left; } char *strrep(char *text, char *oldtok, char *newtok, int *rv) { char *temp; temp = strdup(" "); // ooooooo is this bad?? dostrrep(text, &temp, oldtok, newtok, rv); // swap text = realloc(text, strlen(temp)+1); // extra space for NUL strcpy(text, temp); free(temp); return text; } // returns TRUE if any replacements made char *dostrrep(char* in, char** out, char* oldtok, char* newtok, int *rv) { char *temp; char *found = strstr(in, oldtok); int idx; if(!found) { *out = realloc(*out, strlen(in) + 1); // oooooooo crashing in realloc strcpy(*out, in); if (rv) *rv = B_FALSE; return *out; } idx = found - in; *out = realloc(*out, strlen(in) - strlen(oldtok) + strlen(newtok) + 1); strncpy(*out, in, idx); strcpy(*out + idx, newtok); strcpy(*out + idx + strlen(newtok), in + idx + strlen(oldtok)); temp = malloc(idx+strlen(newtok)+1); strncpy(temp,*out,idx+strlen(newtok)); temp[idx + strlen(newtok)] = '\0'; dostrrep(found + strlen(oldtok), out, oldtok, newtok, rv); temp = realloc(temp, strlen(temp) + strlen(*out) + 1); strcat(temp,*out); free(*out); *out = temp; if (rv) *rv = B_TRUE; return *out; } int strpixmatch(char *haystack, char *needle) { int matched = B_FALSE; char *hword, *nword, *hcont,*ncont; if (strchr(needle, ' ') || strchr(haystack, ' ')) { char lochaystack[BUFLEN], locneedle[BUFLEN]; strcpy(lochaystack, haystack); strcpy(locneedle, needle); // match word for word nword = strtok_r(locneedle, " ", &ncont); hword = strtok_r(lochaystack, " ", &hcont); while (nword && hword) { // all typed words must match if (strcasestr(hword, nword)) { matched = B_TRUE; } else { matched = B_FALSE; break; } nword = strtok_r(NULL, " ", &ncont); hword = strtok_r(NULL, " ", &hcont); if (nword && !hword) { matched = B_FALSE; } } /* if (!matched && !strchr(needle, ' ')) { // now try matching typed word against second word in spellname strcpy(lochaystack, haystack); hword = strtok_r(lochaystack, " ", &hcont); while (hword) { if (strcasestr(hword, needle)) { matched = B_TRUE; break; } else { matched = B_FALSE; } hword = strtok_r(NULL, " ", &hcont); if (!hword) { matched = B_FALSE; } } } */ } else { if (strcasestr(haystack, needle)) { matched = B_TRUE; } } return matched; } char *you(lifeform_t *lf) { if (isplayer(lf)) { return "You"; } return "It"; } char *you_l(lifeform_t *lf) { if (isplayer(lf)) { return "you"; } return "it"; } char *your(lifeform_t *lf) { if (isplayer(lf)) { return "Your"; } return "Its"; } char *your_l(lifeform_t *lf) { if (isplayer(lf)) { return "your"; } return "its"; }