Add standard checks to the assigning of a country name
Add check to ensure a country by that name does not exist. Ensure the length is not too long. Note this is a change behaviour for edit and change commands which used to silently truncate long names. Enforce that a country name can not have control characters in it. Ensure that a country name is not blank or just spaces.
This commit is contained in:
parent
17ad9fc5f2
commit
aa5861d10b
5 changed files with 44 additions and 28 deletions
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue