Mercurial > hg > bitcoin
changeset 1217:5a796ad1abd7 draft
refuse to format nor parse invalid units
author | Wladimir J. van der Laan <laanwj@gmail.com> |
---|---|
date | Tue, 26 Jul 2011 13:11:28 +0200 |
parents | dba16f926a98 |
children | edec278463ac |
files | src/qt/bitcoinunits.cpp src/qt/bitcoinunits.h |
diffstat | 2 files changed, 20 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/src/qt/bitcoinunits.cpp +++ b/src/qt/bitcoinunits.cpp @@ -17,6 +17,19 @@ return unitlist; } +bool BitcoinUnits::valid(int unit) +{ + switch(unit) + { + case BTC: + case mBTC: + case uBTC: + return true; + default: + return false; + } +} + QString BitcoinUnits::name(int unit) { switch(unit) @@ -54,7 +67,7 @@ { switch(unit) { - case BTC: return 8; // 21,000,000 + case BTC: return 8; // 21,000,000 (# digits, without commas) case mBTC: return 11; // 21,000,000,000 case uBTC: return 14; // 21,000,000,000,000 default: return 0; @@ -76,6 +89,8 @@ { // Note: not using straight sprintf here because we do NOT want // localized number formatting. + if(!valid(unit)) + return QString(); // Refuse to format invalid unit qint64 coin = factor(unit); int num_decimals = decimals(unit); qint64 n_abs = (n > 0 ? n : -n); @@ -104,6 +119,8 @@ bool BitcoinUnits::parse(int unit, const QString &value, qint64 *val_out) { + if(!valid(unit)) + return false; // Refuse to parse invalid unit int num_decimals = decimals(unit); QStringList parts = value.split("."); if(parts.size() != 2 || parts.at(1).size() > num_decimals)