From 0d5cdeb7aa55979cfc53c7a1e8345cb007cb309d Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Sun, 5 Nov 2023 23:13:27 +0200 Subject: [PATCH 39/39] Improve report.c coding style See osdn #48949 Signed-off-by: Marko Lindqvist --- server/report.c | 147 ++++++++++++++++++++++++------------------------ 1 file changed, 74 insertions(+), 73 deletions(-) diff --git a/server/report.c b/server/report.c index e06fe0f813..34879233e2 100644 --- a/server/report.c +++ b/server/report.c @@ -66,7 +66,7 @@ struct logging_civ_score { * the end of previous turn in the beginning to turn 0. */ struct history_report latest_history_report = { -2 }; -static struct logging_civ_score *score_log = NULL; +static struct logging_civ_score *score_log = nullptr; static void plrdata_slot_init(struct plrdata_slot *plrdata, const char *name); @@ -87,10 +87,10 @@ enum historian_type { HISTORIAN_HAPPIEST = 3, HISTORIAN_LARGEST = 4}; -#define HISTORIAN_FIRST HISTORIAN_RICHEST -#define HISTORIAN_LAST HISTORIAN_LARGEST +#define HISTORIAN_FIRST HISTORIAN_RICHEST +#define HISTORIAN_LAST HISTORIAN_LARGEST -static const char *historian_message[]={ +static const char *historian_message[] = { /* TRANS: year reports ... */ N_("%s %s reports on the RICHEST Civilizations in the World."), /* TRANS: year reports ... */ @@ -103,7 +103,7 @@ static const char *historian_message[]={ N_("%s %s reports on the LARGEST Civilizations in the World.") }; -static const char *historian_name[]={ +static const char *historian_name[] = { /* TRANS: [year] [reports ...] */ N_("Herodotus"), /* TRANS: [year] [reports ...] */ @@ -193,7 +193,7 @@ static struct dem_col { char key; } coltable[] = {{'q'}, {'r'}, {'b'}}; /* Corresponds to dem_flag enum */ -/* prime number of entries makes for better scaling */ +/* Prime number of entries makes for better scaling */ static const char *ranking[] = { /* TRANS: <#>: The Poles */ N_("%2d: The Supreme %s"), @@ -233,7 +233,7 @@ static const char *ranking[] = { static int secompare(const void *a, const void *b) { return (((const struct player_score_entry *)b)->value - - ((const struct player_score_entry *)a)->value); + ((const struct player_score_entry *)a)->value); } /**********************************************************************//** @@ -250,45 +250,45 @@ static void historian_generic(struct history_report *report, if (GOOD_PLAYER(pplayer)) { switch (which_news) { case HISTORIAN_RICHEST: - size[j].value = pplayer->economic.gold; - break; + size[j].value = pplayer->economic.gold; + break; case HISTORIAN_ADVANCED: - size[j].value - = pplayer->score.techs + research_get(pplayer)->future_tech; - break; + size[j].value + = pplayer->score.techs + research_get(pplayer)->future_tech; + break; case HISTORIAN_MILITARY: - size[j].value = pplayer->score.units; - break; - case HISTORIAN_HAPPIEST: - size[j].value = - (((pplayer->score.happy - pplayer->score.unhappy + size[j].value = pplayer->score.units; + break; + case HISTORIAN_HAPPIEST: + size[j].value + = (((pplayer->score.happy - pplayer->score.unhappy - 2 * pplayer->score.angry) * 1000) / (1 + total_player_citizens(pplayer))); - break; + break; case HISTORIAN_LARGEST: - size[j].value = total_player_citizens(pplayer); - break; + size[j].value = total_player_citizens(pplayer); + break; } size[j].player = pplayer; j++; - } /* else the player is dead or barbarian or observer */ + } /* Else the player is dead or barbarian or observer */ } players_iterate_end; qsort(size, j, sizeof(size[0]), secompare); report->body[0] = '\0'; for (i = 0; i < j; i++) { if (i > 0 && size[i].value < size[i - 1].value) { - /* since i < j, only top entry reigns Supreme */ + /* Since i < j, only top entry reigns Supreme */ rank = ((i * ARRAY_SIZE(ranking)) / j) + 1; } if (rank >= ARRAY_SIZE(ranking)) { - /* clamp to final entry */ + /* Clamp to final entry */ rank = ARRAY_SIZE(ranking) - 1; } cat_snprintf(report->body, REPORT_BODYSIZE, - _(ranking[rank]), - i + 1, - nation_plural_for_player(size[i].player)); + _(ranking[rank]), + i + 1, + nation_plural_for_player(size[i].player)); fc_strlcat(report->body, "\n", REPORT_BODYSIZE); } fc_snprintf(report->title, REPORT_TITLESIZE, _(historian_message[which_news]), @@ -340,7 +340,7 @@ void report_top_cities(struct conn_list *dest) for (i = 0; i < game.info.top_cities_count; i++) { size[i].value = 0; - size[i].city = NULL; + size[i].city = nullptr; } shuffled_players_iterate(pplayer) { @@ -360,7 +360,7 @@ void report_top_cities(struct conn_list *dest) for (i = 0; i < game.info.top_cities_count; i++) { int wonders; - if (size[i].city == NULL) { + if (size[i].city == nullptr) { /* * There are less than game.info.top_cities_count cities in * the whole game. @@ -717,7 +717,7 @@ static int get_settlers(const struct player *pplayer) int result = 0; if (!game.scenario.prevent_new_cities) { - /* count up settlers */ + /* Count up settlers */ unit_list_iterate(pplayer->units, punit) { if (unit_can_do_action(punit, ACTION_FOUND_CITY)) { result++; @@ -957,6 +957,7 @@ static const char *percent_to_text(int value) static const char *production_to_text(int value) { int clip = MAX(0, value); + /* TRANS: "M tons" = million tons, so always plural */ return value_units(clip, PL_(" M tons", " M tons", clip)); } @@ -1015,7 +1016,7 @@ static void dem_line_item(char *outptr, size_t out_size, struct player *pplayer, struct dem_row *prow, bv_cols selcols) { - if (NULL != pplayer && BV_ISSET(selcols, DEM_COL_QUANTITY)) { + if (pplayer != nullptr && BV_ISSET(selcols, DEM_COL_QUANTITY)) { const char *text = prow->to_text(prow->get_value(pplayer)); cat_snprintf(outptr, out_size, " %s", text); @@ -1023,26 +1024,26 @@ static void dem_line_item(char *outptr, size_t out_size, 18 - (int) get_internal_string_length(text), ""); } - if (NULL != pplayer && BV_ISSET(selcols, DEM_COL_RANK)) { + if (pplayer != nullptr && BV_ISSET(selcols, DEM_COL_RANK)) { int basis = prow->get_value(pplayer); int place = 1; players_iterate(other) { if (GOOD_PLAYER(other) - && ((prow->greater_values_are_better - && prow->get_value(other) > basis) - || (!prow->greater_values_are_better - && prow->get_value(other) < basis))) { - place++; + && ((prow->greater_values_are_better + && prow->get_value(other) > basis) + || (!prow->greater_values_are_better + && prow->get_value(other) < basis))) { + place++; } } players_iterate_end; cat_snprintf(outptr, out_size, _("(ranked %d)"), place); } - - if (NULL == pplayer || BV_ISSET(selcols, DEM_COL_BEST)) { + + if (pplayer == nullptr || BV_ISSET(selcols, DEM_COL_BEST)) { struct player *best_player = pplayer; - int best_value = NULL != pplayer ? prow->get_value(pplayer) : 0; + int best_value = (pplayer != nullptr) ? prow->get_value(pplayer) : 0; players_iterate(other) { if (GOOD_PLAYER(other)) { @@ -1057,20 +1058,20 @@ static void dem_line_item(char *outptr, size_t out_size, } } players_iterate_end; - if (NULL == pplayer + if (pplayer == nullptr || (team_has_embassy(pplayer->team, best_player) && (pplayer != best_player))) { cat_snprintf(outptr, out_size, " %s: %s", - nation_plural_for_player(best_player), - prow->to_text(prow->get_value(best_player))); + nation_plural_for_player(best_player), + prow->to_text(prow->get_value(best_player))); } } } /**********************************************************************//** - Verify that a given demography string is valid. See - game.demography. If the string is not valid the index of the _first_ - invalid character is return as 'error'. + Verify that a given demography string is valid. See game.demography. + If the string is not valid the index of the _first_ invalid character + is return as 'error'. Other settings callback functions are in settings.c, but this one uses static values from this file so it's done separately. @@ -1079,8 +1080,8 @@ bool is_valid_demography(const char *demography, int *error) { int len = strlen(demography), i; - /* We check each character individually to see if it's valid. This - * does not check for duplicate entries. */ + /* We check each character individually to see if it's valid. + * This does not check for duplicate entries. */ for (i = 0; i < len; i++) { bool found = FALSE; int j; @@ -1088,8 +1089,8 @@ bool is_valid_demography(const char *demography, int *error) /* See if the character is a valid column label. */ for (j = 0; j < DEM_COL_LAST; j++) { if (demography[i] == coltable[j].key) { - found = TRUE; - break; + found = TRUE; + break; } } @@ -1100,13 +1101,13 @@ bool is_valid_demography(const char *demography, int *error) /* See if the character is a valid row label. */ for (j = 0; j < ARRAY_SIZE(rowtable); j++) { if (demography[i] == rowtable[j].key) { - found = TRUE; - break; + found = TRUE; + break; } } if (!found) { - if (error != NULL) { + if (error != nullptr) { (*error) = i; } /* The character is invalid. */ @@ -1120,7 +1121,7 @@ bool is_valid_demography(const char *demography, int *error) /**********************************************************************//** Send demographics report; what gets reported depends on value of - demographics server option. + demographics server option. **************************************************************************/ void report_demographics(struct connection *pconn) { @@ -1192,7 +1193,7 @@ void report_achievements(struct connection *pconn) char buffer[4096]; struct player *pplayer = pconn->playing; - if (pplayer == NULL) { + if (pplayer == nullptr) { return; } @@ -1219,7 +1220,7 @@ void report_achievements(struct connection *pconn) static void plrdata_slot_init(struct plrdata_slot *plrdata, const char *name) { - fc_assert_ret(plrdata->name == NULL); + fc_assert_ret(plrdata->name == nullptr); plrdata->name = fc_calloc(MAX_LEN_NAME, sizeof(plrdata->name)); plrdata_slot_replace(plrdata, name); @@ -1231,7 +1232,7 @@ static void plrdata_slot_init(struct plrdata_slot *plrdata, static void plrdata_slot_replace(struct plrdata_slot *plrdata, const char *name) { - fc_assert_ret(plrdata->name != NULL); + fc_assert_ret(plrdata->name != nullptr); fc_strlcpy(plrdata->name, name, MAX_LEN_NAME); } @@ -1241,9 +1242,9 @@ static void plrdata_slot_replace(struct plrdata_slot *plrdata, **************************************************************************/ static void plrdata_slot_free(struct plrdata_slot *plrdata) { - if (plrdata->name != NULL) { + if (plrdata->name != nullptr) { free(plrdata->name); - plrdata->name = NULL; + plrdata->name = nullptr; } } @@ -1266,8 +1267,8 @@ static bool scan_score_log(char *id) * saving couple of bytes. */ char plr_name[MAX(MAX_LEN_NAME, MAX_SCORELOG_LINE_LEN - strlen("addplayer "))]; - fc_assert_ret_val(score_log != NULL, FALSE); - fc_assert_ret_val(score_log->fp != NULL, FALSE); + fc_assert_ret_val(score_log != nullptr, FALSE); + fc_assert_ret_val(score_log->fp != nullptr, FALSE); score_log->last_turn = -1; id[0] = '\0'; @@ -1352,7 +1353,7 @@ static bool scan_score_log(char *id) } plrdata = score_log->plrdata + plr_no; - if (plrdata->name != NULL) { + if (plrdata->name != nullptr) { log_error("[%s:%d] Two names for one player (id %d)!", game.server.scorefile, line_nr, plr_no); return FALSE; @@ -1376,7 +1377,7 @@ static bool scan_score_log(char *id) } plrdata = score_log->plrdata + plr_no; - if (plrdata->name == NULL) { + if (plrdata->name == nullptr) { log_error("[%s:%d] Trying to remove undefined player (id %d)!", game.server.scorefile, line_nr, plr_no); return FALSE; @@ -1410,19 +1411,19 @@ static bool scan_score_log(char *id) **************************************************************************/ void log_civ_score_init(void) { - if (score_log != NULL) { + if (score_log != nullptr) { return; } score_log = fc_calloc(1, sizeof(*score_log)); - score_log->fp = NULL; + score_log->fp = nullptr; score_log->last_turn = -1; score_log->plrdata = fc_calloc(player_slot_count(), sizeof(*score_log->plrdata)); player_slots_iterate(pslot) { struct plrdata_slot *plrdata = score_log->plrdata + player_slot_index(pslot); - plrdata->name = NULL; + plrdata->name = nullptr; } player_slots_iterate_end; latest_history_report.turn = -2; @@ -1434,20 +1435,20 @@ void log_civ_score_init(void) void log_civ_score_free(void) { if (!score_log) { - /* nothing to do */ + /* Nothing to do */ return; } if (score_log->fp) { fclose(score_log->fp); - score_log->fp = NULL; + score_log->fp = nullptr; } if (score_log->plrdata) { player_slots_iterate(pslot) { struct plrdata_slot *plrdata = score_log->plrdata + player_slot_index(pslot); - if (plrdata->name != NULL) { + if (plrdata->name != nullptr) { free(plrdata->name); } } player_slots_iterate_end; @@ -1455,7 +1456,7 @@ void log_civ_score_free(void) } free(score_log); - score_log = NULL; + score_log = nullptr; } /**********************************************************************//** @@ -1534,7 +1535,7 @@ void log_civ_score_now(void) oper = SL_APPEND; fclose(score_log->fp); - score_log->fp = NULL; + score_log->fp = nullptr; } } @@ -1581,7 +1582,7 @@ void log_civ_score_now(void) player_slots_iterate(pslot) { struct plrdata_slot *plrdata = score_log->plrdata + player_slot_index(pslot); - if (plrdata->name != NULL + if (plrdata->name != nullptr && player_slot_is_used(pslot)) { struct player *pplayer = player_slot_get_player(pslot); @@ -1596,7 +1597,7 @@ void log_civ_score_now(void) players_iterate(pplayer) { struct plrdata_slot *plrdata = score_log->plrdata + player_index(pplayer); - if (plrdata->name == NULL && GOOD_PLAYER(pplayer)) { + if (plrdata->name == nullptr && GOOD_PLAYER(pplayer)) { switch (game.server.scoreloglevel) { case SL_HUMANS: if (is_ai(pplayer)) { @@ -1619,7 +1620,7 @@ void log_civ_score_now(void) if (GOOD_PLAYER(pplayer)) { switch (game.server.scoreloglevel) { case SL_HUMANS: - if (is_ai(pplayer) && plrdata->name == NULL) { + if (is_ai(pplayer) && plrdata->name == nullptr) { /* If a human player toggled into AI mode, don't break. */ break; } -- 2.42.0