From 90cf6d845fe39bafd42f9f6aac96b7955154ed48 Mon Sep 17 00:00:00 2001 From: CTD Date: Fri, 8 Aug 2014 13:29:26 +0200 Subject: [PATCH] replace strstr MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Neu Funktion check_param die überprüft ob ein übergebener Wert in dem Wert des entsprechenden Parameters steht. --- src/kernel/building.c | 16 ++++------ src/kernel/config.c | 21 ++++++++++++ src/kernel/config.h | 9 +++--- src/kernel/reports.c | 74 +++++++++++++++++++++---------------------- 4 files changed, 69 insertions(+), 51 deletions(-) diff --git a/src/kernel/building.c b/src/kernel/building.c index e0e52e960..67e50a975 100644 --- a/src/kernel/building.c +++ b/src/kernel/building.c @@ -654,7 +654,6 @@ void building_set_owner(struct unit * owner) static unit *building_owner_ex(const building * bld, const struct faction * last_owner) { unit *u, *heir = 0; - const char *btypes; /* Eigentümer tot oder kein Eigentümer vorhanden. Erste lebende Einheit * nehmen. */ for (u = bld->region->units; u; u = u->next) { @@ -670,14 +669,13 @@ static unit *building_owner_ex(const building * bld, const struct faction * last } } } - btypes = get_param(global.parameters, "rules.region_owner_pay_building"); - if (btypes && !heir && (strstr(btypes, bld->type->_name) != NULL)) { - u = building_owner(largestbuilding(bld->region, &cmp_taxes, false)); - if (u) { - heir = u; - } - } - return heir; + if (!heir && check_param(global.parameters, "rules.region_owner_pay_building", bld->type->_name)) { + u = building_owner(largestbuilding(bld->region, &cmp_taxes, false)); + if (u) { + heir = u; + } + } + return heir; } unit *building_owner(const building * bld) diff --git a/src/kernel/config.c b/src/kernel/config.c index 8be554c7d..2295e0f28 100644 --- a/src/kernel/config.c +++ b/src/kernel/config.c @@ -1882,6 +1882,27 @@ int get_param_int(const struct param *p, const char *key, int 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; const char *datapath(void) { diff --git a/src/kernel/config.h b/src/kernel/config.h index 958beef42..28a5dcda9 100644 --- a/src/kernel/config.h +++ b/src/kernel/config.h @@ -390,10 +390,11 @@ extern "C" { const char *dbrace(const struct race *rc); - void set_param(struct param **p, const char *name, const char *data); - const char *get_param(const struct param *p, const char *name); - int get_param_int(const struct param *p, const char *name, int def); - float get_param_flt(const struct param *p, const char *name, float def); + void set_param(struct param **p, const char *key, const char *data); + const char *get_param(const struct param *p, const char *key); + int get_param_int(const struct param *p, const char *key, int 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); int NMRTimeout(void); diff --git a/src/kernel/reports.c b/src/kernel/reports.c index ce8f33aa9..454dbf3c0 100644 --- a/src/kernel/reports.c +++ b/src/kernel/reports.c @@ -1498,7 +1498,6 @@ static void prepare_reports(void) region *r; faction *f; building *b; - const char *btypes; const struct building_type *bt_lighthouse = bt_find("lighthouse"); for (f = factions; f; f = f->next) { @@ -1523,44 +1522,43 @@ static void prepare_reports(void) } } - /* Region owner get always the Lighthouse report */ - btypes = get_param(global.parameters, "rules.region_owner_pay_building"); - if (btypes && strstr(btypes, bt_lighthouse->_name) != NULL) { - for (b = rbuildings(r); b; b = b->next) { - if (b && b->type == bt_lighthouse) { - u = building_owner(largestbuilding(r, &cmp_taxes, false)); - /* alternativ: u = building_owner(b); if not all region owners should see */ - if (u) { - prepare_lighthouse(b, u->faction); - if (u_race(u) != get_race(RC_SPELL) || u->number == RS_FARVISION) { - if (fval(u, UFL_DISBELIEVES)) { - add_seen(u->faction->seen, r, see_unit, true); - } - else { - add_seen(u->faction->seen, r, see_unit, false); - } - } - } - } - } - } - - for (u = r->units; u; u = u->next) { - if (u->building && u->building->type == bt_lighthouse) { - /* we are in a lighthouse. add the regions we can see from here! */ - prepare_lighthouse(u->building, u->faction); - } + /* Region owner get always the Lighthouse report */ + if (check_param(global.parameters, "rules.region_owner_pay_building", bt_lighthouse->_name)) { + for (b = rbuildings(r); b; b = b->next) { + if (b && b->type == bt_lighthouse) { + u = building_owner(largestbuilding(r, &cmp_taxes, false)); + /* alternativ: u = building_owner(b); if not all region owners should see */ + if (u) { + prepare_lighthouse(b, u->faction); + if (u_race(u) != get_race(RC_SPELL) || u->number == RS_FARVISION) { + if (fval(u, UFL_DISBELIEVES)) { + add_seen(u->faction->seen, r, see_unit, true); + } + else { + add_seen(u->faction->seen, r, see_unit, false); + } + } + } + } + } + } + + for (u = r->units; u; u = u->next) { + if (u->building && u->building->type == bt_lighthouse) { + /* we are in a lighthouse. add the regions we can see from here! */ + prepare_lighthouse(u->building, u->faction); + } + + if (u_race(u) != get_race(RC_SPELL) || u->number == RS_FARVISION) { + if (fval(u, UFL_DISBELIEVES)) { + add_seen(u->faction->seen, r, see_unit, true); + } + else { + add_seen(u->faction->seen, r, see_unit, false); + } + } + } - if (u_race(u) != get_race(RC_SPELL) || u->number == RS_FARVISION) { - if (fval(u, UFL_DISBELIEVES)) { - add_seen(u->faction->seen, r, see_unit, true); - } - else { - add_seen(u->faction->seen, r, see_unit, false); - } - } - } - if (fval(r, RF_TRAVELUNIT)) { for (ru = a_find(r->attribs, &at_travelunit);