X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=src%2Futil.cpp;h=4be37a20baa1d16bd0ea59ccfe81f55b44120c34;hb=1428a3bdc4f420dd429d6bb6b55055d1867925e4;hp=9fd406caeac5f19ec83cd30cd22e1d794b6d32d0;hpb=05c208408e2ca3c72b3fd836e11dd1e6a549fbc3;p=novacoin.git diff --git a/src/util.cpp b/src/util.cpp index 9fd406c..4be37a2 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -7,26 +7,18 @@ #include "sync.h" #include "version.h" #include "ui_interface.h" -#include -#include // for to_lower() -// Work around clang compilation problem in Boost 1.46: -// /usr/include/boost/program_options/detail/config_file.hpp:163:17: error: call to function 'to_internal' that is neither visible in the template definition nor found by argument-dependent lookup -// See also: http://stackoverflow.com/questions/10020179/compilation-fail-in-boost-librairies-program-options -// http://clang.debian.net/status.php?version=3.0&key=CANNOT_FIND_FUNCTION -namespace boost { - namespace program_options { - string to_internal(const string&); - } -} +#include +#include +#include // for to_lower() #include #include #include #include - #include #include + #include #include @@ -46,14 +38,13 @@ namespace boost { #include /* for _commit */ #include "shlobj.h" #elif defined(__linux__) -# include +#include #endif #if !defined(WIN32) && !defined(ANDROID) #include #endif - using namespace std; namespace bt = boost::posix_time; @@ -196,10 +187,15 @@ int GetRandInt(int nMax) uint256 GetRandHash() { uint256 hash; - RAND_bytes((unsigned char*)&hash, sizeof(hash)); + RAND_bytes(hash.begin(), hash.size()); return hash; } +void FillRand(uint8_t *buffer, size_t nCount) +{ + RAND_bytes(buffer, nCount); +} + static FILE* fileout = NULL; inline int OutputDebugStringF(const char* pszFormat, ...) @@ -430,7 +426,7 @@ bool ParseMoney(const char* pszIn, int64_t& nRet) return false; if (nUnits < 0 || nUnits > COIN) return false; - int64_t nWhole = atoi64(strWhole); + int64_t nWhole = strtoll(strWhole); int64_t nValue = nWhole*COIN + nUnits; nRet = nValue; @@ -558,7 +554,7 @@ string GetArg(const string& strArg, const string& strDefault) int64_t GetArg(const string& strArg, int64_t nDefault) { if (mapArgs.count(strArg)) - return atoi64(mapArgs[strArg]); + return strtoll(mapArgs[strArg]); return nDefault; } @@ -1125,20 +1121,19 @@ const boost::filesystem::path &GetDataDir(bool fNetSpecific) return path; } -string randomStrGen(int length) { - static string charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; +string randomStrGen(size_t length) { + std::mt19937 mtrand; + mtrand.seed(static_cast(time(NULL))); + static const string charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; string result; result.resize(length); - for (int32_t i = 0; i < length; i++) - result[i] = charset[rand() % charset.length()]; - + for (size_t i = 0; i < length; ++i) + result[i] = charset[mtrand() % charset.length()]; return result; } void createConf() { - srand(static_cast(time(NULL))); - ofstream pConf; #if BOOST_FILESYSTEM_VERSION >= 3 pConf.open(GetConfigFile().generic_string().c_str()); @@ -1252,8 +1247,9 @@ void ShrinkDebugFile() // Restart the file with some of the end try { vector* vBuf = new vector (200000, 0); - fseek(file, -((long)(vBuf->size())), SEEK_END); - size_t nBytes = fread(&vBuf->operator[](0), 1, vBuf->size(), file); + size_t nBytes = 1; //write one byte if fseek failed + if (fseek(file, -((long)(vBuf->size())), SEEK_END) == 0) + nBytes = fread(&vBuf->operator[](0), 1, vBuf->size(), file); fclose(file); file = fopen(pathLog.string().c_str(), "w"); if (file) @@ -1291,7 +1287,7 @@ int64_t GetTime() extern int64_t nNtpOffset; // Median of time samples given by other nodes. -static int64_t nNodesOffset = INT64_MAX; +static int64_t nNodesOffset = numeric_limits::max(); // Select time offset: int64_t GetTimeOffset() @@ -1340,7 +1336,7 @@ void AddTimeData(const CNetAddr& ip, int64_t nTime) } else { - nNodesOffset = INT64_MAX; + nNodesOffset = numeric_limits::max(); static bool fDone; if (!fDone) @@ -1367,7 +1363,7 @@ void AddTimeData(const CNetAddr& ip, int64_t nTime) printf("%+" PRId64 " ", n); printf("| "); } - if (nNodesOffset != INT64_MAX) + if (nNodesOffset != numeric_limits::max()) printf("nNodesOffset = %+" PRId64 " (%+" PRId64 " minutes)\n", nNodesOffset, nNodesOffset/60); } }