Monopoly — Portage moderne 0.1.0
Portage moderne C/C++23 du Monopoly de 1999
Loading...
Searching...
No Matches
MoneyFormat.cpp
Go to the documentation of this file.
1#include "MoneyFormat.hpp"
2
3#include <limits>
4#include <string_view>
5
7{
8 namespace
9 {
10 [[nodiscard]] std::expected<std::int64_t, std::string> scaled(
11 std::int64_t value, std::int64_t factor)
12 {
13 if (factor <= 0)
14 return std::unexpected("money scale factor is invalid");
15 if (value > 0 && value > std::numeric_limits<std::int64_t>::max() / factor)
16 return std::unexpected("money value overflows while applying retail currency scale");
17 if (value < 0 && value < std::numeric_limits<std::int64_t>::min() / factor)
18 return std::unexpected("money value underflows while applying retail currency scale");
19 return value * factor;
20 }
21
22 [[nodiscard]] std::string decorated(
23 std::int64_t value, bool printSymbol,
24 std::string_view prefix, std::string_view suffix = {})
25 {
26 std::string result;
27 if (printSymbol) result.append(prefix);
28 result += std::to_string(value);
29 if (printSymbol) result.append(suffix);
30 return result;
31 }
32 }
33
34 std::expected<std::string, std::string> format(
35 std::int64_t defaultValue,
36 int monetarySystem,
37 bool printSymbol,
38 data::BoardEdition edition)
39 {
40 // Udpenny.cpp is compiled with USA_VERSION for the USA edition and
41 // unconditionally replaces the selected monetary system with US dollars.
42 if (edition == data::BoardEdition::Usa)
43 monetarySystem = 13;
44
45 std::int64_t factor = 1;
46 std::string_view prefix;
47 std::string_view suffix;
48 switch (monetarySystem)
49 {
50 case 0: prefix = "\xC2\xA3 "; break; // UK pound
51 case 1: factor = 100; prefix = "F "; break; // French franc
52 case 2: factor = 20; prefix = "DM "; break; // Deutsche Mark
53 case 3: factor = 100; break; // Spanish peseta: no symbol
54 case 4: factor = 100; prefix = "f "; break; // Dutch guilder
55 case 5: factor = 20; prefix = "KR "; break; // Swedish krona
56 case 6: factor = 20; suffix = " MK"; break; // Finnish markka
57 case 7: factor = 20; suffix = " KR."; break; // Danish krone
58 case 8: factor = 20; prefix = "KR. "; break; // Norwegian krone
59 case 9: factor = 20; prefix = "F "; break; // Belgian franc
60 case 10: factor = 10; prefix = "$ "; break; // Singapore dollar
61 case 11: prefix = "$ "; break; // Australian dollar
62 case 12: prefix = "\xE2\x82\xAC "; break; // Euro
63 case 13: prefix = "$ "; break; // US dollar
64 default:
65 return std::unexpected("monetary system is outside retail 0..13 range");
66 }
67
68 const auto value = scaled(defaultValue, factor);
69 if (!value) return std::unexpected(value.error());
71 }
72}
unsigned index
std::uint16_t value
Definition Timers.cpp:16
SDL_GPUShaderFormat format
const char * suffix