]> git.pond.sub.org Git - empserver/blobdiff - src/lib/subs/natsub.c
Add standard checks to the assigning of a country name
[empserver] / src / lib / subs / natsub.c
index 04c8e8dc980ac135b92231a6b38a6331799855b2..8374ccb9135faecb5d54bdd285dd9673ff805343 100644 (file)
@@ -34,6 +34,7 @@
 #include <config.h>
 
 #include <string.h>
+#include <ctype.h>
 #include <fcntl.h>
 #include <sys/stat.h>
 #include <unistd.h>
@@ -41,6 +42,7 @@
 #include "game.h"
 #include "nat.h"
 #include "optlist.h"
+#include "prototypes.h"
 #include "tel.h"
 #include "xy.h"
 
@@ -112,3 +114,38 @@ nat_reset(struct natstr *natp, enum nat_status stat, coord x, coord y)
 
     return natp;
 }
+
+int
+check_nat_name(char *cname)
+{
+    struct natstr *natp;
+    natid cn;
+    int nonb;
+    char *temp;
+
+    if (strlen(cname) >= sizeof(natp->nat_cnam)) {
+       pr("Country name too long\n");
+       return 0;
+    }
+
+    nonb = 0;
+    for (temp = cname; *temp != '\0'; temp++) {
+       if (iscntrl(*temp)) {
+           pr("No control characters allowed in country names!\n");
+           return 0;
+       } else if (!isspace(*temp))
+           nonb = 1;
+    }
+    if (!nonb) {
+       pr("Country name can't be all blank\n");
+       return 0;
+    }
+
+    for (cn = 0; NULL != (natp = getnatp(cn)); cn++) {
+       if (!strcmp(cname, natp->nat_cnam)) {
+           pr("Country #%d is already called `%s'\n", cn, cname);
+           return 0;
+       }
+    }
+    return 1;
+}