replace strstr

Neu Funktion check_param die überprüft ob ein übergebener Wert in dem
Wert des entsprechenden Parameters steht.
This commit is contained in:
CTD 2014-08-08 13:29:26 +02:00
parent 8b9f344484
commit 90cf6d845f
4 changed files with 69 additions and 51 deletions

View file

@ -654,7 +654,6 @@ void building_set_owner(struct unit * owner)
static unit *building_owner_ex(const building * bld, const struct faction * last_owner) static unit *building_owner_ex(const building * bld, const struct faction * last_owner)
{ {
unit *u, *heir = 0; unit *u, *heir = 0;
const char *btypes;
/* Eigentümer tot oder kein Eigentümer vorhanden. Erste lebende Einheit /* Eigentümer tot oder kein Eigentümer vorhanden. Erste lebende Einheit
* nehmen. */ * nehmen. */
for (u = bld->region->units; u; u = u->next) { for (u = bld->region->units; u; u = u->next) {
@ -670,8 +669,7 @@ static unit *building_owner_ex(const building * bld, const struct faction * last
} }
} }
} }
btypes = get_param(global.parameters, "rules.region_owner_pay_building"); if (!heir && check_param(global.parameters, "rules.region_owner_pay_building", bld->type->_name)) {
if (btypes && !heir && (strstr(btypes, bld->type->_name) != NULL)) {
u = building_owner(largestbuilding(bld->region, &cmp_taxes, false)); u = building_owner(largestbuilding(bld->region, &cmp_taxes, false));
if (u) { if (u) {
heir = u; heir = u;

View file

@ -1882,6 +1882,27 @@ int get_param_int(const struct param *p, const char *key, int def)
return str ? atoi(str) : def; return str ? atoi(str) : def;
} }
int check_param(const struct param *p, const char *key, const char *searchvalue)
{
const char *value = get_param(p, key);
if (!value) {
return 0;
}
char *p_value = malloc(sizeof(char)* (strlen(value) + 1));
strcpy(p_value, value);
const char *delimiter = " ,;";
char *v = strtok(p_value, delimiter);
while (v != NULL) {
if (strcmp(v, searchvalue) == 0)
{
return 1;
}
v = strtok(NULL, delimiter);
}
return 0;
}
static const char *g_datadir; static const char *g_datadir;
const char *datapath(void) const char *datapath(void)
{ {

View file

@ -390,10 +390,11 @@ extern "C" {
const char *dbrace(const struct race *rc); const char *dbrace(const struct race *rc);
void set_param(struct param **p, const char *name, const char *data); void set_param(struct param **p, const char *key, const char *data);
const char *get_param(const struct param *p, const char *name); const char *get_param(const struct param *p, const char *key);
int get_param_int(const struct param *p, const char *name, int def); int get_param_int(const struct param *p, const char *key, int def);
float get_param_flt(const struct param *p, const char *name, float def); int check_param(const struct param *p, const char *key, const char *searchvalue);
float get_param_flt(const struct param *p, const char *key, float def);
bool ExpensiveMigrants(void); bool ExpensiveMigrants(void);
int NMRTimeout(void); int NMRTimeout(void);

View file

@ -1498,7 +1498,6 @@ static void prepare_reports(void)
region *r; region *r;
faction *f; faction *f;
building *b; building *b;
const char *btypes;
const struct building_type *bt_lighthouse = bt_find("lighthouse"); const struct building_type *bt_lighthouse = bt_find("lighthouse");
for (f = factions; f; f = f->next) { for (f = factions; f; f = f->next) {
@ -1524,8 +1523,7 @@ static void prepare_reports(void)
} }
/* Region owner get always the Lighthouse report */ /* Region owner get always the Lighthouse report */
btypes = get_param(global.parameters, "rules.region_owner_pay_building"); if (check_param(global.parameters, "rules.region_owner_pay_building", bt_lighthouse->_name)) {
if (btypes && strstr(btypes, bt_lighthouse->_name) != NULL) {
for (b = rbuildings(r); b; b = b->next) { for (b = rbuildings(r); b; b = b->next) {
if (b && b->type == bt_lighthouse) { if (b && b->type == bt_lighthouse) {
u = building_owner(largestbuilding(r, &cmp_taxes, false)); u = building_owner(largestbuilding(r, &cmp_taxes, false));