nexus/text.c

364 lines
7.4 KiB
C
Raw Normal View History

2011-02-01 06:16:13 +11:00
#include <ctype.h>
2010-12-02 12:17:54 +11:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "defs.h"
2011-02-01 06:16:13 +11:00
#include "lf.h"
2010-12-02 12:17:54 +11:00
#include "objects.h"
2011-02-01 06:16:13 +11:00
#include "text.h"
extern long curtime;
2010-12-02 12:17:54 +11:00
char *capitalise(char *text) {
2011-02-01 06:16:13 +11:00
if (strlen(text) > 0) {
text[0] = toupper(text[0]);
}
2010-12-02 12:17:54 +11:00
return text;
}
char *capitaliseall(char *text) {
if (strlen(text) > 0) {
char *p;
for (p = text ; *p; p++) {
if (p == text) { // first letter
*p = toupper(*p);
} else if (*(p-1) == ' ') { // first letter after a space
*p = toupper(*p);
}
}
}
return text;
}
2011-02-01 06:16:13 +11:00
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;
}
2010-12-02 12:17:54 +11:00
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;
2010-12-07 18:34:26 +11:00
char *newtext;
int rv;
newtext = strdup(text);
// scrolls
2011-02-01 06:16:13 +11:00
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);
2010-12-07 18:34:26 +11:00
if (rv) return newtext;
2011-02-01 06:16:13 +11:00
newtext = strrep(newtext, "lump ", "lumps ", &rv);
2010-12-07 18:34:26 +11:00
if (rv) return newtext;
2011-02-01 06:16:13 +11:00
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);
2010-12-07 18:34:26 +11:00
if (rv) return newtext;
2010-12-02 12:17:54 +11:00
2011-02-01 06:16:13 +11:00
//
newtext = strrep(newtext, "pair ", "pairs ", &rv);
2010-12-07 18:34:26 +11:00
// don't return
// default
2010-12-02 12:17:54 +11:00
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;
}
2010-12-07 18:34:26 +11:00
2011-02-01 06:16:13 +11:00
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) {
2010-12-07 18:34:26 +11:00
char *temp;
2011-02-01 06:16:13 +11:00
temp = strdup(" "); // ooooooo is this bad??
dostrrep(text, &temp, oldtok, newtok, rv);
2010-12-07 18:34:26 +11:00
// swap
2011-02-01 06:16:13 +11:00
text = realloc(text, strlen(temp)+1); // extra space for NUL
2010-12-07 18:34:26 +11:00
strcpy(text, temp);
free(temp);
2011-02-01 06:16:13 +11:00
return text;
2010-12-07 18:34:26 +11:00
}
// returns TRUE if any replacements made
2011-02-01 06:16:13 +11:00
char *dostrrep(char* in, char** out, char* oldtok, char* newtok, int *rv) {
2010-12-07 18:34:26 +11:00
char *temp;
char *found = strstr(in, oldtok);
int idx;
if(!found) {
2011-02-01 06:16:13 +11:00
*out = realloc(*out, strlen(in) + 1); // oooooooo crashing in realloc
2010-12-07 18:34:26 +11:00
strcpy(*out, in);
2011-02-01 06:16:13 +11:00
if (rv) *rv = B_FALSE;
return *out;
2010-12-07 18:34:26 +11:00
}
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';
2011-02-01 06:16:13 +11:00
dostrrep(found + strlen(oldtok), out, oldtok, newtok, rv);
2010-12-07 18:34:26 +11:00
temp = realloc(temp, strlen(temp) + strlen(*out) + 1);
strcat(temp,*out);
free(*out);
*out = temp;
2011-02-01 06:16:13 +11:00
if (rv) *rv = B_TRUE;
return *out;
2010-12-07 18:34:26 +11:00
}
2011-02-01 06:16:13 +11:00
int strpixmatch(char *haystack, char *needle) {
int matched = B_FALSE;
char *hword, *nword, *hcont,*ncont;
2010-12-07 18:34:26 +11:00
2011-02-01 06:16:13 +11:00
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";
}