2014-08-28 22:26:56 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2015-12-13 14:51:43 +01:00
|
|
|
// Copyright (c) 2009-2015 The Bitcoin Core developers
|
2014-11-17 04:04:01 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2014-08-28 22:26:56 +02:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2021-06-27 08:33:13 +02:00
|
|
|
#include <util/moneystr.h>
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
|
2022-12-20 17:14:58 +01:00
|
|
|
#include <amount.h>
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <tinyformat.h>
|
2021-06-27 08:33:13 +02:00
|
|
|
#include <util/strencodings.h>
|
2019-12-16 16:15:51 +01:00
|
|
|
#include <util/string.h>
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
|
2022-12-20 17:14:58 +01:00
|
|
|
#include <optional>
|
|
|
|
|
2015-06-04 14:43:02 +02:00
|
|
|
std::string FormatMoney(const CAmount& n)
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
{
|
|
|
|
// Note: not using straight sprintf here because we do NOT want
|
|
|
|
// localized number formatting.
|
|
|
|
int64_t n_abs = (n > 0 ? n : -n);
|
|
|
|
int64_t quotient = n_abs/COIN;
|
|
|
|
int64_t remainder = n_abs%COIN;
|
2017-03-09 08:10:09 +01:00
|
|
|
std::string str = strprintf("%d.%08d", quotient, remainder);
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
|
|
|
|
// Right-trim excess zeros before the decimal point:
|
|
|
|
int nTrim = 0;
|
2019-01-09 18:10:22 +01:00
|
|
|
for (int i = str.size()-1; (str[i] == '0' && IsDigit(str[i-2])); --i)
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
++nTrim;
|
|
|
|
if (nTrim)
|
|
|
|
str.erase(str.size()-nTrim, nTrim);
|
|
|
|
|
|
|
|
if (n < 0)
|
|
|
|
str.insert((unsigned int)0, 1, '-');
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-20 17:14:58 +01:00
|
|
|
std::optional<CAmount> ParseMoney(const std::string& money_string)
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
{
|
2020-03-05 21:20:36 +01:00
|
|
|
if (!ValidAsCString(money_string)) {
|
2022-12-20 17:14:58 +01:00
|
|
|
return std::nullopt;
|
2019-12-16 16:15:51 +01:00
|
|
|
}
|
2020-03-05 21:20:36 +01:00
|
|
|
const std::string str = TrimString(money_string);
|
2020-02-28 18:27:48 +01:00
|
|
|
if (str.empty()) {
|
2022-12-20 17:14:58 +01:00
|
|
|
return std::nullopt;
|
2020-02-28 18:27:48 +01:00
|
|
|
}
|
|
|
|
|
2017-03-09 08:10:09 +01:00
|
|
|
std::string strWhole;
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
int64_t nUnits = 0;
|
2020-02-28 18:27:48 +01:00
|
|
|
const char* p = str.c_str();
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
for (; *p; p++)
|
|
|
|
{
|
|
|
|
if (*p == '.')
|
|
|
|
{
|
|
|
|
p++;
|
2018-09-27 18:01:47 +02:00
|
|
|
int64_t nMult = COIN / 10;
|
2019-01-09 18:10:22 +01:00
|
|
|
while (IsDigit(*p) && (nMult > 0))
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
{
|
|
|
|
nUnits += nMult * (*p++ - '0');
|
|
|
|
nMult /= 10;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2018-10-28 11:48:46 +01:00
|
|
|
if (IsSpace(*p))
|
2022-12-20 17:14:58 +01:00
|
|
|
return std::nullopt;
|
2019-01-09 18:10:22 +01:00
|
|
|
if (!IsDigit(*p))
|
2022-12-20 17:14:58 +01:00
|
|
|
return std::nullopt;
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
strWhole.insert(strWhole.end(), *p);
|
|
|
|
}
|
2020-03-05 21:20:36 +01:00
|
|
|
if (*p) {
|
2022-12-20 17:14:58 +01:00
|
|
|
return std::nullopt;
|
2020-03-05 21:20:36 +01:00
|
|
|
}
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
if (strWhole.size() > 10) // guard against 63 bit overflow
|
2022-12-20 17:14:58 +01:00
|
|
|
return std::nullopt;
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
if (nUnits < 0 || nUnits > COIN)
|
2022-12-20 17:14:58 +01:00
|
|
|
return std::nullopt;
|
2022-12-20 17:18:10 +01:00
|
|
|
int64_t nWhole = LocaleIndependentAtoi<int64_t>(strWhole);
|
2022-12-20 17:14:58 +01:00
|
|
|
CAmount value = nWhole * COIN + nUnits;
|
|
|
|
|
|
|
|
if (!MoneyRange(value)) {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
}
|