X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=src%2Futil.h;h=e0821cce19bbe3b72b01eddfdb11f4ec1253b8e4;hb=d11488abd05cb39a9f481e7c4c35f780197a3d28;hp=c57e82bab030599c35149eba45468c112e51bc3f;hpb=b0870346f268d4c7f2d461c921ff63b6d967242d;p=novacoin.git diff --git a/src/util.h b/src/util.h index c57e82b..e0821cc 100644 --- a/src/util.h +++ b/src/util.h @@ -1,7 +1,7 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2011 The Bitcoin developers +// Copyright (c) 2009-2012 The Bitcoin developers // Distributed under the MIT/X11 software license, see the accompanying -// file license.txt or http://www.opensource.org/licenses/mit-license.php. +// file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifndef BITCOIN_UTIL_H #define BITCOIN_UTIL_H @@ -24,20 +24,10 @@ #include #include +#include "netbase.h" -#if defined(_MSC_VER) || defined(__BORLANDC__) -typedef __int64 int64; -typedef unsigned __int64 uint64; -#else typedef long long int64; typedef unsigned long long uint64; -#endif -#if defined(_MSC_VER) && _MSC_VER < 1300 -#define for if (false) ; else for -#endif -#ifndef _MSC_VER -#define __forceinline inline -#endif #define loop for (;;) #define BEGIN(a) ((char*)&(a)) @@ -53,7 +43,7 @@ typedef unsigned long long uint64; #define snprintf my_snprintf #ifndef PRI64d -#if defined(_MSC_VER) || defined(__BORLANDC__) || defined(__MSVCRT__) +#if defined(_MSC_VER) || defined(__MSVCRT__) #define PRI64d "I64d" #define PRI64u "I64u" #define PRI64x "I64x" @@ -84,54 +74,25 @@ T* alignup(T* p) #ifdef WIN32 #define MSG_NOSIGNAL 0 #define MSG_DONTWAIT 0 -#ifndef UINT64_MAX -#define UINT64_MAX _UI64_MAX -#define INT64_MAX _I64_MAX -#define INT64_MIN _I64_MIN -#endif + #ifndef S_IRUSR #define S_IRUSR 0400 #define S_IWUSR 0200 #endif #define unlink _unlink -typedef int socklen_t; #else -#define WSAGetLastError() errno -#define WSAEINVAL EINVAL -#define WSAEALREADY EALREADY -#define WSAEWOULDBLOCK EWOULDBLOCK -#define WSAEMSGSIZE EMSGSIZE -#define WSAEINTR EINTR -#define WSAEINPROGRESS EINPROGRESS -#define WSAEADDRINUSE EADDRINUSE -#define WSAENOTSOCK EBADF -#define INVALID_SOCKET (SOCKET)(~0) -#define SOCKET_ERROR -1 -typedef u_int SOCKET; #define _vsnprintf(a,b,c,d) vsnprintf(a,b,c,d) #define strlwr(psz) to_lower(psz) #define _strlwr(psz) to_lower(psz) #define MAX_PATH 1024 -#define Beep(n1,n2) (0) inline void Sleep(int64 n) { - boost::thread::sleep(boost::get_system_time() + boost::posix_time::milliseconds(n)); + /*Boost has a year 2038 problem— if the request sleep time is past epoch+2^31 seconds the sleep returns instantly. + So we clamp our sleeps here to 10 years and hope that boost is fixed by 2028.*/ + boost::thread::sleep(boost::get_system_time() + boost::posix_time::milliseconds(n>315576000000LL?315576000000LL:n)); } #endif -inline int myclosesocket(SOCKET& hSocket) -{ - if (hSocket == INVALID_SOCKET) - return WSAENOTSOCK; -#ifdef WIN32 - int ret = closesocket(hSocket); -#else - int ret = close(hSocket); -#endif - hSocket = INVALID_SOCKET; - return ret; -} -#define closesocket(s) myclosesocket(s) #if !defined(QT_GUI) inline const char* _(const char* psz) { @@ -178,12 +139,12 @@ bool ParseMoney(const std::string& str, int64& nRet); bool ParseMoney(const char* pszIn, int64& nRet); std::vector ParseHex(const char* psz); std::vector ParseHex(const std::string& str); +bool IsHex(const std::string& str); std::vector DecodeBase64(const char* p, bool* pfInvalid = NULL); std::string DecodeBase64(const std::string& str); std::string EncodeBase64(const unsigned char* pch, size_t len); std::string EncodeBase64(const std::string& str); -void ParseParameters(int argc, char* argv[]); -const char* wxGetTranslation(const char* psz); +void ParseParameters(int argc, const char*const argv[]); bool WildcardMatch(const char* psz, const char* mask); bool WildcardMatch(const std::string& str, const std::string& mask); int GetFilesize(FILE* file); @@ -191,7 +152,7 @@ void GetDataDir(char* pszDirRet); std::string GetConfigFile(); std::string GetPidFile(); void CreatePidFile(std::string pidFile, pid_t pid); -void ReadConfigFile(std::map& mapSettingsRet, std::map >& mapMultiSettingsRet); +bool ReadConfigFile(std::map& mapSettingsRet, std::map >& mapMultiSettingsRet); #ifdef WIN32 std::string MyGetSpecialFolderPath(int nFolder, bool fCreate); #endif @@ -203,8 +164,9 @@ uint64 GetRand(uint64 nMax); int64 GetTime(); void SetMockTime(int64 nMockTimeIn); int64 GetAdjustedTime(); -void AddTimeData(unsigned int ip, int64 nTime); std::string FormatFullVersion(); +std::string FormatSubVersion(const std::string& name, int nClientVersion, const std::vector& comments); +void AddTimeData(const CNetAddr& ip, int64 nTime); @@ -217,8 +179,7 @@ std::string FormatFullVersion(); - -// Wrapper to automatically initialize mutex +/** Wrapper to automatically initialize mutex. */ class CCriticalSection { protected: @@ -231,7 +192,7 @@ public: bool TryEnter(const char* pszName, const char* pszFile, int nLine); }; -// Automatically leave critical section when leaving block, needed for exception safety +/** RAII object that acquires mutex. Needed for exception safety. */ class CCriticalBlock { protected: @@ -243,20 +204,28 @@ public: pcs = &csIn; pcs->Enter(pszName, pszFile, nLine); } + + operator bool() const + { + return true; + } + ~CCriticalBlock() { pcs->Leave(); } }; -// WARNING: This will catch continue and break! -// break is caught with an assertion, but there's no way to detect continue. -// I'd rather be careful than suffer the other more error prone syntax. -// The compiler will optimise away all this loop junk. #define CRITICAL_BLOCK(cs) \ - for (bool fcriticalblockonce=true; fcriticalblockonce; assert(("break caught by CRITICAL_BLOCK!" && !fcriticalblockonce)), fcriticalblockonce=false) \ - for (CCriticalBlock criticalblock(cs, #cs, __FILE__, __LINE__); fcriticalblockonce; fcriticalblockonce=false) + if (CCriticalBlock criticalblock = CCriticalBlock(cs, #cs, __FILE__, __LINE__)) + +#define ENTER_CRITICAL_SECTION(cs) \ + (cs).Enter(#cs, __FILE__, __LINE__) +#define LEAVE_CRITICAL_SECTION(cs) \ + (cs).Leave() + +/** RAII object that tries to acquire mutex. Needed for exception safety. */ class CTryCriticalBlock { protected: @@ -267,6 +236,12 @@ public: { pcs = (csIn.TryEnter(pszName, pszFile, nLine) ? &csIn : NULL); } + + operator bool() const + { + return Entered(); + } + ~CTryCriticalBlock() { if (pcs) @@ -274,12 +249,11 @@ public: pcs->Leave(); } } - bool Entered() { return pcs != NULL; } + bool Entered() const { return pcs != NULL; } }; #define TRY_CRITICAL_BLOCK(cs) \ - for (bool fcriticalblockonce=true; fcriticalblockonce; assert(("break caught by TRY_CRITICAL_BLOCK!" && !fcriticalblockonce)), fcriticalblockonce=false) \ - for (CTryCriticalBlock criticalblock(cs, #cs, __FILE__, __LINE__); fcriticalblockonce && (fcriticalblockonce = criticalblock.Entered()); fcriticalblockonce=false) + if (CTryCriticalBlock criticalblock = CTryCriticalBlock(cs, #cs, __FILE__, __LINE__)) @@ -362,25 +336,6 @@ inline std::string HexStr(const std::vector& vch, bool fSpaces=fa } template -std::string HexNumStr(const T itbegin, const T itend, bool f0x=true) -{ - if (itbegin == itend) - return ""; - const unsigned char* pbegin = (const unsigned char*)&itbegin[0]; - const unsigned char* pend = pbegin + (itend - itbegin) * sizeof(itbegin[0]); - std::string str = (f0x ? "0x" : ""); - str.reserve(str.size() + (pend-pbegin) * 2); - for (const unsigned char* p = pend-1; p >= pbegin; p--) - str += strprintf("%02x", *p); - return str; -} - -inline std::string HexNumStr(const std::vector& vch, bool f0x=true) -{ - return HexNumStr(vch.begin(), vch.end(), f0x); -} - -template void PrintHex(const T pbegin, const T pend, const char* pszFormat="%s", bool fSpaces=true) { printf(pszFormat, HexStr(pbegin, pend, fSpaces).c_str()); @@ -435,30 +390,32 @@ inline bool IsSwitchChar(char c) #endif } -inline std::string GetArg(const std::string& strArg, const std::string& strDefault) -{ - if (mapArgs.count(strArg)) - return mapArgs[strArg]; - return strDefault; -} +/** + * Return string argument or default value + * + * @param strArg Argument to get (e.g. "-foo") + * @param default (e.g. "1") + * @return command-line argument or default value + */ +std::string GetArg(const std::string& strArg, const std::string& strDefault); -inline int64 GetArg(const std::string& strArg, int64 nDefault) -{ - if (mapArgs.count(strArg)) - return atoi64(mapArgs[strArg]); - return nDefault; -} +/** + * Return integer argument or default value + * + * @param strArg Argument to get (e.g. "-foo") + * @param default (e.g. 1) + * @return command-line argument (0 if invalid number) or default value + */ +int64 GetArg(const std::string& strArg, int64 nDefault); -inline bool GetBoolArg(const std::string& strArg, bool fDefault=false) -{ - if (mapArgs.count(strArg)) - { - if (mapArgs[strArg].empty()) - return true; - return (atoi(mapArgs[strArg]) != 0); - } - return fDefault; -} +/** + * Return boolean argument or default value + * + * @param strArg Argument to get (e.g. "-foo") + * @param default (true or false) + * @return command-line argument or default value + */ +bool GetBoolArg(const std::string& strArg, bool fDefault=false); /** * Set an argument if it doesn't already have a value @@ -476,7 +433,7 @@ bool SoftSetArg(const std::string& strArg, const std::string& strValue); * @param fValue Value (e.g. false) * @return true if argument gets set, false if it already had a value */ -bool SoftSetArg(const std::string& strArg, bool fValue); +bool SoftSetBoolArg(const std::string& strArg, bool fValue); @@ -486,15 +443,6 @@ bool SoftSetArg(const std::string& strArg, bool fValue); -inline void heapchk() -{ -#ifdef WIN32 - /// for debugging - //if (_heapchk() != _HEAPOK) - // DebugBreak(); -#endif -} - // Randomize the stack to help protect against buffer overrun exploits #define IMPLEMENT_RANDOMIZE_STACK(ThreadFn) \ { \ @@ -508,21 +456,6 @@ inline void heapchk() } \ } -#define CATCH_PRINT_EXCEPTION(pszFn) \ - catch (std::exception& e) { \ - PrintException(&e, (pszFn)); \ - } catch (...) { \ - PrintException(NULL, (pszFn)); \ - } - - - - - - - - - template inline uint256 Hash(const T1 pbegin, const T1 pend) @@ -570,7 +503,7 @@ inline uint256 Hash(const T1 p1begin, const T1 p1end, } template -uint256 SerializeHash(const T& obj, int nType=SER_GETHASH, int nVersion=VERSION) +uint256 SerializeHash(const T& obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION) { // Most of the time is spent allocating and deallocating CDataStream's // buffer. If this ever needs to be optimized further, make a CStaticStream @@ -591,8 +524,9 @@ inline uint160 Hash160(const std::vector& vch) } -// Median filter over a stream of values -// Returns the median of the last N numbers +/** Median filter over a stream of values. + * Returns the median of the last N numbers + */ template class CMedianFilter { private: @@ -634,6 +568,16 @@ public: return (vSorted[size/2-1] + vSorted[size/2]) / 2; } } + + int size() const + { + return vValues.size(); + } + + std::vector sorted () const + { + return vSorted; + } }; @@ -712,11 +656,6 @@ inline void SetThreadPriority(int nPriority) #endif } -inline bool TerminateThread(pthread_t hthread, unsigned int nExitCode) -{ - return (pthread_cancel(hthread) == 0); -} - inline void ExitThread(size_t nExitCode) { pthread_exit((void*)nExitCode); @@ -754,3 +693,4 @@ inline uint32_t ByteReverse(uint32_t value) } #endif +