Changeset View
Changeset View
Standalone View
Standalone View
src/util/moneystr.cpp
Show All 25 Lines | std::string FormatMoney(const Amount amt) { | ||||
} | } | ||||
if (amt < Amount::zero()) { | if (amt < Amount::zero()) { | ||||
str.insert((unsigned int)0, 1, '-'); | str.insert((unsigned int)0, 1, '-'); | ||||
} | } | ||||
return str; | return str; | ||||
} | } | ||||
bool ParseMoney(const std::string &str, Amount &nRet) { | bool ParseMoney(const std::string &money_string, Amount &nRet) { | ||||
if (!ValidAsCString(str)) { | if (!ValidAsCString(money_string)) { | ||||
return false; | return false; | ||||
} | } | ||||
const std::string str = TrimString(money_string); | |||||
if (str.empty()) { | if (str.empty()) { | ||||
return false; | return false; | ||||
} | } | ||||
std::string strWhole; | std::string strWhole; | ||||
Amount nUnits = Amount::zero(); | Amount nUnits = Amount::zero(); | ||||
const char *p = str.c_str(); | const char *p = str.c_str(); | ||||
while (IsSpace(*p)) { | |||||
p++; | |||||
} | |||||
for (; *p; p++) { | for (; *p; p++) { | ||||
if (*p == '.') { | if (*p == '.') { | ||||
p++; | p++; | ||||
Amount nMult = COIN / 10; | Amount nMult = COIN / 10; | ||||
while (IsDigit(*p) && (nMult > Amount::zero())) { | while (IsDigit(*p) && (nMult > Amount::zero())) { | ||||
nUnits += (*p++ - '0') * nMult; | nUnits += (*p++ - '0') * nMult; | ||||
nMult /= 10; | nMult /= 10; | ||||
} | } | ||||
break; | break; | ||||
} | } | ||||
if (IsSpace(*p)) { | if (IsSpace(*p)) { | ||||
break; | return false; | ||||
} | } | ||||
if (!IsDigit(*p)) { | if (!IsDigit(*p)) { | ||||
return false; | return false; | ||||
} | } | ||||
strWhole.insert(strWhole.end(), *p); | strWhole.insert(strWhole.end(), *p); | ||||
} | } | ||||
for (; *p; p++) { | if (*p) { | ||||
if (!IsSpace(*p)) { | |||||
return false; | return false; | ||||
} | } | ||||
} | |||||
// guard against 63 bit overflow | // guard against 63 bit overflow | ||||
if (strWhole.size() > 10) { | if (strWhole.size() > 10) { | ||||
return false; | return false; | ||||
} | } | ||||
if (nUnits < Amount::zero() || nUnits > COIN) { | if (nUnits < Amount::zero() || nUnits > COIN) { | ||||
return false; | return false; | ||||
} | } | ||||
Amount nWhole = atoi64(strWhole) * COIN; | Amount nWhole = atoi64(strWhole) * COIN; | ||||
nRet = nWhole + Amount(nUnits); | nRet = nWhole + Amount(nUnits); | ||||
return true; | return true; | ||||
} | } |