From 16e51939204337540f4c89f9a792dc4c3377a950 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Tue, 28 Sep 2021 05:19:46 +0300 Subject: [PATCH 43/43] Introduce new sex.[ch] module Initial version has just sex value macros and sex_by_name() method. See osdn #42937 Signed-off-by: Marko Lindqvist --- common/Makefile.am | 2 ++ common/sex.c | 37 +++++++++++++++++++++++++++++++++++++ common/sex.h | 30 ++++++++++++++++++++++++++++++ meson.build | 1 + server/ruleset.c | 28 +++++++++++++++++++++------- 5 files changed, 91 insertions(+), 7 deletions(-) create mode 100644 common/sex.c create mode 100644 common/sex.h diff --git a/common/Makefile.am b/common/Makefile.am index dea42192b0..9bfcd8f6fc 100644 --- a/common/Makefile.am +++ b/common/Makefile.am @@ -98,6 +98,8 @@ libfreeciv_la_SOURCES = \ road.h \ server_settings.h \ server_settings.c \ + sex.c \ + sex.h \ spaceship.c \ spaceship.h \ specialist.c \ diff --git a/common/sex.c b/common/sex.c new file mode 100644 index 0000000000..e33421daa2 --- /dev/null +++ b/common/sex.c @@ -0,0 +1,37 @@ +/**************************************************************************** + Freeciv - Copyright (C) 2004 - The Freeciv Team + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. +****************************************************************************/ + +#ifdef HAVE_CONFIG_H +#include +#endif + +/* utility */ +#include "support.h" + +#include "sex.h" + +/************************************************************************//** + Return sex by the name provided +****************************************************************************/ +int sex_by_name(const char *name) +{ + if (!fc_strcasecmp("Male", name)) { + return SEX_MALE; + } + + if (!fc_strcasecmp("Female", name)) { + return SEX_FEMALE; + } + + return SEX_UNKNOWN; +} diff --git a/common/sex.h b/common/sex.h new file mode 100644 index 0000000000..c05a5ae1c3 --- /dev/null +++ b/common/sex.h @@ -0,0 +1,30 @@ +/*********************************************************************** + Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. +***********************************************************************/ +#ifndef FC__SEX_H +#define FC__SEX_H + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +#define SEX_MALE 1 +#define SEX_FEMALE 0 +#define SEX_UNKNOWN -1 + +int sex_by_name(const char *name); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* FC__SEX_H */ diff --git a/meson.build b/meson.build index d1d645b9d2..f0d54ac7d8 100644 --- a/meson.build +++ b/meson.build @@ -582,6 +582,7 @@ common_lib = library('freeciv', 'common/rgbcolor.c', 'common/road.c', 'common/server_settings.c', + 'common/sex.c', 'common/spaceship.c', 'common/specialist.c', 'common/style.c', diff --git a/server/ruleset.c b/server/ruleset.c index b4b7e67d75..0c4699c3a0 100644 --- a/server/ruleset.c +++ b/server/ruleset.c @@ -54,6 +54,7 @@ #include "requirements.h" #include "rgbcolor.h" #include "road.h" +#include "sex.h" #include "specialist.h" #include "style.h" #include "tech.h" @@ -5026,7 +5027,8 @@ static bool load_ruleset_nations(struct section_file *file, /* Nation leaders. */ for (j = 0; j < MAX_NUM_LEADERS; j++) { - const char *sex; + const char *sexstr; + int sex; bool is_male = FALSE; name = secfile_lookup_str(file, "%s.leaders%d.name", sec_name, j); @@ -5046,22 +5048,34 @@ static bool load_ruleset_nations(struct section_file *file, break; } - sex = secfile_lookup_str(file, "%s.leaders%d.sex", sec_name, j); - if (NULL == sex) { + sexstr = secfile_lookup_str(file, "%s.leaders%d.sex", sec_name, j); + if (NULL == sexstr) { ruleset_error(LOG_ERROR, "Nation %s: leader \"%s\": %s.", nation_rule_name(pnation), name, secfile_error()); ok = FALSE; break; - } else if (0 == fc_strcasecmp("Male", sex)) { + } + + sex = sex_by_name(sexstr); + + switch (sex) { + case SEX_MALE: is_male = TRUE; - } else if (0 != fc_strcasecmp("Female", sex)) { + break; + case SEX_FEMALE: + is_male = FALSE; + break; + case SEX_UNKNOWN: ruleset_error(LOG_ERROR, "Nation %s: leader \"%s\" has unsupported " "sex variant \"%s\".", - nation_rule_name(pnation), name, sex); + nation_rule_name(pnation), name, sexstr); ok = FALSE; break; } - (void) nation_leader_new(pnation, name, is_male); + + if (ok) { + (void) nation_leader_new(pnation, name, is_male); + } } if (!ok) { break; -- 2.33.0