(item_by_name): New, factored out of whatitem(). Use it to simplify

some uses of whatitem().
This commit is contained in:
Markus Armbruster 2004-04-08 19:26:55 +00:00
parent e42f7e8210
commit a8a6f5649f
4 changed files with 34 additions and 14 deletions

View file

@ -52,6 +52,7 @@ extern struct ichrstr ichr[];
/* procedures using/returning this struct */
extern struct ichrstr *whatitem(s_char *ptr, s_char *prompt);
extern struct ichrstr *whatitem(char *, char *);
extern struct ichrstr *item_by_name(char *);
#endif /* _ITEM_H_ */

View file

@ -94,10 +94,10 @@ load(void)
type = EF_PLANE;
else if (!strncmp(p, "land", 4))
type = EF_LAND;
else if (NULL != (ich = whatitem(p, (s_char *)0)))
else if (NULL != (ich = item_by_name(p)))
type = EF_SECTOR;
else {
pr("Bad commodity.\n");
pr("Can't load '%s'\n", p);
return RET_SYN;
}
@ -234,10 +234,10 @@ lload(void)
type = EF_PLANE;
else if (!strncmp(p, "land", 4))
type = EF_LAND;
else if (NULL != (ich = whatitem(p, (s_char *)0)))
else if (NULL != (ich = item_by_name(p)))
type = EF_SECTOR;
else {
pr("Bad commodity.\n");
pr("Can't load '%s'\n", p);
return RET_SYN;
}

View file

@ -80,10 +80,10 @@ tend(void)
if (!strncmp(p, "land", 4))
type = EF_LAND;
else if (NULL != (ip = whatitem(p, (s_char *)0)))
else if (NULL != (ip = item_by_name(p)))
type = EF_SECTOR;
else {
pr("Bad commodity.\n");
pr("Can't tend '%s'\n", p);
return RET_SYN;
}

View file

@ -35,20 +35,39 @@
#include "item.h"
#include "prototypes.h"
/*
* Get item type argument.
* If INPUT is not empty, use it, else prompt for more input using PROMPT.
* Return item characteristics on success, else NULL.
*/
struct ichrstr *
whatitem(s_char *ptr, s_char *prompt)
whatitem(char *input, char *prompt)
{
register s_char *p;
register struct ichrstr *ip;
s_char buf[1024];
char *p;
struct ichrstr *ip;
char buf[1024];
p = getstarg(ptr, prompt, buf);
p = getstarg(input, prompt, buf);
if (p == 0 || *p == 0)
return 0;
ip = item_by_name(p);
if (!ip)
pr("Unrecognized item \"%c\"\n", *p);
return ip;
}
/*
* Map item type name STR to item characteristics, NULL if not found.
*/
struct ichrstr *
item_by_name(char *str)
{
struct ichrstr *ip;
for (ip = &ichr[1]; ip->i_mnem != 0; ip++) {
if (*p == ip->i_mnem)
/* FIXME check i_name if str[2]? */
if (*str == ip->i_mnem)
return ip;
}
pr("Unrecognized item \"%c\"\n", *p);
return 0;
}