2014-08-28 22:26:56 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
|
|
|
// Copyright (c) 2009-2014 The Bitcoin 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.
|
|
|
|
|
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
|
|
|
#include "utilmoneystr.h"
|
|
|
|
|
2014-10-18 02:34:06 +02:00
|
|
|
#include "core/transaction.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
|
|
|
#include "tinyformat.h"
|
2014-09-25 05:32:36 +02:00
|
|
|
#include "utilstrencodings.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
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2014-04-23 00:46:19 +02:00
|
|
|
string FormatMoney(const CAmount& n, bool fPlus)
|
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;
|
|
|
|
string str = strprintf("%d.%08d", quotient, remainder);
|
|
|
|
|
|
|
|
// Right-trim excess zeros before the decimal point:
|
|
|
|
int nTrim = 0;
|
|
|
|
for (int i = str.size()-1; (str[i] == '0' && isdigit(str[i-2])); --i)
|
|
|
|
++nTrim;
|
|
|
|
if (nTrim)
|
|
|
|
str.erase(str.size()-nTrim, nTrim);
|
|
|
|
|
|
|
|
if (n < 0)
|
|
|
|
str.insert((unsigned int)0, 1, '-');
|
|
|
|
else if (fPlus && n > 0)
|
|
|
|
str.insert((unsigned int)0, 1, '+');
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-23 00:46:19 +02:00
|
|
|
bool ParseMoney(const string& str, CAmount& nRet)
|
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
|
|
|
{
|
|
|
|
return ParseMoney(str.c_str(), nRet);
|
|
|
|
}
|
|
|
|
|
2014-04-23 00:46:19 +02:00
|
|
|
bool ParseMoney(const char* pszIn, CAmount& nRet)
|
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
|
|
|
{
|
|
|
|
string strWhole;
|
|
|
|
int64_t nUnits = 0;
|
|
|
|
const char* p = pszIn;
|
|
|
|
while (isspace(*p))
|
|
|
|
p++;
|
|
|
|
for (; *p; p++)
|
|
|
|
{
|
|
|
|
if (*p == '.')
|
|
|
|
{
|
|
|
|
p++;
|
|
|
|
int64_t nMult = CENT*10;
|
|
|
|
while (isdigit(*p) && (nMult > 0))
|
|
|
|
{
|
|
|
|
nUnits += nMult * (*p++ - '0');
|
|
|
|
nMult /= 10;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (isspace(*p))
|
|
|
|
break;
|
|
|
|
if (!isdigit(*p))
|
|
|
|
return false;
|
|
|
|
strWhole.insert(strWhole.end(), *p);
|
|
|
|
}
|
|
|
|
for (; *p; p++)
|
|
|
|
if (!isspace(*p))
|
|
|
|
return false;
|
|
|
|
if (strWhole.size() > 10) // guard against 63 bit overflow
|
|
|
|
return false;
|
|
|
|
if (nUnits < 0 || nUnits > COIN)
|
|
|
|
return false;
|
|
|
|
int64_t nWhole = atoi64(strWhole);
|
2014-04-23 00:46:19 +02:00
|
|
|
CAmount nValue = nWhole*COIN + nUnits;
|
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
|
|
|
|
|
|
|
nRet = nValue;
|
|
|
|
return true;
|
|
|
|
}
|