From 402c47b66ee8799afedcc050f3b7e41b2f40a1d6 Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Fri, 6 Jan 2006 20:27:17 +0000 Subject: [PATCH] (natargp): New, factored out of natarg(). --- include/prototypes.h | 1 + src/lib/subs/natarg.c | 61 +++++++++++++++++++++++++++---------------- 2 files changed, 40 insertions(+), 22 deletions(-) diff --git a/include/prototypes.h b/include/prototypes.h index 94cbd1ab..64005120 100644 --- a/include/prototypes.h +++ b/include/prototypes.h @@ -545,6 +545,7 @@ extern int msl_launch_mindam(struct emp_qelem *, coord, coord, int, /* mtch.c */ extern int comtch(register s_char *, struct cmndstr *, int); /* natarg.c */ +extern struct natstr *natargp(char *, char *); extern int natarg(char *, char *); /* neigh.c */ extern int neigh(coord, coord, natid); diff --git a/src/lib/subs/natarg.c b/src/lib/subs/natarg.c index 4293faa9..301eac0d 100644 --- a/src/lib/subs/natarg.c +++ b/src/lib/subs/natarg.c @@ -42,6 +42,41 @@ #include "prototypes.h" #include "optlist.h" +/* + * Get nation argument. + * If ARG is not empty, use it, else prompt for input using PROMPT. + * If no input is provided, return NULL. + * If the argument identifies a country, return its getnatp() value. + * Else complain and return NULL. + * Caution: this function doesn't care for lack of contact. + */ +struct natstr * +natargp(char *arg, char *prompt) +{ + char buf[1024]; + int n; + struct natstr *np; + + arg = getstarg(arg, prompt, buf); + if (arg == 0 || *arg == 0) + return NULL; + if (isdigit(*arg)) + n = atoi(arg); + else { + n = cnumb(arg); + if (n == M_NOTUNIQUE) { + pr("Country '%s' is ambiguous\n", arg); + return NULL; + } + } + np = getnatp(n); + if (!np) { + pr("Country '%s' doesn't exist.\n", arg); + return NULL; + } + return np; +} + /* * Get nation argument. * If ARG is not empty, use it, else prompt for input using PROMPT. @@ -55,34 +90,16 @@ int natarg(char *arg, char *prompt) { - char buf[1024]; - int n; - struct natstr *np; - - arg = getstarg(arg, prompt, buf); - if (arg == 0 || *arg == 0) + struct natstr *np = natargp(arg, prompt); + if (!np) return -1; - if (isdigit(*arg)) - n = atoi(arg); - else { - n = cnumb(arg); - if (n == M_NOTUNIQUE) { - pr("Country '%s' is ambiguous\n", arg); - return -1; - } - } - np = getnatp(n); - if (!np) { - pr("Country '%s' doesn't exist.\n", arg); - return -1; - } if (opt_HIDDEN) { - if (!player->god && !getcontact(getnatp(player->cnum), n)) { + if (!player->god && !getcontact(getnatp(player->cnum), np->nat_cnum)) { if (np->nat_stat != STAT_GOD) { pr("Country '%s' has not been contacted.\n", arg); return -1; } } } - return n; + return np->nat_cnum; }