Switch to builtin tolower
[novacoin.git] / src / util.cpp
index c0513ef..f798da6 100644 (file)
@@ -8,8 +8,9 @@
 #include "version.h"
 #include "ui_interface.h"
 
+#include <random>
+
 #include <boost/algorithm/string/join.hpp>
-#include <boost/algorithm/string/case_conv.hpp> // for to_lower()
 #include <boost/program_options/detail/config_file.hpp>
 #include <boost/program_options/parsers.hpp>
 #include <boost/filesystem.hpp>
@@ -185,10 +186,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, ...)
@@ -507,7 +513,7 @@ void ParseParameters(int argc, const char* const argv[])
             str = str.substr(0, is_index);
         }
 #ifdef WIN32
-        boost::to_lower(str);
+        transform(str.begin(), str.end(), str.begin(), ::tolower);
         if (str.compare(0,1, "/") == 0)
             str = "-" + str.substr(1);
 #endif
@@ -1114,20 +1120,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<unsigned int>(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<unsigned int>(time(NULL)));
-
     ofstream pConf;
 #if BOOST_FILESYSTEM_VERSION >= 3
     pConf.open(GetConfigFile().generic_string().c_str());
@@ -1241,8 +1246,9 @@ void ShrinkDebugFile()
         // Restart the file with some of the end
         try {
             vector<char>* vBuf = new vector <char>(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)
@@ -1280,7 +1286,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<int64_t>::max();
 
 // Select time offset:
 int64_t GetTimeOffset()
@@ -1329,7 +1335,7 @@ void AddTimeData(const CNetAddr& ip, int64_t nTime)
         }
         else
         {
-            nNodesOffset = INT64_MAX;
+            nNodesOffset = numeric_limits<int64_t>::max();
 
             static bool fDone;
             if (!fDone)
@@ -1356,7 +1362,7 @@ void AddTimeData(const CNetAddr& ip, int64_t nTime)
                 printf("%+" PRId64 "  ", n);
             printf("|  ");
         }
-        if (nNodesOffset != INT64_MAX)
+        if (nNodesOffset != numeric_limits<int64_t>::max())
             printf("nNodesOffset = %+" PRId64 "  (%+" PRId64 " minutes)\n", nNodesOffset, nNodesOffset/60);
     }
 }