From 4a89b3dd5c011049b87331c10110212fbb71c283 Mon Sep 17 00:00:00 2001 From: IntelOrca Date: Thu, 7 Aug 2014 21:53:22 +0100 Subject: [PATCH] refactor currency --- src/currency.c | 20 ++++++++++---------- src/currency.h | 36 ++++++++++++++++++++---------------- src/string_ids.c | 26 ++++++++++++++++---------- 3 files changed, 46 insertions(+), 36 deletions(-) diff --git a/src/currency.c b/src/currency.c index aed162a52b..7ef56e22f1 100644 --- a/src/currency.c +++ b/src/currency.c @@ -21,14 +21,14 @@ #include "currency.h" const rct_currency_spec g_currency_specs[CURRENCY_END] = { - {10 , "\xA3" , 1}, //British Pound - {10 , "\x24" , 1}, //US Dollar - {10 , "F" , 0}, //French Franc - {10 , "DM" , 1}, //Deutsche Mark - {1000 , "\xA5" , 1}, //Japanese Yen - {10 , "Pts" , 0}, //Spanish Peseta - {1000 , "L" , 1}, //Italian Lira - {10 , "fl." , 1}, //Dutch Guilder - {10 , "kr." , 0}, //Swedish Krona - {10 , "\xb5" , 1}, //Euro + { 10 , "\xA3" , CURRENCY_PREFIX }, // British Pound + { 10 , "\x24" , CURRENCY_PREFIX }, // US Dollar + { 10 , "F" , CURRENCY_SUFFIX }, // French Franc + { 10 , "DM" , CURRENCY_PREFIX }, // Deutsche Mark + { 1000 , "\xA5" , CURRENCY_PREFIX }, // Japanese Yen + { 10 , "Pts" , CURRENCY_SUFFIX }, // Spanish Peseta + { 1000 , "L" , CURRENCY_PREFIX }, // Italian Lira + { 10 , "fl." , CURRENCY_PREFIX }, // Dutch Guilder + { 10 , "kr." , CURRENCY_SUFFIX }, // Swedish Krona + { 10 , "\xb5" , CURRENCY_PREFIX }, // Euro }; diff --git a/src/currency.h b/src/currency.h index cce9e54b59..2b60f996f4 100644 --- a/src/currency.h +++ b/src/currency.h @@ -23,27 +23,31 @@ // List of currencies typedef enum { - CURRENCY_POUNDS, //British Pound - CURRENCY_DOLLARS, //US Dollar - CURRENCY_FRANC, //French Franc - CURRENCY_DEUTSCHMARK, //Deutsche Mark - CURRENCY_YEN, //Japanese Yen - CURRENCY_PESETA, //Spanish Peseta - CURRENCY_LIRA, //Italian Lira - CURRENCY_GUILDERS, //Dutch Gilder - CURRENCY_KRONA, //Swedish Krona - CURRENCY_EUROS, //Euro + CURRENCY_POUNDS, // British Pound + CURRENCY_DOLLARS, // US Dollar + CURRENCY_FRANC, // French Franc + CURRENCY_DEUTSCHMARK, // Deutsche Mark + CURRENCY_YEN, // Japanese Yen + CURRENCY_PESETA, // Spanish Peseta + CURRENCY_LIRA, // Italian Lira + CURRENCY_GUILDERS, // Dutch Gilder + CURRENCY_KRONA, // Swedish Krona + CURRENCY_EUROS, // Euro - CURRENCY_END //Last item + CURRENCY_END // Last item } CURRENCY_TYPE; +typedef enum { + CURRENCY_PREFIX, + CURRENCY_SUFFIX +} CURRENCY_AFFIX; + // Currency format specification - inspired by OpenTTD typedef struct { - // Rate is relative to 0.1 GBP - int rate; - char symbol[8]; - // 0: symbol is a suffix, 1: symbol is a prefix - int prefix; + // Rate is relative to 0.1 GBP + int rate; + char symbol[8]; + char affix; } rct_currency_spec; // List of currency formats diff --git a/src/string_ids.c b/src/string_ids.c index 6e2f0383c6..667a492bf0 100644 --- a/src/string_ids.c +++ b/src/string_ids.c @@ -1220,7 +1220,9 @@ void format_comma_separated_fixed_2dp(char **dest, int value) void format_currency(char **dest, int value) { - int rate = g_currency_specs[gGeneral_config.currency_format].rate; + rct_currency_spec *currencySpec = &g_currency_specs[gGeneral_config.currency_format]; + + int rate = currencySpec->rate; value *= rate; // Negative sign @@ -1230,18 +1232,19 @@ void format_currency(char **dest, int value) } // Currency symbol - const char *symbol = g_currency_specs[gGeneral_config.currency_format].symbol; + const char *symbol = currencySpec->symbol; + // Prefix - if (g_currency_specs[gGeneral_config.currency_format].prefix) { + if (currencySpec->affix == CURRENCY_PREFIX) { strcpy(*dest, symbol); *dest += strlen(*dest); } // Divide by 100 to get rid of the pennies - format_comma_separated_integer(dest, value/100); + format_comma_separated_integer(dest, value / 100); // Currency symbol suffix - if (!g_currency_specs[gGeneral_config.currency_format].prefix) { + if (currencySpec->affix == CURRENCY_SUFFIX) { strcpy(*dest, symbol); *dest += strlen(*dest); } @@ -1249,7 +1252,9 @@ void format_currency(char **dest, int value) void format_currency_2dp(char **dest, int value) { - int rate = g_currency_specs[gGeneral_config.currency_format].rate; + rct_currency_spec *currencySpec = &g_currency_specs[gGeneral_config.currency_format]; + + int rate = currencySpec->rate; value *= rate; // Negative sign @@ -1259,22 +1264,23 @@ void format_currency_2dp(char **dest, int value) } // Currency symbol - const char *symbol = g_currency_specs[gGeneral_config.currency_format].symbol; + const char *symbol = currencySpec->symbol; + // Prefix - if (g_currency_specs[gGeneral_config.currency_format].prefix) { + if (currencySpec->affix == CURRENCY_PREFIX) { strcpy(*dest, symbol); *dest += strlen(*dest); } // Drop the pennies for "large" currencies if (rate > 10) { - format_comma_separated_integer(dest, value/100); + format_comma_separated_integer(dest, value / 100); } else { format_comma_separated_fixed_2dp(dest, value); } // Currency symbol suffix - if (!g_currency_specs[gGeneral_config.currency_format].prefix) { + if (currencySpec->affix == CURRENCY_SUFFIX) { strcpy(*dest, symbol); *dest += strlen(*dest); }