7 #ifndef ANONCOIN_UTIL_H
8 #define ANONCOIN_UTIL_H
10 #if defined(HAVE_CONFIG_H)
28 #include <sys/resource.h>
30 #include <sys/types.h>
33 #include <boost/filesystem/path.hpp>
34 #include <boost/thread.hpp>
39 static const int64_t COIN = 100000000;
40 static const int64_t CENT = 1000000;
42 #define BEGIN(a) ((char*)&(a))
43 #define END(a) ((char*)&((&(a))[1]))
44 #define UBEGIN(a) ((unsigned char*)&(a))
45 #define UEND(a) ((unsigned char*)&((&(a))[1]))
46 #define ARRAYLEN(array) (sizeof(array)/sizeof((array)[0]))
49 #define PAIRTYPE(t1, t2) std::pair<t1, t2>
52 template <
size_t nBytes,
typename T>
61 u.n = (u.n + (nBytes-1)) & ~(nBytes-1);
66 #define MSG_DONTWAIT 0
76 #if !defined(HAVE_MSG_NOSIGNAL) && !defined(MSG_NOSIGNAL)
77 #define MSG_NOSIGNAL 0
85 #if defined(HAVE_WORKING_BOOST_SLEEP_FOR)
86 boost::this_thread::sleep_for(boost::chrono::milliseconds(n));
87 #elif defined(HAVE_WORKING_BOOST_SLEEP)
88 boost::this_thread::sleep(boost::posix_time::milliseconds(n));
91 #error missing boost sleep implementation
97 extern std::map<std::string, std::string>
mapArgs;
98 extern std::map<std::string, std::vector<std::string> >
mapMultiArgs;
117 #define strprintf tfm::format
118 #define LogPrintf(...) LogPrint(NULL, __VA_ARGS__)
123 #define MAKE_ERROR_AND_LOG_FUNC(n) \
125 template<TINYFORMAT_ARGTYPES(n)> \
126 static inline int LogPrint(const char* category, const char* format, TINYFORMAT_VARARGS(n)) \
128 if(!LogAcceptCategory(category)) return 0; \
129 return LogPrintStr(tfm::format(format, TINYFORMAT_PASSARGS(n))); \
132 template<TINYFORMAT_ARGTYPES(n)> \
133 static inline bool error(const char* format, TINYFORMAT_VARARGS(n)) \
135 LogPrintStr("ERROR: " + tfm::format(format, TINYFORMAT_PASSARGS(n)) + "\n"); \
144 static inline
int LogPrint(const
char* category, const
char*
format)
149 static inline bool error(
const char*
format)
151 LogPrintStr(std::string(
"ERROR: ") + format +
"\n");
156 void LogException(std::exception* pex,
const char* pszThread);
158 std::string
FormatMoney(int64_t n,
bool fPlus=
false);
159 bool ParseMoney(
const std::string& str, int64_t& nRet);
160 bool ParseMoney(
const char* pszIn, int64_t& nRet);
162 std::vector<unsigned char>
ParseHex(
const char* psz);
163 std::vector<unsigned char>
ParseHex(
const std::string& str);
164 bool IsHex(
const std::string& str);
165 std::vector<unsigned char>
DecodeBase64(
const char* p,
bool* pfInvalid = NULL);
167 std::string
EncodeBase64(
const unsigned char* pch,
size_t len);
169 std::vector<unsigned char>
DecodeBase32(
const char* p,
bool* pfInvalid = NULL);
171 std::string
EncodeBase32(
const unsigned char* pch,
size_t len);
175 bool WildcardMatch(
const std::string& str,
const std::string& mask);
180 bool RenameOver(boost::filesystem::path src, boost::filesystem::path dest);
183 const boost::filesystem::path &
GetDataDir(
bool fNetSpecific =
true);
188 void CreatePidFile(
const boost::filesystem::path &path, pid_t pid);
190 void ReadConfigFile(std::map<std::string, std::string>& mapSettingsRet, std::map<std::string, std::vector<std::string> >& mapMultiSettingsRet);
192 boost::filesystem::path GetSpecialFolderPath(
int nFolder,
bool fCreate =
true);
197 uint64_t
GetRand(uint64_t nMax);
222 return strtoll(psz, NULL, 10);
226 inline int64_t
atoi64(
const std::string& str)
229 return _atoi64(str.c_str());
231 return strtoll(str.c_str(), NULL, 10);
235 inline int atoi(
const std::string& str)
237 return atoi(str.c_str());
242 return (
int)(d > 0 ? d + 0.5 : d - 0.5);
247 return (int64_t)(d > 0 ? d + 0.5 : d - 0.5);
252 return (n >= 0 ? n : -n);
256 std::string
HexStr(
const T itbegin,
const T itend,
bool fSpaces=
false)
259 static const char hexmap[16] = {
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
260 '8',
'9',
'a',
'b',
'c',
'd',
'e',
'f' };
261 rv.reserve((itend-itbegin)*3);
262 for(T it = itbegin; it < itend; ++it)
264 unsigned char val = (
unsigned char)(*it);
265 if(fSpaces && it != itbegin)
267 rv.push_back(hexmap[val>>4]);
268 rv.push_back(hexmap[val&15]);
275 inline std::string
HexStr(
const T& vch,
bool fSpaces=
false)
277 return HexStr(vch.begin(), vch.end(), fSpaces);
281 void PrintHex(
const T pbegin,
const T pend,
const char* pszFormat=
"%s",
bool fSpaces=
true)
286 inline void PrintHex(
const std::vector<unsigned char>& vch,
const char* pszFormat=
"%s",
bool fSpaces=
true)
293 int64_t nCounter = 0;
295 QueryPerformanceCounter((LARGE_INTEGER*)&nCounter);
298 gettimeofday(&t, NULL);
299 nCounter = (int64_t) t.tv_sec * 1000000 + t.tv_usec;
306 return (boost::posix_time::ptime(boost::posix_time::microsec_clock::universal_time()) -
307 boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_milliseconds();
312 return (boost::posix_time::ptime(boost::posix_time::microsec_clock::universal_time()) -
313 boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_microseconds();
328 return c ==
'-' || c ==
'/';
341 std::string
GetArg(
const std::string& strArg,
const std::string& strDefault);
350 int64_t
GetArg(
const std::string& strArg, int64_t nDefault);
359 bool GetBoolArg(
const std::string& strArg,
bool fDefault);
368 bool SoftSetArg(
const std::string& strArg,
const std::string& strValue);
388 static inline uint32_t insecure_rand(
void)
390 insecure_rand_Rz = 36969 * (insecure_rand_Rz & 65535) + (insecure_rand_Rz >> 16);
391 insecure_rand_Rw = 18000 * (insecure_rand_Rw & 65535) + (insecure_rand_Rw >> 16);
406 template <
typename T>
409 if (b.size() == 0)
return a.size() == 0;
410 size_t accumulator = a.size() ^ b.size();
411 for (
size_t i = 0; i < a.size(); i++)
412 accumulator |= a[i] ^ b[i%b.size()];
413 return accumulator == 0;
429 vValues.reserve(size);
430 vValues.push_back(initial_value);
436 if(vValues.size() ==
nSize)
438 vValues.erase(vValues.begin());
440 vValues.push_back(value);
442 vSorted.resize(vValues.size());
443 std::copy(vValues.begin(), vValues.end(), vSorted.begin());
444 std::sort(vSorted.begin(), vSorted.end());
449 int size = vSorted.size();
453 return vSorted[size/2];
457 return (vSorted[size/2-1] + vSorted[size/2]) / 2;
463 return vValues.size();
483 #define THREAD_PRIORITY_LOWEST PRIO_MAX
484 #define THREAD_PRIORITY_BELOW_NORMAL 2
485 #define THREAD_PRIORITY_NORMAL 0
486 #define THREAD_PRIORITY_ABOVE_NORMAL (-2)
493 setpriority(PRIO_THREAD, 0, nPriority);
495 setpriority(PRIO_PROCESS, 0, nPriority);
504 value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8);
505 return (value<<16) | (value>>16);
515 template <
typename Callable>
void LoopForever(
const char* name, Callable func, int64_t msecs)
517 std::string s =
strprintf(
"anoncoin-%s", name);
528 catch (boost::thread_interrupted)
533 catch (std::exception& e) {
543 template <
typename Callable>
void TraceThread(
const char* name, Callable func)
545 std::string s =
strprintf(
"anoncoin-%s", name);
553 catch (boost::thread_interrupted)
555 LogPrintf(
"%s thread interrupt\n", name);
558 catch (std::exception& e) {
uint64_t GetRand(uint64_t nMax)
void PrintHex(const T pbegin, const T pend, const char *pszFormat="%s", bool fSpaces=true)
void MilliSleep(int64_t n)
void LogException(std::exception *pex, const char *pszThread)
std::vector< unsigned char > DecodeBase32(const char *p, bool *pfInvalid=NULL)
void SetMockTime(int64_t nMockTimeIn)
int64_t GetAdjustedTime()
void RandAddSeedPerfmon()
std::string i64tostr(int64_t n)
void RenameThread(const char *name)
void ReadConfigFile(std::map< std::string, std::string > &mapSettingsRet, std::map< std::string, std::vector< std::string > > &mapMultiSettingsRet)
std::string SanitizeString(const std::string &str)
std::map< std::string, std::vector< std::string > > mapMultiArgs
void CreatePidFile(const boost::filesystem::path &path, pid_t pid)
std::map< std::string, std::string > mapArgs
bool SoftSetArg(const std::string &strArg, const std::string &strValue)
Set an argument if it doesn't already have a value.
std::vector< unsigned char > ParseHex(const char *psz)
const boost::filesystem::path & GetDataDir(bool fNetSpecific=true)
std::string itostr(int n)
bool SoftSetBoolArg(const std::string &strArg, bool fValue)
Set a boolean argument if it doesn't already have a value.
int64_t atoi64(const char *psz)
void TraceThread(const char *name, Callable func)
bool ParseMoney(const std::string &str, int64_t &nRet)
void ParseParameters(int argc, const char *const argv[])
bool IsHex(const std::string &str)
std::string EncodeBase64(const unsigned char *pch, size_t len)
#define MAKE_ERROR_AND_LOG_FUNC(n)
void SetThreadPriority(int nPriority)
void PrintExceptionContinue(std::exception *pex, const char *pszThread)
std::vector< unsigned char > DecodeBase64(const char *p, bool *pfInvalid=NULL)
IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96))
std::string EncodeBase32(const unsigned char *pch, size_t len)
int64_t GetPerformanceCounter()
std::string strMiscWarning
bool IsSwitchChar(char c)
uint32_t insecure_rand_Rz
MWC RNG of George Marsaglia This is intended to be fast.
std::string FormatMoney(int64_t n, bool fPlus=false)
bool TryCreateDirectory(const boost::filesystem::path &p)
bool RenameOver(boost::filesystem::path src, boost::filesystem::path dest)
uint32_t ByteReverse(uint32_t value)
void FileCommit(FILE *fileout)
boost::filesystem::path GetConfigFile()
void seed_insecure_rand(bool fDeterministic=false)
Seed insecure_rand using the random pool.
boost::filesystem::path GetTempPath()
std::string GetArg(const std::string &strArg, const std::string &strDefault)
Return string argument or default value.
bool TimingResistantEqual(const T &a, const T &b)
Timing-attack-resistant comparison.
bool WildcardMatch(const char *psz, const char *mask)
bool LogAcceptCategory(const char *category)
int RaiseFileDescriptorLimit(int nMinFD)
int64_t roundint64(double d)
boost::filesystem::path GetPidFile()
void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length)
bool TruncateFile(FILE *file, unsigned int length)
std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
bool GetBoolArg(const std::string &strArg, bool fDefault)
Return boolean argument or default value.
int LogPrintStr(const std::string &str)
volatile bool fReopenDebugLog
void LoopForever(const char *name, Callable func, int64_t msecs)
std::string DateTimeStrFormat(const char *pszFormat, int64_t nTime)
boost::filesystem::path GetQtStyleFile()
boost::filesystem::path GetDefaultDataDir()
uint32_t insecure_rand_Rw
int atoi(const std::string &str)
void runCommand(std::string strCommand)
void AddTimeData(const CNetAddr &ip, int64_t nTime)