| 57 | } |
| 58 | |
| 59 | money64 StringToMoney(const char* string_to_monetise) |
| 60 | { |
| 61 | const char* decimal_char = LanguageGetString(STR_LOCALE_DECIMAL_POINT); |
| 62 | const CurrencyDescriptor* currencyDesc = &CurrencyDescriptors[EnumValue(Config::Get().general.currencyFormat)]; |
| 63 | char processedString[128] = {}; |
| 64 | |
| 65 | Guard::Assert(strlen(string_to_monetise) < sizeof(processedString)); |
| 66 | |
| 67 | uint32_t numNumbers = 0; |
| 68 | bool hasMinus = false; |
| 69 | bool hasDecSep = false; |
| 70 | const char* src_ptr = string_to_monetise; |
| 71 | char* dst_ptr = processedString; |
| 72 | |
| 73 | // Process the string, keeping only numbers decimal, and minus sign(s). |
| 74 | while (*src_ptr != '\0') |
| 75 | { |
| 76 | if (*src_ptr >= '0' && *src_ptr <= '9') |
| 77 | { |
| 78 | numNumbers++; |
| 79 | } |
| 80 | else if (*src_ptr == decimal_char[0]) |
| 81 | { |
| 82 | if (hasDecSep) |
| 83 | return kMoney64Undefined; |
| 84 | hasDecSep = true; |
| 85 | |
| 86 | // Replace localised decimal separator with an English one. |
| 87 | *dst_ptr++ = '.'; |
| 88 | src_ptr++; |
| 89 | continue; |
| 90 | } |
| 91 | else if (*src_ptr == '-') |
| 92 | { |
| 93 | if (hasMinus) |
| 94 | return kMoney64Undefined; |
| 95 | hasMinus = true; |
| 96 | } |
| 97 | else |
| 98 | { |
| 99 | // Skip invalid characters. |
| 100 | src_ptr++; |
| 101 | continue; |
| 102 | } |
| 103 | |
| 104 | // Copy numeric values. |
| 105 | *dst_ptr++ = *src_ptr; |
| 106 | src_ptr++; |
| 107 | } |
| 108 | |
| 109 | // Terminate destination string. |
| 110 | *dst_ptr = '\0'; |
| 111 | |
| 112 | if (numNumbers == 0) |
| 113 | return kMoney64Undefined; |
| 114 | |
| 115 | if (hasMinus && processedString[0] != '-') |
| 116 | { |
no test coverage detected