aboutsummaryrefslogtreecommitdiff
path: root/src/util.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/util.cpp')
-rw-r--r--src/util.cpp59
1 files changed, 42 insertions, 17 deletions
diff --git a/src/util.cpp b/src/util.cpp
index 296842acc3..9162886450 100644
--- a/src/util.cpp
+++ b/src/util.cpp
@@ -5,10 +5,11 @@
#include "util.h"
#include "sync.h"
-#include "strlcpy.h"
#include "version.h"
#include "ui_interface.h"
#include <boost/algorithm/string/join.hpp>
+#include <boost/algorithm/string/case_conv.hpp> // for to_lower()
+#include <boost/algorithm/string/predicate.hpp> // for startswith() and endswith()
// 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
@@ -220,8 +221,14 @@ inline int OutputDebugStringF(const char* pszFormat, ...)
if (fileout)
{
static bool fStartedNewLine = true;
- static boost::mutex mutexDebugLog;
- boost::mutex::scoped_lock scoped_lock(mutexDebugLog);
+
+ // This routine may be called by global destructors during shutdown.
+ // Since the order of destruction of static/global objects is undefined,
+ // allocate mutexDebugLog on the heap the first time this routine
+ // is called to avoid crashes during shutdown.
+ static boost::mutex* mutexDebugLog = NULL;
+ if (mutexDebugLog == NULL) mutexDebugLog = new boost::mutex();
+ boost::mutex::scoped_lock scoped_lock(*mutexDebugLog);
// reopen the log file, if requested
if (fReopenDebugLog) {
@@ -499,24 +506,24 @@ void ParseParameters(int argc, const char* const argv[])
mapMultiArgs.clear();
for (int i = 1; i < argc; i++)
{
- char psz[10000];
- strlcpy(psz, argv[i], sizeof(psz));
- char* pszValue = (char*)"";
- if (strchr(psz, '='))
+ std::string str(argv[i]);
+ std::string strValue;
+ size_t is_index = str.find('=');
+ if (is_index != std::string::npos)
{
- pszValue = strchr(psz, '=');
- *pszValue++ = '\0';
+ strValue = str.substr(is_index+1);
+ str = str.substr(0, is_index);
}
- #ifdef WIN32
- _strlwr(psz);
- if (psz[0] == '/')
- psz[0] = '-';
- #endif
- if (psz[0] != '-')
+#ifdef WIN32
+ boost::to_lower(str);
+ if (boost::algorithm::starts_with(str, "/"))
+ str = "-" + str.substr(1);
+#endif
+ if (str[0] != '-')
break;
- mapArgs[psz] = pszValue;
- mapMultiArgs[psz].push_back(pszValue);
+ mapArgs[str] = strValue;
+ mapMultiArgs[str].push_back(strValue);
}
// New 0.6 features:
@@ -1112,7 +1119,11 @@ void FileCommit(FILE *fileout)
#ifdef WIN32
_commit(_fileno(fileout));
#else
+ #if defined(__linux__) || defined(__NetBSD__)
+ fdatasync(fileno(fileout));
+ #else
fsync(fileno(fileout));
+ #endif
#endif
}
@@ -1126,6 +1137,20 @@ int GetFilesize(FILE* file)
return nFilesize;
}
+// this function tries to make a particular range of a file allocated (corresponding to disk space)
+// it is advisory, and the range specified in the arguments will never contain live data
+void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length) {
+ static const char buf[65536] = {};
+ fseek(file, offset, SEEK_SET);
+ while (length > 0) {
+ unsigned int now = 65536;
+ if (length < now)
+ now = length;
+ fwrite(buf, 1, now, file); // allowed to fail; this function is advisory anyway
+ length -= now;
+ }
+}
+
void ShrinkDebugFile()
{
// Scroll debug.log if it's getting too big