]> git.pond.sub.org Git - empserver/blobdiff - src/lib/subs/cnumb.c
Get rid of src/lib/gen/copy.c
[empserver] / src / lib / subs / cnumb.c
index 044e4adbe0f3a5314991b3bf680586bc0af6b2fd..26a48b1120855451f50b5e41f236a2bb8d4587e7 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *  Copyright (C) 1986-2008, Dave Pare, Jeff Bailey, Thomas Ruschak,
  *                           Ken Stevens, Steve McClure
  *
  *  This program is free software; you can redistribute it and/or modify
@@ -19,9 +19,9 @@
  *
  *  ---
  *
- *  See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
- *  related information and legal notices. It is expected that any future
- *  projects/authors will amend these files as needed.
+ *  See files README, COPYING and CREDITS in the root of the source
+ *  tree for related information and legal notices.  It is expected
+ *  that future projects/authors will amend these files as needed.
  *
  *  ---
  *
  *     
  */
 
-#include "misc.h"
-#include "player.h"
-#include "nat.h"
+#include <config.h>
+
 #include "file.h"
+#include "match.h"
+#include "nat.h"
 #include "prototypes.h"
 
+/*
+ * Search for a country matching CNTRY, return its number.
+ * Return M_NOTFOUND if no such country exists, M_NOTUNIQUE if there
+ * are several.
+ */
 int
-cnumb(s_char *cntry)
+cnumb(char *cntry)
 {
-       register s_char *ncp;
-       register s_char *cp;
-       register struct natstr *natp;
-       int     bcount;
-       natid   best;
-       natid   cn;
+    char *ncp;
+    char *cp;
+    struct natstr *natp;
+    int res;
+    natid cn;
 
-       bcount = 0;
-       for (cn = 0; cn < MAXNOC; cn++) {
-               if ((natp = getnatp(cn)) == 0)
-                       break;
-               ncp = natp->nat_cnam;
-               for (cp=cntry; *cp == *ncp; cp++, ncp++) {
-                       if (*cp == 0)
-                               return cn;
-               }
-               if (cp > cntry && *cp == 0) {
-                       best = cn;
-                       bcount++;
-               }
+    res = M_NOTFOUND;
+    for (cn = 0; cn < MAXNOC; cn++) {
+       if ((natp = getnatp(cn)) == 0)
+           break;
+       if (natp->nat_stat == STAT_UNUSED)
+           continue;
+       ncp = natp->nat_cnam;
+       for (cp = cntry; *cp == *ncp; cp++, ncp++) {
+           if (*cp == 0)
+               return cn;      /* exact match */
+       }
+       if (*cp == 0) {
+           /* is a prefix */
+           if (res >= 0)
+               return M_NOTUNIQUE;
+           res = cn;
        }
-       /* can only have 1 match or not successful */
-       return bcount == 1 ? (int) best : -1;
+    }
+    return res;
 }