Added lastword parsing

This commit is contained in:
Devine Lu Linvega 2020-09-09 11:08:22 -07:00
parent 2199b38886
commit fd0ae6dacc
1 changed files with 73 additions and 45 deletions

118
main.c
View File

@ -31,28 +31,36 @@ Vessel* guest;
/* helpers */
int
cspa(char c)
cisp(char c)
{
return c == ' ' || c == '\t' || c == '\n' || c == '\r';
}
int
slen(char* src)
slen(char* s)
{
int n = 0;
if(src[0] == '\0')
return 0;
while(src[++n])
while(s[n] != '\0' && s[++n])
;
return n;
}
int
cpos(char* str, char c)
cpof(char* s, char c)
{
int i;
for(i = 0; i < slen(str); i++)
if(str[i] == c)
for(i = 0; i < slen(s); i++)
if(s[i] == c)
return i;
return -1;
}
int
cpol(char* s, char c)
{
int i;
for(i = slen(s); i >= 0; i--)
if(s[i] == c)
return i;
return -1;
}
@ -60,27 +68,28 @@ cpos(char* str, char c)
int
scmp(char* a, char* b)
{
int i;
if(slen(a) != slen(b))
int i, l = slen(a);
if(l != slen(b))
return 0;
for(i = 0; i < slen(a); ++i)
for(i = 0; i < l; ++i)
if(a[i] != b[i])
return 0;
return 1;
}
char*
strm(char* src)
strm(char* s)
{
int i = -1, j = slen(src);
if(src[0] == '\0' || j == 1)
return src;
while(src[++i] && cspa(src[i]))
;
while(src[--j] && cspa(src[j]))
;
src[j + 1] = '\0';
return src + i;
char* end;
while(cisp(*s))
s++;
if(*s == 0)
return s;
end = s + slen(s) - 1;
while(end > s && cisp(*end))
end--;
end[1] = '\0';
return s;
}
void
@ -93,7 +102,7 @@ scpy(char* src, char* dest)
}
int
spos(char* src[], int len, char* val)
sfin(char* src[], int len, char* val)
{
int i;
for(i = 0; i < len; i++)
@ -111,6 +120,12 @@ sstr(char* src, char* dest, int from, int to)
/* Parade */
char*
attribute(char* src)
{
return strrchr(src, ' ') + 1;
}
int
isvisible(Vessel* g, Vessel* v)
{
@ -123,7 +138,7 @@ isvisible(Vessel* g, Vessel* v)
return 1;
}
void
Vessel*
addvessel(Parade* p, Vessel* v, char* name)
{
Vessel* nv = &p->vessels[p->len];
@ -133,16 +148,18 @@ addvessel(Parade* p, Vessel* v, char* name)
nv->parent = v ? v->parent : nv;
strcpy(nv->name, name);
p->len++;
return nv;
}
Vessel*
findvisible(Parade* p, Vessel* v, char* name)
{
int i;
char* n = strrchr(name, ' ') + 1;
for(i = 0; i < p->len; ++i) {
if(!isvisible(v, &p->vessels[i]))
continue;
if(scmp(p->vessels[i].name, name))
if(scmp(p->vessels[i].name, n))
return &p->vessels[i];
}
return NULL;
@ -152,12 +169,13 @@ Vessel*
findinventory(Parade* p, Vessel* v, char* name)
{
int i;
char* n = strrchr(name, ' ') + 1;
for(i = 0; i < p->len; ++i) {
if(&p->vessels[i] == v)
continue;
if(p->vessels[i].parent != v)
continue;
if(scmp(p->vessels[i].name, name))
if(scmp(p->vessels[i].name, n))
return &p->vessels[i];
}
@ -168,8 +186,9 @@ Vessel*
findany(Parade* p, char* name)
{
int i;
char* n = strrchr(name, ' ') + 1;
for(i = 0; i < p->len; ++i)
if(scmp(p->vessels[i].name, name))
if(scmp(p->vessels[i].name, n))
return &p->vessels[i];
return NULL;
}
@ -179,11 +198,12 @@ findany(Parade* p, char* name)
void
createvessel(Parade* p, char* val)
{
if(findany(p, val))
Vessel* v;
if(findany(p, val) && slen(val) > 3)
printf("You cannot create %s.\n", val);
else {
addvessel(p, guest, val);
printf("You created %s.\n", val);
v = addvessel(p, guest, strrchr(val, ' ') + 1);
printf("You created a %s.\n", v->name);
}
}
@ -195,7 +215,7 @@ becomevessel(Parade* p, char* val)
printf("You do not see %s.\n", val);
else {
guest = v;
printf("You became %s.\n", v->name);
printf("You became the %s.\n", v->name);
}
}
@ -209,7 +229,7 @@ entervessel(Parade* p, char* val)
printf("Your vessel is locked.\n");
else {
guest->parent = v;
printf("You entered %s.\n", v->name);
printf("You entered the %s.\n", v->name);
}
}
@ -219,7 +239,7 @@ leavevessel(void)
if(guest->parent == guest->parent->parent)
printf("You cannot leave %s.\n", guest->parent->name);
else {
printf("You left %s.\n", guest->parent->name);
printf("You left the %s.\n", guest->parent->name);
guest->parent = guest->parent->parent;
}
}
@ -234,7 +254,7 @@ takevessel(Parade* p, char* val)
printf("The vessel is locked.\n");
else {
v->parent = guest;
printf("You took %s.\n", v->name);
printf("You took the %s.\n", v->name);
}
}
@ -245,10 +265,10 @@ dropvessel(Parade* p, char* val)
if(!v)
printf("You do not carry %s.\n", val);
else if(v->lock)
printf("The vessel is locked.\n");
printf("The %s is locked.\n", v->name);
else {
v->parent = guest->parent->parent;
printf("You dropped %s.\n", v->name);
printf("You dropped the %s.\n", v->name);
}
}
@ -259,10 +279,10 @@ lockvessel(Parade* p, char* val)
if(!v)
printf("You do not see %s.\n", val);
else if(v->owner != guest)
printf("You cannot lock %s.\n", v->name);
printf("You cannot lockthe %s.\n", v->name);
else {
v->lock = 1;
printf("You locked %s.\n", v->name);
printf("You locked the %s.\n", v->name);
}
}
@ -273,9 +293,9 @@ unlockvessel(Parade* p, char* val)
if(!v)
printf("You do not see %s.\n", val);
else if(v->owner != guest)
printf("You cannot unlock %s.\n", v->name);
printf("You cannot unlock the %s.\n", v->name);
else {
printf("You unlocked %s.\n", v->name);
printf("You unlocked the %s.\n", v->name);
v->lock = 1;
}
}
@ -284,10 +304,10 @@ void
notevessel(char* val)
{
if(guest->parent->lock)
printf("You cannot add a note to %s.\n", guest->parent->name);
printf("You cannot add a note to the %s.\n", guest->parent->name);
else {
scpy(val, guest->parent->note);
printf("You added a note to %s.\n", guest->parent->name);
printf("You added a note to the %s.\n", guest->parent->name);
}
}
@ -295,10 +315,10 @@ void
programvessel(char* val)
{
if(guest->parent->lock)
printf("You cannot program %s.\n", guest->parent->name);
printf("You cannot program the %s.\n", guest->parent->name);
else {
scpy(val, guest->parent->prog);
printf("You programmed %s.\n", guest->parent->name);
printf("You programmed the %s.\n", guest->parent->name);
}
}
@ -335,7 +355,7 @@ usevessel(Parade* p, char* val)
void
act(Parade* p, char* cmd, char* val)
{
switch(spos(actions, 13, cmd)) {
switch(sfin(actions, 13, cmd)) {
case 0x0:
createvessel(p, val);
break;
@ -381,7 +401,7 @@ act(Parade* p, char* cmd, char* val)
void
answer(Parade* p, char* input)
{
int split = cpos(input, ' ');
int split = cpof(input, ' ');
char action[NAMELEN], value[NAMELEN];
if(scmp(input, "quit"))
exit(0);
@ -417,7 +437,15 @@ main(void)
addvessel(&parade, &parade.vessels[0], "the ghost");
guest = &parade.vessels[1];
answer(&parade, "");
answer(&parade, "create a machine");
answer(&parade, "enter the machine");
answer(&parade, "program create a coffee");
answer(&parade, "leave");
answer(&parade, "use the machine");
/*
while(1)
listen(&parade);
*/
return 0;
}