Mercurial > hg > bitcoin
annotate src/util.cpp @ 2167:dea3ba62a8b4 draft
removed obsolete boost workaround (boost ticket #4258)
author | Philip Kaufmann <phil.kaufmann@t-online.de> |
---|---|
date | Thu, 05 Apr 2012 08:34:31 +0200 |
parents | eb336ea43cc7 |
children | acab8afcdba7 |
rev | line source |
---|---|
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1 // Copyright (c) 2009-2010 Satoshi Nakamoto |
1818
20667468f95b
Update all copyrights to 2012
Gavin Andresen <gavinandresen@gmail.com>
parents:
1812
diff
changeset
|
2 // Copyright (c) 2009-2012 The Bitcoin developers |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
3 // Distributed under the MIT/X11 software license, see the accompanying |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
4 // file license.txt or http://www.opensource.org/licenses/mit-license.php. |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
5 #include "headers.h" |
609
a75f7da5c78c
Only include strlcpy.h when we have to
Jordan Lewis <jordanthelewis@gmail.com>
parents:
595
diff
changeset
|
6 #include "strlcpy.h" |
1584
a67f13278e2c
Implement BIP 14 : separate protocol version from client version
Gavin Andresen <gavinandresen@gmail.com>
parents:
1390
diff
changeset
|
7 #include <boost/algorithm/string/join.hpp> |
611
503d51554676
Only include certain boost headers if necessary.
Jordan Lewis <jordanthelewis@gmail.com>
parents:
609
diff
changeset
|
8 #include <boost/program_options/detail/config_file.hpp> |
503d51554676
Only include certain boost headers if necessary.
Jordan Lewis <jordanthelewis@gmail.com>
parents:
609
diff
changeset
|
9 #include <boost/program_options/parsers.hpp> |
695
9609c7af94ba
Fix missing includes needed for Boost 1.46.
Shane Wegner <shane-github@csy.ca>
parents:
688
diff
changeset
|
10 #include <boost/filesystem.hpp> |
611
503d51554676
Only include certain boost headers if necessary.
Jordan Lewis <jordanthelewis@gmail.com>
parents:
609
diff
changeset
|
11 #include <boost/filesystem/fstream.hpp> |
503d51554676
Only include certain boost headers if necessary.
Jordan Lewis <jordanthelewis@gmail.com>
parents:
609
diff
changeset
|
12 #include <boost/interprocess/sync/interprocess_mutex.hpp> |
503d51554676
Only include certain boost headers if necessary.
Jordan Lewis <jordanthelewis@gmail.com>
parents:
609
diff
changeset
|
13 #include <boost/interprocess/sync/interprocess_recursive_mutex.hpp> |
503d51554676
Only include certain boost headers if necessary.
Jordan Lewis <jordanthelewis@gmail.com>
parents:
609
diff
changeset
|
14 #include <boost/foreach.hpp> |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
15 |
595
e630bbb11629
make bitcoin include files more modular
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
575
diff
changeset
|
16 using namespace std; |
e630bbb11629
make bitcoin include files more modular
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
575
diff
changeset
|
17 using namespace boost; |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
18 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
19 map<string, string> mapArgs; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
20 map<string, vector<string> > mapMultiArgs; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
21 bool fDebug = false; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
22 bool fPrintToConsole = false; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
23 bool fPrintToDebugger = false; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
24 char pszSetDataDir[MAX_PATH] = ""; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
25 bool fRequestShutdown = false; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
26 bool fShutdown = false; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
27 bool fDaemon = false; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
28 bool fServer = false; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
29 bool fCommandLine = false; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
30 string strMiscWarning; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
31 bool fTestNet = false; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
32 bool fNoListen = false; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
33 bool fLogTimestamps = false; |
1616
997b708d15b8
Revert "Use standard C99 (and Qt) types for 64-bit integers"
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
1615
diff
changeset
|
34 CMedianFilter<int64> vTimeOffsets(200,0); |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
35 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
36 // Init openssl library multithreading support |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
37 static boost::interprocess::interprocess_mutex** ppmutexOpenSSL; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
38 void locking_callback(int mode, int i, const char* file, int line) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
39 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
40 if (mode & CRYPTO_LOCK) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
41 ppmutexOpenSSL[i]->lock(); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
42 else |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
43 ppmutexOpenSSL[i]->unlock(); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
44 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
45 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
46 // Init |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
47 class CInit |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
48 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
49 public: |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
50 CInit() |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
51 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
52 // Init openssl library multithreading support |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
53 ppmutexOpenSSL = (boost::interprocess::interprocess_mutex**)OPENSSL_malloc(CRYPTO_num_locks() * sizeof(boost::interprocess::interprocess_mutex*)); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
54 for (int i = 0; i < CRYPTO_num_locks(); i++) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
55 ppmutexOpenSSL[i] = new boost::interprocess::interprocess_mutex(); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
56 CRYPTO_set_locking_callback(locking_callback); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
57 |
1390
97bac6569f62
Fix build on windows and mac
Gavin Andresen <gavinandresen@gmail.com>
parents:
1361
diff
changeset
|
58 #ifdef WIN32 |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
59 // Seed random number generator with screen scrape and other hardware sources |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
60 RAND_screen(); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
61 #endif |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
62 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
63 // Seed random number generator with performance counter |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
64 RandAddSeed(); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
65 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
66 ~CInit() |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
67 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
68 // Shutdown openssl library multithreading support |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
69 CRYPTO_set_locking_callback(NULL); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
70 for (int i = 0; i < CRYPTO_num_locks(); i++) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
71 delete ppmutexOpenSSL[i]; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
72 OPENSSL_free(ppmutexOpenSSL); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
73 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
74 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
75 instance_of_cinit; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
76 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
77 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
78 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
79 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
80 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
81 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
82 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
83 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
84 void RandAddSeed() |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
85 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
86 // Seed with CPU performance counter |
1616
997b708d15b8
Revert "Use standard C99 (and Qt) types for 64-bit integers"
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
1615
diff
changeset
|
87 int64 nCounter = GetPerformanceCounter(); |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
88 RAND_add(&nCounter, sizeof(nCounter), 1.5); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
89 memset(&nCounter, 0, sizeof(nCounter)); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
90 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
91 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
92 void RandAddSeedPerfmon() |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
93 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
94 RandAddSeed(); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
95 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
96 // This can take up to 2 seconds, so only do it every 10 minutes |
1616
997b708d15b8
Revert "Use standard C99 (and Qt) types for 64-bit integers"
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
1615
diff
changeset
|
97 static int64 nLastPerfmon; |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
98 if (GetTime() < nLastPerfmon + 10 * 60) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
99 return; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
100 nLastPerfmon = GetTime(); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
101 |
1390
97bac6569f62
Fix build on windows and mac
Gavin Andresen <gavinandresen@gmail.com>
parents:
1361
diff
changeset
|
102 #ifdef WIN32 |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
103 // Don't need this on Linux, OpenSSL automatically uses /dev/urandom |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
104 // Seed with the entire set of perfmon data |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
105 unsigned char pdata[250000]; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
106 memset(pdata, 0, sizeof(pdata)); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
107 unsigned long nSize = sizeof(pdata); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
108 long ret = RegQueryValueExA(HKEY_PERFORMANCE_DATA, "Global", NULL, NULL, pdata, &nSize); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
109 RegCloseKey(HKEY_PERFORMANCE_DATA); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
110 if (ret == ERROR_SUCCESS) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
111 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
112 RAND_add(pdata, nSize, nSize/100.0); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
113 memset(pdata, 0, nSize); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
114 printf("%s RandAddSeed() %d bytes\n", DateTimeStrFormat("%x %H:%M", GetTime()).c_str(), nSize); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
115 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
116 #endif |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
117 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
118 |
1616
997b708d15b8
Revert "Use standard C99 (and Qt) types for 64-bit integers"
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
1615
diff
changeset
|
119 uint64 GetRand(uint64 nMax) |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
120 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
121 if (nMax == 0) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
122 return 0; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
123 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
124 // The range of the random source must be a multiple of the modulus |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
125 // to give every possible output value an equal possibility |
1616
997b708d15b8
Revert "Use standard C99 (and Qt) types for 64-bit integers"
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
1615
diff
changeset
|
126 uint64 nRange = (std::numeric_limits<uint64>::max() / nMax) * nMax; |
997b708d15b8
Revert "Use standard C99 (and Qt) types for 64-bit integers"
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
1615
diff
changeset
|
127 uint64 nRand = 0; |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
128 do |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
129 RAND_bytes((unsigned char*)&nRand, sizeof(nRand)); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
130 while (nRand >= nRange); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
131 return (nRand % nMax); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
132 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
133 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
134 int GetRandInt(int nMax) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
135 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
136 return GetRand(nMax); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
137 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
138 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
139 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
140 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
141 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
142 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
143 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
144 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
145 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
146 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
147 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
148 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
149 inline int OutputDebugStringF(const char* pszFormat, ...) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
150 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
151 int ret = 0; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
152 if (fPrintToConsole) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
153 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
154 // print to console |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
155 va_list arg_ptr; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
156 va_start(arg_ptr, pszFormat); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
157 ret = vprintf(pszFormat, arg_ptr); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
158 va_end(arg_ptr); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
159 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
160 else |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
161 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
162 // print to debug.log |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
163 static FILE* fileout = NULL; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
164 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
165 if (!fileout) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
166 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
167 char pszFile[MAX_PATH+100]; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
168 GetDataDir(pszFile); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
169 strlcat(pszFile, "/debug.log", sizeof(pszFile)); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
170 fileout = fopen(pszFile, "a"); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
171 if (fileout) setbuf(fileout, NULL); // unbuffered |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
172 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
173 if (fileout) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
174 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
175 static bool fStartedNewLine = true; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
176 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
177 // Debug print useful for profiling |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
178 if (fLogTimestamps && fStartedNewLine) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
179 fprintf(fileout, "%s ", DateTimeStrFormat("%x %H:%M:%S", GetTime()).c_str()); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
180 if (pszFormat[strlen(pszFormat) - 1] == '\n') |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
181 fStartedNewLine = true; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
182 else |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
183 fStartedNewLine = false; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
184 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
185 va_list arg_ptr; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
186 va_start(arg_ptr, pszFormat); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
187 ret = vfprintf(fileout, pszFormat, arg_ptr); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
188 va_end(arg_ptr); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
189 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
190 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
191 |
1390
97bac6569f62
Fix build on windows and mac
Gavin Andresen <gavinandresen@gmail.com>
parents:
1361
diff
changeset
|
192 #ifdef WIN32 |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
193 if (fPrintToDebugger) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
194 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
195 static CCriticalSection cs_OutputDebugStringF; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
196 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
197 // accumulate a line at a time |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
198 CRITICAL_BLOCK(cs_OutputDebugStringF) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
199 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
200 static char pszBuffer[50000]; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
201 static char* pend; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
202 if (pend == NULL) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
203 pend = pszBuffer; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
204 va_list arg_ptr; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
205 va_start(arg_ptr, pszFormat); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
206 int limit = END(pszBuffer) - pend - 2; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
207 int ret = _vsnprintf(pend, limit, pszFormat, arg_ptr); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
208 va_end(arg_ptr); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
209 if (ret < 0 || ret >= limit) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
210 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
211 pend = END(pszBuffer) - 2; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
212 *pend++ = '\n'; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
213 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
214 else |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
215 pend += ret; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
216 *pend = '\0'; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
217 char* p1 = pszBuffer; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
218 char* p2; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
219 while (p2 = strchr(p1, '\n')) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
220 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
221 p2++; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
222 char c = *p2; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
223 *p2 = '\0'; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
224 OutputDebugStringA(p1); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
225 *p2 = c; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
226 p1 = p2; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
227 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
228 if (p1 != pszBuffer) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
229 memmove(pszBuffer, p1, pend - p1 + 1); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
230 pend -= (p1 - pszBuffer); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
231 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
232 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
233 #endif |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
234 return ret; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
235 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
236 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
237 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
238 // Safer snprintf |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
239 // - prints up to limit-1 characters |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
240 // - output string is always null terminated even if limit reached |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
241 // - return value is the number of characters actually printed |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
242 int my_snprintf(char* buffer, size_t limit, const char* format, ...) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
243 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
244 if (limit == 0) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
245 return 0; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
246 va_list arg_ptr; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
247 va_start(arg_ptr, format); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
248 int ret = _vsnprintf(buffer, limit, format, arg_ptr); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
249 va_end(arg_ptr); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
250 if (ret < 0 || ret >= limit) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
251 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
252 ret = limit - 1; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
253 buffer[limit-1] = 0; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
254 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
255 return ret; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
256 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
257 |
2143
4bb59fe180f9
VC2010 compile fixes
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
2088
diff
changeset
|
258 string real_strprintf(const std::string &format, int dummy, ...) |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
259 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
260 char buffer[50000]; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
261 char* p = buffer; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
262 int limit = sizeof(buffer); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
263 int ret; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
264 loop |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
265 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
266 va_list arg_ptr; |
2143
4bb59fe180f9
VC2010 compile fixes
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
2088
diff
changeset
|
267 va_start(arg_ptr, dummy); |
1181
855f1d50bed7
temp patch for qtui
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
739
diff
changeset
|
268 ret = _vsnprintf(p, limit, format.c_str(), arg_ptr); |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
269 va_end(arg_ptr); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
270 if (ret >= 0 && ret < limit) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
271 break; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
272 if (p != buffer) |
639
069da23e4507
Fixes #240 and #244 - delete delete[] mismatch.
Matt Corallo <matt@bluematt.me>
parents:
595
diff
changeset
|
273 delete[] p; |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
274 limit *= 2; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
275 p = new char[limit]; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
276 if (p == NULL) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
277 throw std::bad_alloc(); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
278 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
279 string str(p, p+ret); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
280 if (p != buffer) |
639
069da23e4507
Fixes #240 and #244 - delete delete[] mismatch.
Matt Corallo <matt@bluematt.me>
parents:
595
diff
changeset
|
281 delete[] p; |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
282 return str; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
283 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
284 |
2143
4bb59fe180f9
VC2010 compile fixes
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
2088
diff
changeset
|
285 bool error(const char *format, ...) |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
286 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
287 char buffer[50000]; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
288 int limit = sizeof(buffer); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
289 va_list arg_ptr; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
290 va_start(arg_ptr, format); |
2143
4bb59fe180f9
VC2010 compile fixes
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
2088
diff
changeset
|
291 int ret = _vsnprintf(buffer, limit, format, arg_ptr); |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
292 va_end(arg_ptr); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
293 if (ret < 0 || ret >= limit) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
294 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
295 ret = limit - 1; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
296 buffer[limit-1] = 0; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
297 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
298 printf("ERROR: %s\n", buffer); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
299 return false; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
300 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
301 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
302 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
303 void ParseString(const string& str, char c, vector<string>& v) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
304 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
305 if (str.empty()) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
306 return; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
307 string::size_type i1 = 0; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
308 string::size_type i2; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
309 loop |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
310 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
311 i2 = str.find(c, i1); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
312 if (i2 == str.npos) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
313 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
314 v.push_back(str.substr(i1)); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
315 return; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
316 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
317 v.push_back(str.substr(i1, i2-i1)); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
318 i1 = i2+1; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
319 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
320 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
321 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
322 |
1616
997b708d15b8
Revert "Use standard C99 (and Qt) types for 64-bit integers"
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
1615
diff
changeset
|
323 string FormatMoney(int64 n, bool fPlus) |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
324 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
325 // Note: not using straight sprintf here because we do NOT want |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
326 // localized number formatting. |
1616
997b708d15b8
Revert "Use standard C99 (and Qt) types for 64-bit integers"
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
1615
diff
changeset
|
327 int64 n_abs = (n > 0 ? n : -n); |
997b708d15b8
Revert "Use standard C99 (and Qt) types for 64-bit integers"
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
1615
diff
changeset
|
328 int64 quotient = n_abs/COIN; |
997b708d15b8
Revert "Use standard C99 (and Qt) types for 64-bit integers"
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
1615
diff
changeset
|
329 int64 remainder = n_abs%COIN; |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
330 string str = strprintf("%"PRI64d".%08"PRI64d, quotient, remainder); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
331 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
332 // Right-trim excess 0's before the decimal point: |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
333 int nTrim = 0; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
334 for (int i = str.size()-1; (str[i] == '0' && isdigit(str[i-2])); --i) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
335 ++nTrim; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
336 if (nTrim) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
337 str.erase(str.size()-nTrim, nTrim); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
338 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
339 if (n < 0) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
340 str.insert((unsigned int)0, 1, '-'); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
341 else if (fPlus && n > 0) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
342 str.insert((unsigned int)0, 1, '+'); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
343 return str; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
344 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
345 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
346 |
1616
997b708d15b8
Revert "Use standard C99 (and Qt) types for 64-bit integers"
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
1615
diff
changeset
|
347 bool ParseMoney(const string& str, int64& nRet) |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
348 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
349 return ParseMoney(str.c_str(), nRet); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
350 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
351 |
1616
997b708d15b8
Revert "Use standard C99 (and Qt) types for 64-bit integers"
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
1615
diff
changeset
|
352 bool ParseMoney(const char* pszIn, int64& nRet) |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
353 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
354 string strWhole; |
1616
997b708d15b8
Revert "Use standard C99 (and Qt) types for 64-bit integers"
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
1615
diff
changeset
|
355 int64 nUnits = 0; |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
356 const char* p = pszIn; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
357 while (isspace(*p)) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
358 p++; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
359 for (; *p; p++) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
360 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
361 if (*p == '.') |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
362 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
363 p++; |
1616
997b708d15b8
Revert "Use standard C99 (and Qt) types for 64-bit integers"
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
1615
diff
changeset
|
364 int64 nMult = CENT*10; |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
365 while (isdigit(*p) && (nMult > 0)) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
366 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
367 nUnits += nMult * (*p++ - '0'); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
368 nMult /= 10; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
369 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
370 break; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
371 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
372 if (isspace(*p)) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
373 break; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
374 if (!isdigit(*p)) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
375 return false; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
376 strWhole.insert(strWhole.end(), *p); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
377 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
378 for (; *p; p++) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
379 if (!isspace(*p)) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
380 return false; |
1361
f82b42e087d7
remove possibility of 63 bit overflow in ParseMoney
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
1327
diff
changeset
|
381 if (strWhole.size() > 10) // guard against 63 bit overflow |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
382 return false; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
383 if (nUnits < 0 || nUnits > COIN) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
384 return false; |
1616
997b708d15b8
Revert "Use standard C99 (and Qt) types for 64-bit integers"
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
1615
diff
changeset
|
385 int64 nWhole = atoi64(strWhole); |
997b708d15b8
Revert "Use standard C99 (and Qt) types for 64-bit integers"
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
1615
diff
changeset
|
386 int64 nValue = nWhole*COIN + nUnits; |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
387 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
388 nRet = nValue; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
389 return true; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
390 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
391 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
392 |
1752
7b4383bddf3b
Replace OP_EVAL (BIP 12) with Pay-to-script-hash (BIP 16).
Gavin Andresen <gavinandresen@gmail.com>
parents:
1731
diff
changeset
|
393 static char phexdigit[256] = |
7b4383bddf3b
Replace OP_EVAL (BIP 12) with Pay-to-script-hash (BIP 16).
Gavin Andresen <gavinandresen@gmail.com>
parents:
1731
diff
changeset
|
394 { -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
7b4383bddf3b
Replace OP_EVAL (BIP 12) with Pay-to-script-hash (BIP 16).
Gavin Andresen <gavinandresen@gmail.com>
parents:
1731
diff
changeset
|
395 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
7b4383bddf3b
Replace OP_EVAL (BIP 12) with Pay-to-script-hash (BIP 16).
Gavin Andresen <gavinandresen@gmail.com>
parents:
1731
diff
changeset
|
396 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
7b4383bddf3b
Replace OP_EVAL (BIP 12) with Pay-to-script-hash (BIP 16).
Gavin Andresen <gavinandresen@gmail.com>
parents:
1731
diff
changeset
|
397 0,1,2,3,4,5,6,7,8,9,-1,-1,-1,-1,-1,-1, |
7b4383bddf3b
Replace OP_EVAL (BIP 12) with Pay-to-script-hash (BIP 16).
Gavin Andresen <gavinandresen@gmail.com>
parents:
1731
diff
changeset
|
398 -1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
7b4383bddf3b
Replace OP_EVAL (BIP 12) with Pay-to-script-hash (BIP 16).
Gavin Andresen <gavinandresen@gmail.com>
parents:
1731
diff
changeset
|
399 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
7b4383bddf3b
Replace OP_EVAL (BIP 12) with Pay-to-script-hash (BIP 16).
Gavin Andresen <gavinandresen@gmail.com>
parents:
1731
diff
changeset
|
400 -1,0xa,0xb,0xc,0xd,0xe,0xf,-1,-1,-1,-1,-1,-1,-1,-1,-1 |
7b4383bddf3b
Replace OP_EVAL (BIP 12) with Pay-to-script-hash (BIP 16).
Gavin Andresen <gavinandresen@gmail.com>
parents:
1731
diff
changeset
|
401 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
7b4383bddf3b
Replace OP_EVAL (BIP 12) with Pay-to-script-hash (BIP 16).
Gavin Andresen <gavinandresen@gmail.com>
parents:
1731
diff
changeset
|
402 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
7b4383bddf3b
Replace OP_EVAL (BIP 12) with Pay-to-script-hash (BIP 16).
Gavin Andresen <gavinandresen@gmail.com>
parents:
1731
diff
changeset
|
403 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
7b4383bddf3b
Replace OP_EVAL (BIP 12) with Pay-to-script-hash (BIP 16).
Gavin Andresen <gavinandresen@gmail.com>
parents:
1731
diff
changeset
|
404 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
7b4383bddf3b
Replace OP_EVAL (BIP 12) with Pay-to-script-hash (BIP 16).
Gavin Andresen <gavinandresen@gmail.com>
parents:
1731
diff
changeset
|
405 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
7b4383bddf3b
Replace OP_EVAL (BIP 12) with Pay-to-script-hash (BIP 16).
Gavin Andresen <gavinandresen@gmail.com>
parents:
1731
diff
changeset
|
406 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
7b4383bddf3b
Replace OP_EVAL (BIP 12) with Pay-to-script-hash (BIP 16).
Gavin Andresen <gavinandresen@gmail.com>
parents:
1731
diff
changeset
|
407 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
7b4383bddf3b
Replace OP_EVAL (BIP 12) with Pay-to-script-hash (BIP 16).
Gavin Andresen <gavinandresen@gmail.com>
parents:
1731
diff
changeset
|
408 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
7b4383bddf3b
Replace OP_EVAL (BIP 12) with Pay-to-script-hash (BIP 16).
Gavin Andresen <gavinandresen@gmail.com>
parents:
1731
diff
changeset
|
409 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, }; |
7b4383bddf3b
Replace OP_EVAL (BIP 12) with Pay-to-script-hash (BIP 16).
Gavin Andresen <gavinandresen@gmail.com>
parents:
1731
diff
changeset
|
410 |
7b4383bddf3b
Replace OP_EVAL (BIP 12) with Pay-to-script-hash (BIP 16).
Gavin Andresen <gavinandresen@gmail.com>
parents:
1731
diff
changeset
|
411 bool IsHex(const string& str) |
7b4383bddf3b
Replace OP_EVAL (BIP 12) with Pay-to-script-hash (BIP 16).
Gavin Andresen <gavinandresen@gmail.com>
parents:
1731
diff
changeset
|
412 { |
7b4383bddf3b
Replace OP_EVAL (BIP 12) with Pay-to-script-hash (BIP 16).
Gavin Andresen <gavinandresen@gmail.com>
parents:
1731
diff
changeset
|
413 BOOST_FOREACH(unsigned char c, str) |
7b4383bddf3b
Replace OP_EVAL (BIP 12) with Pay-to-script-hash (BIP 16).
Gavin Andresen <gavinandresen@gmail.com>
parents:
1731
diff
changeset
|
414 { |
7b4383bddf3b
Replace OP_EVAL (BIP 12) with Pay-to-script-hash (BIP 16).
Gavin Andresen <gavinandresen@gmail.com>
parents:
1731
diff
changeset
|
415 if (phexdigit[c] < 0) |
7b4383bddf3b
Replace OP_EVAL (BIP 12) with Pay-to-script-hash (BIP 16).
Gavin Andresen <gavinandresen@gmail.com>
parents:
1731
diff
changeset
|
416 return false; |
7b4383bddf3b
Replace OP_EVAL (BIP 12) with Pay-to-script-hash (BIP 16).
Gavin Andresen <gavinandresen@gmail.com>
parents:
1731
diff
changeset
|
417 } |
7b4383bddf3b
Replace OP_EVAL (BIP 12) with Pay-to-script-hash (BIP 16).
Gavin Andresen <gavinandresen@gmail.com>
parents:
1731
diff
changeset
|
418 return (str.size() > 0) && (str.size()%2 == 0); |
7b4383bddf3b
Replace OP_EVAL (BIP 12) with Pay-to-script-hash (BIP 16).
Gavin Andresen <gavinandresen@gmail.com>
parents:
1731
diff
changeset
|
419 } |
7b4383bddf3b
Replace OP_EVAL (BIP 12) with Pay-to-script-hash (BIP 16).
Gavin Andresen <gavinandresen@gmail.com>
parents:
1731
diff
changeset
|
420 |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
421 vector<unsigned char> ParseHex(const char* psz) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
422 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
423 // convert hex dump to vector |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
424 vector<unsigned char> vch; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
425 loop |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
426 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
427 while (isspace(*psz)) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
428 psz++; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
429 char c = phexdigit[(unsigned char)*psz++]; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
430 if (c == (char)-1) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
431 break; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
432 unsigned char n = (c << 4); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
433 c = phexdigit[(unsigned char)*psz++]; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
434 if (c == (char)-1) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
435 break; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
436 n |= c; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
437 vch.push_back(n); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
438 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
439 return vch; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
440 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
441 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
442 vector<unsigned char> ParseHex(const string& str) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
443 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
444 return ParseHex(str.c_str()); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
445 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
446 |
1855
82511bef45d3
Fix #846. Allow negative options such as "nolisten=1" in bitcoin.conf as well as on the command line.
Chris Moore <dooglus@gmail.com>
parents:
1818
diff
changeset
|
447 static void InterpretNegativeSetting(string name, map<string, string>& mapSettingsRet) |
82511bef45d3
Fix #846. Allow negative options such as "nolisten=1" in bitcoin.conf as well as on the command line.
Chris Moore <dooglus@gmail.com>
parents:
1818
diff
changeset
|
448 { |
82511bef45d3
Fix #846. Allow negative options such as "nolisten=1" in bitcoin.conf as well as on the command line.
Chris Moore <dooglus@gmail.com>
parents:
1818
diff
changeset
|
449 // interpret -nofoo as -foo=0 (and -nofoo=0 as -foo=1) as long as -foo not set |
82511bef45d3
Fix #846. Allow negative options such as "nolisten=1" in bitcoin.conf as well as on the command line.
Chris Moore <dooglus@gmail.com>
parents:
1818
diff
changeset
|
450 if (name.find("-no") == 0) |
82511bef45d3
Fix #846. Allow negative options such as "nolisten=1" in bitcoin.conf as well as on the command line.
Chris Moore <dooglus@gmail.com>
parents:
1818
diff
changeset
|
451 { |
82511bef45d3
Fix #846. Allow negative options such as "nolisten=1" in bitcoin.conf as well as on the command line.
Chris Moore <dooglus@gmail.com>
parents:
1818
diff
changeset
|
452 std::string positive("-"); |
82511bef45d3
Fix #846. Allow negative options such as "nolisten=1" in bitcoin.conf as well as on the command line.
Chris Moore <dooglus@gmail.com>
parents:
1818
diff
changeset
|
453 positive.append(name.begin()+3, name.end()); |
82511bef45d3
Fix #846. Allow negative options such as "nolisten=1" in bitcoin.conf as well as on the command line.
Chris Moore <dooglus@gmail.com>
parents:
1818
diff
changeset
|
454 if (mapSettingsRet.count(positive) == 0) |
82511bef45d3
Fix #846. Allow negative options such as "nolisten=1" in bitcoin.conf as well as on the command line.
Chris Moore <dooglus@gmail.com>
parents:
1818
diff
changeset
|
455 { |
82511bef45d3
Fix #846. Allow negative options such as "nolisten=1" in bitcoin.conf as well as on the command line.
Chris Moore <dooglus@gmail.com>
parents:
1818
diff
changeset
|
456 bool value = !GetBoolArg(name); |
82511bef45d3
Fix #846. Allow negative options such as "nolisten=1" in bitcoin.conf as well as on the command line.
Chris Moore <dooglus@gmail.com>
parents:
1818
diff
changeset
|
457 mapSettingsRet[positive] = (value ? "1" : "0"); |
82511bef45d3
Fix #846. Allow negative options such as "nolisten=1" in bitcoin.conf as well as on the command line.
Chris Moore <dooglus@gmail.com>
parents:
1818
diff
changeset
|
458 } |
82511bef45d3
Fix #846. Allow negative options such as "nolisten=1" in bitcoin.conf as well as on the command line.
Chris Moore <dooglus@gmail.com>
parents:
1818
diff
changeset
|
459 } |
82511bef45d3
Fix #846. Allow negative options such as "nolisten=1" in bitcoin.conf as well as on the command line.
Chris Moore <dooglus@gmail.com>
parents:
1818
diff
changeset
|
460 } |
82511bef45d3
Fix #846. Allow negative options such as "nolisten=1" in bitcoin.conf as well as on the command line.
Chris Moore <dooglus@gmail.com>
parents:
1818
diff
changeset
|
461 |
1811
49f43cb2eab0
Unit tests for the GetArg() methods
Gavin Andresen <gavinandresen@gmail.com>
parents:
1809
diff
changeset
|
462 void ParseParameters(int argc, const char*const argv[]) |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
463 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
464 mapArgs.clear(); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
465 mapMultiArgs.clear(); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
466 for (int i = 1; i < argc; i++) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
467 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
468 char psz[10000]; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
469 strlcpy(psz, argv[i], sizeof(psz)); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
470 char* pszValue = (char*)""; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
471 if (strchr(psz, '=')) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
472 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
473 pszValue = strchr(psz, '='); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
474 *pszValue++ = '\0'; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
475 } |
1390
97bac6569f62
Fix build on windows and mac
Gavin Andresen <gavinandresen@gmail.com>
parents:
1361
diff
changeset
|
476 #ifdef WIN32 |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
477 _strlwr(psz); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
478 if (psz[0] == '/') |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
479 psz[0] = '-'; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
480 #endif |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
481 if (psz[0] != '-') |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
482 break; |
1812
0c3c32816278
New GetArg features: allow --, and booleans can be -foo or -nofoo
Gavin Andresen <gavinandresen@gmail.com>
parents:
1811
diff
changeset
|
483 |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
484 mapArgs[psz] = pszValue; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
485 mapMultiArgs[psz].push_back(pszValue); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
486 } |
1812
0c3c32816278
New GetArg features: allow --, and booleans can be -foo or -nofoo
Gavin Andresen <gavinandresen@gmail.com>
parents:
1811
diff
changeset
|
487 |
0c3c32816278
New GetArg features: allow --, and booleans can be -foo or -nofoo
Gavin Andresen <gavinandresen@gmail.com>
parents:
1811
diff
changeset
|
488 // New 0.6 features: |
0c3c32816278
New GetArg features: allow --, and booleans can be -foo or -nofoo
Gavin Andresen <gavinandresen@gmail.com>
parents:
1811
diff
changeset
|
489 BOOST_FOREACH(const PAIRTYPE(string,string)& entry, mapArgs) |
0c3c32816278
New GetArg features: allow --, and booleans can be -foo or -nofoo
Gavin Andresen <gavinandresen@gmail.com>
parents:
1811
diff
changeset
|
490 { |
0c3c32816278
New GetArg features: allow --, and booleans can be -foo or -nofoo
Gavin Andresen <gavinandresen@gmail.com>
parents:
1811
diff
changeset
|
491 string name = entry.first; |
0c3c32816278
New GetArg features: allow --, and booleans can be -foo or -nofoo
Gavin Andresen <gavinandresen@gmail.com>
parents:
1811
diff
changeset
|
492 |
0c3c32816278
New GetArg features: allow --, and booleans can be -foo or -nofoo
Gavin Andresen <gavinandresen@gmail.com>
parents:
1811
diff
changeset
|
493 // interpret --foo as -foo (as long as both are not set) |
0c3c32816278
New GetArg features: allow --, and booleans can be -foo or -nofoo
Gavin Andresen <gavinandresen@gmail.com>
parents:
1811
diff
changeset
|
494 if (name.find("--") == 0) |
0c3c32816278
New GetArg features: allow --, and booleans can be -foo or -nofoo
Gavin Andresen <gavinandresen@gmail.com>
parents:
1811
diff
changeset
|
495 { |
0c3c32816278
New GetArg features: allow --, and booleans can be -foo or -nofoo
Gavin Andresen <gavinandresen@gmail.com>
parents:
1811
diff
changeset
|
496 std::string singleDash(name.begin()+1, name.end()); |
0c3c32816278
New GetArg features: allow --, and booleans can be -foo or -nofoo
Gavin Andresen <gavinandresen@gmail.com>
parents:
1811
diff
changeset
|
497 if (mapArgs.count(singleDash) == 0) |
0c3c32816278
New GetArg features: allow --, and booleans can be -foo or -nofoo
Gavin Andresen <gavinandresen@gmail.com>
parents:
1811
diff
changeset
|
498 mapArgs[singleDash] = entry.second; |
0c3c32816278
New GetArg features: allow --, and booleans can be -foo or -nofoo
Gavin Andresen <gavinandresen@gmail.com>
parents:
1811
diff
changeset
|
499 name = singleDash; |
0c3c32816278
New GetArg features: allow --, and booleans can be -foo or -nofoo
Gavin Andresen <gavinandresen@gmail.com>
parents:
1811
diff
changeset
|
500 } |
0c3c32816278
New GetArg features: allow --, and booleans can be -foo or -nofoo
Gavin Andresen <gavinandresen@gmail.com>
parents:
1811
diff
changeset
|
501 |
1855
82511bef45d3
Fix #846. Allow negative options such as "nolisten=1" in bitcoin.conf as well as on the command line.
Chris Moore <dooglus@gmail.com>
parents:
1818
diff
changeset
|
502 // interpret -nofoo as -foo=0 (and -nofoo=0 as -foo=1) as long as -foo not set |
82511bef45d3
Fix #846. Allow negative options such as "nolisten=1" in bitcoin.conf as well as on the command line.
Chris Moore <dooglus@gmail.com>
parents:
1818
diff
changeset
|
503 InterpretNegativeSetting(name, mapArgs); |
1812
0c3c32816278
New GetArg features: allow --, and booleans can be -foo or -nofoo
Gavin Andresen <gavinandresen@gmail.com>
parents:
1811
diff
changeset
|
504 } |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
505 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
506 |
1811
49f43cb2eab0
Unit tests for the GetArg() methods
Gavin Andresen <gavinandresen@gmail.com>
parents:
1809
diff
changeset
|
507 std::string GetArg(const std::string& strArg, const std::string& strDefault) |
49f43cb2eab0
Unit tests for the GetArg() methods
Gavin Andresen <gavinandresen@gmail.com>
parents:
1809
diff
changeset
|
508 { |
49f43cb2eab0
Unit tests for the GetArg() methods
Gavin Andresen <gavinandresen@gmail.com>
parents:
1809
diff
changeset
|
509 if (mapArgs.count(strArg)) |
49f43cb2eab0
Unit tests for the GetArg() methods
Gavin Andresen <gavinandresen@gmail.com>
parents:
1809
diff
changeset
|
510 return mapArgs[strArg]; |
49f43cb2eab0
Unit tests for the GetArg() methods
Gavin Andresen <gavinandresen@gmail.com>
parents:
1809
diff
changeset
|
511 return strDefault; |
49f43cb2eab0
Unit tests for the GetArg() methods
Gavin Andresen <gavinandresen@gmail.com>
parents:
1809
diff
changeset
|
512 } |
49f43cb2eab0
Unit tests for the GetArg() methods
Gavin Andresen <gavinandresen@gmail.com>
parents:
1809
diff
changeset
|
513 |
49f43cb2eab0
Unit tests for the GetArg() methods
Gavin Andresen <gavinandresen@gmail.com>
parents:
1809
diff
changeset
|
514 int64 GetArg(const std::string& strArg, int64 nDefault) |
49f43cb2eab0
Unit tests for the GetArg() methods
Gavin Andresen <gavinandresen@gmail.com>
parents:
1809
diff
changeset
|
515 { |
49f43cb2eab0
Unit tests for the GetArg() methods
Gavin Andresen <gavinandresen@gmail.com>
parents:
1809
diff
changeset
|
516 if (mapArgs.count(strArg)) |
49f43cb2eab0
Unit tests for the GetArg() methods
Gavin Andresen <gavinandresen@gmail.com>
parents:
1809
diff
changeset
|
517 return atoi64(mapArgs[strArg]); |
49f43cb2eab0
Unit tests for the GetArg() methods
Gavin Andresen <gavinandresen@gmail.com>
parents:
1809
diff
changeset
|
518 return nDefault; |
49f43cb2eab0
Unit tests for the GetArg() methods
Gavin Andresen <gavinandresen@gmail.com>
parents:
1809
diff
changeset
|
519 } |
49f43cb2eab0
Unit tests for the GetArg() methods
Gavin Andresen <gavinandresen@gmail.com>
parents:
1809
diff
changeset
|
520 |
49f43cb2eab0
Unit tests for the GetArg() methods
Gavin Andresen <gavinandresen@gmail.com>
parents:
1809
diff
changeset
|
521 bool GetBoolArg(const std::string& strArg, bool fDefault) |
49f43cb2eab0
Unit tests for the GetArg() methods
Gavin Andresen <gavinandresen@gmail.com>
parents:
1809
diff
changeset
|
522 { |
49f43cb2eab0
Unit tests for the GetArg() methods
Gavin Andresen <gavinandresen@gmail.com>
parents:
1809
diff
changeset
|
523 if (mapArgs.count(strArg)) |
49f43cb2eab0
Unit tests for the GetArg() methods
Gavin Andresen <gavinandresen@gmail.com>
parents:
1809
diff
changeset
|
524 { |
49f43cb2eab0
Unit tests for the GetArg() methods
Gavin Andresen <gavinandresen@gmail.com>
parents:
1809
diff
changeset
|
525 if (mapArgs[strArg].empty()) |
49f43cb2eab0
Unit tests for the GetArg() methods
Gavin Andresen <gavinandresen@gmail.com>
parents:
1809
diff
changeset
|
526 return true; |
49f43cb2eab0
Unit tests for the GetArg() methods
Gavin Andresen <gavinandresen@gmail.com>
parents:
1809
diff
changeset
|
527 return (atoi(mapArgs[strArg]) != 0); |
49f43cb2eab0
Unit tests for the GetArg() methods
Gavin Andresen <gavinandresen@gmail.com>
parents:
1809
diff
changeset
|
528 } |
49f43cb2eab0
Unit tests for the GetArg() methods
Gavin Andresen <gavinandresen@gmail.com>
parents:
1809
diff
changeset
|
529 return fDefault; |
49f43cb2eab0
Unit tests for the GetArg() methods
Gavin Andresen <gavinandresen@gmail.com>
parents:
1809
diff
changeset
|
530 } |
49f43cb2eab0
Unit tests for the GetArg() methods
Gavin Andresen <gavinandresen@gmail.com>
parents:
1809
diff
changeset
|
531 |
1644
35ccaffca27e
Fix issue #659, and cleanup wallet/command-line argument handling a bit
Gavin Andresen <gavinandresen@gmail.com>
parents:
1616
diff
changeset
|
532 bool SoftSetArg(const std::string& strArg, const std::string& strValue) |
35ccaffca27e
Fix issue #659, and cleanup wallet/command-line argument handling a bit
Gavin Andresen <gavinandresen@gmail.com>
parents:
1616
diff
changeset
|
533 { |
35ccaffca27e
Fix issue #659, and cleanup wallet/command-line argument handling a bit
Gavin Andresen <gavinandresen@gmail.com>
parents:
1616
diff
changeset
|
534 if (mapArgs.count(strArg)) |
35ccaffca27e
Fix issue #659, and cleanup wallet/command-line argument handling a bit
Gavin Andresen <gavinandresen@gmail.com>
parents:
1616
diff
changeset
|
535 return false; |
35ccaffca27e
Fix issue #659, and cleanup wallet/command-line argument handling a bit
Gavin Andresen <gavinandresen@gmail.com>
parents:
1616
diff
changeset
|
536 mapArgs[strArg] = strValue; |
35ccaffca27e
Fix issue #659, and cleanup wallet/command-line argument handling a bit
Gavin Andresen <gavinandresen@gmail.com>
parents:
1616
diff
changeset
|
537 return true; |
35ccaffca27e
Fix issue #659, and cleanup wallet/command-line argument handling a bit
Gavin Andresen <gavinandresen@gmail.com>
parents:
1616
diff
changeset
|
538 } |
35ccaffca27e
Fix issue #659, and cleanup wallet/command-line argument handling a bit
Gavin Andresen <gavinandresen@gmail.com>
parents:
1616
diff
changeset
|
539 |
1809
b00fd0a54a85
-bip16 option (default: 1) to support / not support BIP 16. And bumped default BIP16 switchover date from Feb 15 to Mar 1
Gavin Andresen <gavinandresen@gmail.com>
parents:
1776
diff
changeset
|
540 bool SoftSetBoolArg(const std::string& strArg, bool fValue) |
1644
35ccaffca27e
Fix issue #659, and cleanup wallet/command-line argument handling a bit
Gavin Andresen <gavinandresen@gmail.com>
parents:
1616
diff
changeset
|
541 { |
35ccaffca27e
Fix issue #659, and cleanup wallet/command-line argument handling a bit
Gavin Andresen <gavinandresen@gmail.com>
parents:
1616
diff
changeset
|
542 if (fValue) |
35ccaffca27e
Fix issue #659, and cleanup wallet/command-line argument handling a bit
Gavin Andresen <gavinandresen@gmail.com>
parents:
1616
diff
changeset
|
543 return SoftSetArg(strArg, std::string("1")); |
35ccaffca27e
Fix issue #659, and cleanup wallet/command-line argument handling a bit
Gavin Andresen <gavinandresen@gmail.com>
parents:
1616
diff
changeset
|
544 else |
35ccaffca27e
Fix issue #659, and cleanup wallet/command-line argument handling a bit
Gavin Andresen <gavinandresen@gmail.com>
parents:
1616
diff
changeset
|
545 return SoftSetArg(strArg, std::string("0")); |
35ccaffca27e
Fix issue #659, and cleanup wallet/command-line argument handling a bit
Gavin Andresen <gavinandresen@gmail.com>
parents:
1616
diff
changeset
|
546 } |
35ccaffca27e
Fix issue #659, and cleanup wallet/command-line argument handling a bit
Gavin Andresen <gavinandresen@gmail.com>
parents:
1616
diff
changeset
|
547 |
35ccaffca27e
Fix issue #659, and cleanup wallet/command-line argument handling a bit
Gavin Andresen <gavinandresen@gmail.com>
parents:
1616
diff
changeset
|
548 |
1327
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
549 string EncodeBase64(const unsigned char* pch, size_t len) |
1326
f70bb1af83c3
Faster Base64 decoder.
JoelKatz <DavidJoelSchwartz@GMail.com>
parents:
1320
diff
changeset
|
550 { |
1327
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
551 static const char *pbase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
552 |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
553 string strRet=""; |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
554 strRet.reserve((len+2)/3*4); |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
555 |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
556 int mode=0, left=0; |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
557 const unsigned char *pchEnd = pch+len; |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
558 |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
559 while (pch<pchEnd) |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
560 { |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
561 int enc = *(pch++); |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
562 switch (mode) |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
563 { |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
564 case 0: // we have no bits |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
565 strRet += pbase64[enc >> 2]; |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
566 left = (enc & 3) << 4; |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
567 mode = 1; |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
568 break; |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
569 |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
570 case 1: // we have two bits |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
571 strRet += pbase64[left | (enc >> 4)]; |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
572 left = (enc & 15) << 2; |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
573 mode = 2; |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
574 break; |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
575 |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
576 case 2: // we have four bits |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
577 strRet += pbase64[left | (enc >> 6)]; |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
578 strRet += pbase64[enc & 63]; |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
579 mode = 0; |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
580 break; |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
581 } |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
582 } |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
583 |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
584 if (mode) |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
585 { |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
586 strRet += pbase64[left]; |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
587 strRet += '='; |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
588 if (mode == 1) |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
589 strRet += '='; |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
590 } |
1326
f70bb1af83c3
Faster Base64 decoder.
JoelKatz <DavidJoelSchwartz@GMail.com>
parents:
1320
diff
changeset
|
591 |
1327
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
592 return strRet; |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
593 } |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
594 |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
595 string EncodeBase64(const string& str) |
1326
f70bb1af83c3
Faster Base64 decoder.
JoelKatz <DavidJoelSchwartz@GMail.com>
parents:
1320
diff
changeset
|
596 { |
1327
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
597 return EncodeBase64((const unsigned char*)str.c_str(), str.size()); |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
598 } |
1326
f70bb1af83c3
Faster Base64 decoder.
JoelKatz <DavidJoelSchwartz@GMail.com>
parents:
1320
diff
changeset
|
599 |
1327
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
600 vector<unsigned char> DecodeBase64(const char* p, bool* pfInvalid) |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
601 { |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
602 static const int decode64_table[256] = |
1326
f70bb1af83c3
Faster Base64 decoder.
JoelKatz <DavidJoelSchwartz@GMail.com>
parents:
1320
diff
changeset
|
603 { |
1327
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
604 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
605 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
606 -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
607 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
608 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
609 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
610 49, 50, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
611 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
612 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
613 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
614 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
615 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
616 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
617 }; |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
618 |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
619 if (pfInvalid) |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
620 *pfInvalid = false; |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
621 |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
622 vector<unsigned char> vchRet; |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
623 vchRet.reserve(strlen(p)*3/4); |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
624 |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
625 int mode = 0; |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
626 int left = 0; |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
627 |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
628 while (1) |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
629 { |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
630 int dec = decode64_table[*p]; |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
631 if (dec == -1) break; |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
632 p++; |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
633 switch (mode) |
1326
f70bb1af83c3
Faster Base64 decoder.
JoelKatz <DavidJoelSchwartz@GMail.com>
parents:
1320
diff
changeset
|
634 { |
f70bb1af83c3
Faster Base64 decoder.
JoelKatz <DavidJoelSchwartz@GMail.com>
parents:
1320
diff
changeset
|
635 case 0: // we have no bits and get 6 |
f70bb1af83c3
Faster Base64 decoder.
JoelKatz <DavidJoelSchwartz@GMail.com>
parents:
1320
diff
changeset
|
636 left = dec; |
f70bb1af83c3
Faster Base64 decoder.
JoelKatz <DavidJoelSchwartz@GMail.com>
parents:
1320
diff
changeset
|
637 mode = 1; |
f70bb1af83c3
Faster Base64 decoder.
JoelKatz <DavidJoelSchwartz@GMail.com>
parents:
1320
diff
changeset
|
638 break; |
f70bb1af83c3
Faster Base64 decoder.
JoelKatz <DavidJoelSchwartz@GMail.com>
parents:
1320
diff
changeset
|
639 |
f70bb1af83c3
Faster Base64 decoder.
JoelKatz <DavidJoelSchwartz@GMail.com>
parents:
1320
diff
changeset
|
640 case 1: // we have 6 bits and keep 4 |
1327
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
641 vchRet.push_back((left<<2) | (dec>>4)); |
1326
f70bb1af83c3
Faster Base64 decoder.
JoelKatz <DavidJoelSchwartz@GMail.com>
parents:
1320
diff
changeset
|
642 left = dec & 15; |
f70bb1af83c3
Faster Base64 decoder.
JoelKatz <DavidJoelSchwartz@GMail.com>
parents:
1320
diff
changeset
|
643 mode = 2; |
f70bb1af83c3
Faster Base64 decoder.
JoelKatz <DavidJoelSchwartz@GMail.com>
parents:
1320
diff
changeset
|
644 break; |
1327
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
645 |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
646 case 2: // we have 4 bits and get 6, we keep 2 |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
647 vchRet.push_back((left<<4) | (dec>>2)); |
1326
f70bb1af83c3
Faster Base64 decoder.
JoelKatz <DavidJoelSchwartz@GMail.com>
parents:
1320
diff
changeset
|
648 left = dec & 3; |
f70bb1af83c3
Faster Base64 decoder.
JoelKatz <DavidJoelSchwartz@GMail.com>
parents:
1320
diff
changeset
|
649 mode = 3; |
f70bb1af83c3
Faster Base64 decoder.
JoelKatz <DavidJoelSchwartz@GMail.com>
parents:
1320
diff
changeset
|
650 break; |
1327
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
651 |
1326
f70bb1af83c3
Faster Base64 decoder.
JoelKatz <DavidJoelSchwartz@GMail.com>
parents:
1320
diff
changeset
|
652 case 3: // we have 2 bits and get 6 |
1327
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
653 vchRet.push_back((left<<6) | dec); |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
654 mode = 0; |
1326
f70bb1af83c3
Faster Base64 decoder.
JoelKatz <DavidJoelSchwartz@GMail.com>
parents:
1320
diff
changeset
|
655 break; |
f70bb1af83c3
Faster Base64 decoder.
JoelKatz <DavidJoelSchwartz@GMail.com>
parents:
1320
diff
changeset
|
656 } |
f70bb1af83c3
Faster Base64 decoder.
JoelKatz <DavidJoelSchwartz@GMail.com>
parents:
1320
diff
changeset
|
657 } |
f70bb1af83c3
Faster Base64 decoder.
JoelKatz <DavidJoelSchwartz@GMail.com>
parents:
1320
diff
changeset
|
658 |
1327
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
659 if (pfInvalid) |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
660 switch (mode) |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
661 { |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
662 case 0: // 4n base64 characters processed: ok |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
663 break; |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
664 |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
665 case 1: // 4n+1 base64 character processed: impossible |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
666 *pfInvalid = true; |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
667 break; |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
668 |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
669 case 2: // 4n+2 base64 characters processed: require '==' |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
670 if (left || p[0] != '=' || p[1] != '=' || decode64_table[p[2]] != -1) |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
671 *pfInvalid = true; |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
672 break; |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
673 |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
674 case 3: // 4n+3 base64 characters processed: require '=' |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
675 if (left || p[0] != '=' || decode64_table[p[1]] != -1) |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
676 *pfInvalid = true; |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
677 break; |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
678 } |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
679 |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
680 return vchRet; |
1326
f70bb1af83c3
Faster Base64 decoder.
JoelKatz <DavidJoelSchwartz@GMail.com>
parents:
1320
diff
changeset
|
681 } |
f70bb1af83c3
Faster Base64 decoder.
JoelKatz <DavidJoelSchwartz@GMail.com>
parents:
1320
diff
changeset
|
682 |
1327
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
683 string DecodeBase64(const string& str) |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
684 { |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
685 vector<unsigned char> vchRet = DecodeBase64(str.c_str()); |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
686 return string((const char*)&vchRet[0], vchRet.size()); |
ee5daf89604c
Inline base64 encoder/decoder
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1326
diff
changeset
|
687 } |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
688 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
689 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
690 bool WildcardMatch(const char* psz, const char* mask) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
691 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
692 loop |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
693 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
694 switch (*mask) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
695 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
696 case '\0': |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
697 return (*psz == '\0'); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
698 case '*': |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
699 return WildcardMatch(psz, mask+1) || (*psz && WildcardMatch(psz+1, mask)); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
700 case '?': |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
701 if (*psz == '\0') |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
702 return false; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
703 break; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
704 default: |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
705 if (*psz != *mask) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
706 return false; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
707 break; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
708 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
709 psz++; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
710 mask++; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
711 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
712 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
713 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
714 bool WildcardMatch(const string& str, const string& mask) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
715 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
716 return WildcardMatch(str.c_str(), mask.c_str()); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
717 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
718 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
719 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
720 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
721 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
722 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
723 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
724 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
725 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
726 void FormatException(char* pszMessage, std::exception* pex, const char* pszThread) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
727 { |
1390
97bac6569f62
Fix build on windows and mac
Gavin Andresen <gavinandresen@gmail.com>
parents:
1361
diff
changeset
|
728 #ifdef WIN32 |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
729 char pszModule[MAX_PATH]; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
730 pszModule[0] = '\0'; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
731 GetModuleFileNameA(NULL, pszModule, sizeof(pszModule)); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
732 #else |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
733 const char* pszModule = "bitcoin"; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
734 #endif |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
735 if (pex) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
736 snprintf(pszMessage, 1000, |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
737 "EXCEPTION: %s \n%s \n%s in %s \n", typeid(*pex).name(), pex->what(), pszModule, pszThread); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
738 else |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
739 snprintf(pszMessage, 1000, |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
740 "UNKNOWN EXCEPTION \n%s in %s \n", pszModule, pszThread); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
741 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
742 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
743 void LogException(std::exception* pex, const char* pszThread) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
744 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
745 char pszMessage[10000]; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
746 FormatException(pszMessage, pex, pszThread); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
747 printf("\n%s", pszMessage); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
748 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
749 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
750 void PrintException(std::exception* pex, const char* pszThread) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
751 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
752 char pszMessage[10000]; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
753 FormatException(pszMessage, pex, pszThread); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
754 printf("\n\n************************\n%s\n", pszMessage); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
755 fprintf(stderr, "\n\n************************\n%s\n", pszMessage); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
756 strMiscWarning = pszMessage; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
757 throw; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
758 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
759 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
760 void PrintExceptionContinue(std::exception* pex, const char* pszThread) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
761 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
762 char pszMessage[10000]; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
763 FormatException(pszMessage, pex, pszThread); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
764 printf("\n\n************************\n%s\n", pszMessage); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
765 fprintf(stderr, "\n\n************************\n%s\n", pszMessage); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
766 strMiscWarning = pszMessage; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
767 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
768 |
1390
97bac6569f62
Fix build on windows and mac
Gavin Andresen <gavinandresen@gmail.com>
parents:
1361
diff
changeset
|
769 #ifdef WIN32 |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
770 string MyGetSpecialFolderPath(int nFolder, bool fCreate) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
771 { |
1919
38635f2c611a
Simplify MyGetSpecialFolderPath and fix possible buffer overflow (#901)
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
1855
diff
changeset
|
772 char pszPath[MAX_PATH] = ""; |
38635f2c611a
Simplify MyGetSpecialFolderPath and fix possible buffer overflow (#901)
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
1855
diff
changeset
|
773 if(SHGetSpecialFolderPathA(NULL, pszPath, nFolder, fCreate)) |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
774 { |
1919
38635f2c611a
Simplify MyGetSpecialFolderPath and fix possible buffer overflow (#901)
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
1855
diff
changeset
|
775 return pszPath; |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
776 } |
1919
38635f2c611a
Simplify MyGetSpecialFolderPath and fix possible buffer overflow (#901)
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
1855
diff
changeset
|
777 else if (nFolder == CSIDL_STARTUP) |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
778 { |
1919
38635f2c611a
Simplify MyGetSpecialFolderPath and fix possible buffer overflow (#901)
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
1855
diff
changeset
|
779 return string(getenv("USERPROFILE")) + "\\Start Menu\\Programs\\Startup"; |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
780 } |
1919
38635f2c611a
Simplify MyGetSpecialFolderPath and fix possible buffer overflow (#901)
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
1855
diff
changeset
|
781 else if (nFolder == CSIDL_APPDATA) |
38635f2c611a
Simplify MyGetSpecialFolderPath and fix possible buffer overflow (#901)
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
1855
diff
changeset
|
782 { |
38635f2c611a
Simplify MyGetSpecialFolderPath and fix possible buffer overflow (#901)
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
1855
diff
changeset
|
783 return getenv("APPDATA"); |
38635f2c611a
Simplify MyGetSpecialFolderPath and fix possible buffer overflow (#901)
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
1855
diff
changeset
|
784 } |
38635f2c611a
Simplify MyGetSpecialFolderPath and fix possible buffer overflow (#901)
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
1855
diff
changeset
|
785 return ""; |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
786 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
787 #endif |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
788 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
789 string GetDefaultDataDir() |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
790 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
791 // Windows: C:\Documents and Settings\username\Application Data\Bitcoin |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
792 // Mac: ~/Library/Application Support/Bitcoin |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
793 // Unix: ~/.bitcoin |
1390
97bac6569f62
Fix build on windows and mac
Gavin Andresen <gavinandresen@gmail.com>
parents:
1361
diff
changeset
|
794 #ifdef WIN32 |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
795 // Windows |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
796 return MyGetSpecialFolderPath(CSIDL_APPDATA, true) + "\\Bitcoin"; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
797 #else |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
798 char* pszHome = getenv("HOME"); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
799 if (pszHome == NULL || strlen(pszHome) == 0) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
800 pszHome = (char*)"/"; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
801 string strHome = pszHome; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
802 if (strHome[strHome.size()-1] != '/') |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
803 strHome += '/'; |
1390
97bac6569f62
Fix build on windows and mac
Gavin Andresen <gavinandresen@gmail.com>
parents:
1361
diff
changeset
|
804 #ifdef MAC_OSX |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
805 // Mac |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
806 strHome += "Library/Application Support/"; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
807 filesystem::create_directory(strHome.c_str()); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
808 return strHome + "Bitcoin"; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
809 #else |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
810 // Unix |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
811 return strHome + ".bitcoin"; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
812 #endif |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
813 #endif |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
814 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
815 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
816 void GetDataDir(char* pszDir) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
817 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
818 // pszDir must be at least MAX_PATH length. |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
819 int nVariation; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
820 if (pszSetDataDir[0] != 0) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
821 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
822 strlcpy(pszDir, pszSetDataDir, MAX_PATH); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
823 nVariation = 0; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
824 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
825 else |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
826 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
827 // This can be called during exceptions by printf, so we cache the |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
828 // value so we don't have to do memory allocations after that. |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
829 static char pszCachedDir[MAX_PATH]; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
830 if (pszCachedDir[0] == 0) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
831 strlcpy(pszCachedDir, GetDefaultDataDir().c_str(), sizeof(pszCachedDir)); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
832 strlcpy(pszDir, pszCachedDir, MAX_PATH); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
833 nVariation = 1; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
834 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
835 if (fTestNet) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
836 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
837 char* p = pszDir + strlen(pszDir); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
838 if (p > pszDir && p[-1] != '/' && p[-1] != '\\') |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
839 *p++ = '/'; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
840 strcpy(p, "testnet"); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
841 nVariation += 2; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
842 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
843 static bool pfMkdir[4]; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
844 if (!pfMkdir[nVariation]) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
845 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
846 pfMkdir[nVariation] = true; |
595
e630bbb11629
make bitcoin include files more modular
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
575
diff
changeset
|
847 boost::filesystem::create_directory(pszDir); |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
848 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
849 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
850 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
851 string GetDataDir() |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
852 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
853 char pszDir[MAX_PATH]; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
854 GetDataDir(pszDir); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
855 return pszDir; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
856 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
857 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
858 string GetConfigFile() |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
859 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
860 namespace fs = boost::filesystem; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
861 fs::path pathConfig(GetArg("-conf", "bitcoin.conf")); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
862 if (!pathConfig.is_complete()) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
863 pathConfig = fs::path(GetDataDir()) / pathConfig; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
864 return pathConfig.string(); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
865 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
866 |
1921
4fa7530953de
Reworked QT settings
Gavin Andresen <gavinandresen@gmail.com>
parents:
1855
diff
changeset
|
867 bool ReadConfigFile(map<string, string>& mapSettingsRet, |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
868 map<string, vector<string> >& mapMultiSettingsRet) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
869 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
870 namespace fs = boost::filesystem; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
871 namespace pod = boost::program_options::detail; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
872 |
1921
4fa7530953de
Reworked QT settings
Gavin Andresen <gavinandresen@gmail.com>
parents:
1855
diff
changeset
|
873 if (mapSettingsRet.count("-datadir")) |
4fa7530953de
Reworked QT settings
Gavin Andresen <gavinandresen@gmail.com>
parents:
1855
diff
changeset
|
874 { |
4fa7530953de
Reworked QT settings
Gavin Andresen <gavinandresen@gmail.com>
parents:
1855
diff
changeset
|
875 if (fs::is_directory(fs::system_complete(mapSettingsRet["-datadir"]))) |
4fa7530953de
Reworked QT settings
Gavin Andresen <gavinandresen@gmail.com>
parents:
1855
diff
changeset
|
876 { |
4fa7530953de
Reworked QT settings
Gavin Andresen <gavinandresen@gmail.com>
parents:
1855
diff
changeset
|
877 fs::path pathDataDir = fs::system_complete(mapSettingsRet["-datadir"]); |
4fa7530953de
Reworked QT settings
Gavin Andresen <gavinandresen@gmail.com>
parents:
1855
diff
changeset
|
878 strlcpy(pszSetDataDir, pathDataDir.string().c_str(), sizeof(pszSetDataDir)); |
4fa7530953de
Reworked QT settings
Gavin Andresen <gavinandresen@gmail.com>
parents:
1855
diff
changeset
|
879 } |
4fa7530953de
Reworked QT settings
Gavin Andresen <gavinandresen@gmail.com>
parents:
1855
diff
changeset
|
880 else |
4fa7530953de
Reworked QT settings
Gavin Andresen <gavinandresen@gmail.com>
parents:
1855
diff
changeset
|
881 { |
4fa7530953de
Reworked QT settings
Gavin Andresen <gavinandresen@gmail.com>
parents:
1855
diff
changeset
|
882 return false; |
4fa7530953de
Reworked QT settings
Gavin Andresen <gavinandresen@gmail.com>
parents:
1855
diff
changeset
|
883 } |
4fa7530953de
Reworked QT settings
Gavin Andresen <gavinandresen@gmail.com>
parents:
1855
diff
changeset
|
884 } |
4fa7530953de
Reworked QT settings
Gavin Andresen <gavinandresen@gmail.com>
parents:
1855
diff
changeset
|
885 |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
886 fs::ifstream streamConfig(GetConfigFile()); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
887 if (!streamConfig.good()) |
1921
4fa7530953de
Reworked QT settings
Gavin Andresen <gavinandresen@gmail.com>
parents:
1855
diff
changeset
|
888 return true; // No bitcoin.conf file is OK |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
889 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
890 set<string> setOptions; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
891 setOptions.insert("*"); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
892 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
893 for (pod::config_file_iterator it(streamConfig, setOptions), end; it != end; ++it) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
894 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
895 // Don't overwrite existing settings so command line settings override bitcoin.conf |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
896 string strKey = string("-") + it->string_key; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
897 if (mapSettingsRet.count(strKey) == 0) |
1855
82511bef45d3
Fix #846. Allow negative options such as "nolisten=1" in bitcoin.conf as well as on the command line.
Chris Moore <dooglus@gmail.com>
parents:
1818
diff
changeset
|
898 { |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
899 mapSettingsRet[strKey] = it->value[0]; |
1855
82511bef45d3
Fix #846. Allow negative options such as "nolisten=1" in bitcoin.conf as well as on the command line.
Chris Moore <dooglus@gmail.com>
parents:
1818
diff
changeset
|
900 // interpret nofoo=1 as foo=0 (and nofoo=0 as foo=1) as long as foo not set) |
82511bef45d3
Fix #846. Allow negative options such as "nolisten=1" in bitcoin.conf as well as on the command line.
Chris Moore <dooglus@gmail.com>
parents:
1818
diff
changeset
|
901 InterpretNegativeSetting(strKey, mapSettingsRet); |
82511bef45d3
Fix #846. Allow negative options such as "nolisten=1" in bitcoin.conf as well as on the command line.
Chris Moore <dooglus@gmail.com>
parents:
1818
diff
changeset
|
902 } |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
903 mapMultiSettingsRet[strKey].push_back(it->value[0]); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
904 } |
1921
4fa7530953de
Reworked QT settings
Gavin Andresen <gavinandresen@gmail.com>
parents:
1855
diff
changeset
|
905 return true; |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
906 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
907 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
908 string GetPidFile() |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
909 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
910 namespace fs = boost::filesystem; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
911 fs::path pathConfig(GetArg("-pid", "bitcoind.pid")); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
912 if (!pathConfig.is_complete()) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
913 pathConfig = fs::path(GetDataDir()) / pathConfig; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
914 return pathConfig.string(); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
915 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
916 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
917 void CreatePidFile(string pidFile, pid_t pid) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
918 { |
777
7e199b96a8d9
fix warnings: using the result of an assignment as a condition without parentheses [-Wparentheses]
Giel van Schijndel <me@mortis.eu>
parents:
739
diff
changeset
|
919 FILE* file = fopen(pidFile.c_str(), "w"); |
7e199b96a8d9
fix warnings: using the result of an assignment as a condition without parentheses [-Wparentheses]
Giel van Schijndel <me@mortis.eu>
parents:
739
diff
changeset
|
920 if (file) |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
921 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
922 fprintf(file, "%d\n", pid); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
923 fclose(file); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
924 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
925 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
926 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
927 int GetFilesize(FILE* file) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
928 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
929 int nSavePos = ftell(file); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
930 int nFilesize = -1; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
931 if (fseek(file, 0, SEEK_END) == 0) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
932 nFilesize = ftell(file); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
933 fseek(file, nSavePos, SEEK_SET); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
934 return nFilesize; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
935 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
936 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
937 void ShrinkDebugFile() |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
938 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
939 // Scroll debug.log if it's getting too big |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
940 string strFile = GetDataDir() + "/debug.log"; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
941 FILE* file = fopen(strFile.c_str(), "r"); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
942 if (file && GetFilesize(file) > 10 * 1000000) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
943 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
944 // Restart the file with some of the end |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
945 char pch[200000]; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
946 fseek(file, -sizeof(pch), SEEK_END); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
947 int nBytes = fread(pch, 1, sizeof(pch), file); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
948 fclose(file); |
777
7e199b96a8d9
fix warnings: using the result of an assignment as a condition without parentheses [-Wparentheses]
Giel van Schijndel <me@mortis.eu>
parents:
739
diff
changeset
|
949 |
7e199b96a8d9
fix warnings: using the result of an assignment as a condition without parentheses [-Wparentheses]
Giel van Schijndel <me@mortis.eu>
parents:
739
diff
changeset
|
950 file = fopen(strFile.c_str(), "w"); |
7e199b96a8d9
fix warnings: using the result of an assignment as a condition without parentheses [-Wparentheses]
Giel van Schijndel <me@mortis.eu>
parents:
739
diff
changeset
|
951 if (file) |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
952 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
953 fwrite(pch, 1, nBytes, file); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
954 fclose(file); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
955 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
956 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
957 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
958 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
959 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
960 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
961 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
962 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
963 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
964 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
965 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
966 // |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
967 // "Never go to sea with two chronometers; take one or three." |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
968 // Our three time sources are: |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
969 // - System clock |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
970 // - Median of other nodes's clocks |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
971 // - The user (asking the user to fix the system clock if the first two disagree) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
972 // |
1616
997b708d15b8
Revert "Use standard C99 (and Qt) types for 64-bit integers"
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
1615
diff
changeset
|
973 static int64 nMockTime = 0; // For unit testing |
1299
432633b3e50d
SetMockTime() for cleaner unit testing
Gavin Andresen <gavinandresen@gmail.com>
parents:
909
diff
changeset
|
974 |
1616
997b708d15b8
Revert "Use standard C99 (and Qt) types for 64-bit integers"
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
1615
diff
changeset
|
975 int64 GetTime() |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
976 { |
1299
432633b3e50d
SetMockTime() for cleaner unit testing
Gavin Andresen <gavinandresen@gmail.com>
parents:
909
diff
changeset
|
977 if (nMockTime) return nMockTime; |
432633b3e50d
SetMockTime() for cleaner unit testing
Gavin Andresen <gavinandresen@gmail.com>
parents:
909
diff
changeset
|
978 |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
979 return time(NULL); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
980 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
981 |
1616
997b708d15b8
Revert "Use standard C99 (and Qt) types for 64-bit integers"
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
1615
diff
changeset
|
982 void SetMockTime(int64 nMockTimeIn) |
1299
432633b3e50d
SetMockTime() for cleaner unit testing
Gavin Andresen <gavinandresen@gmail.com>
parents:
909
diff
changeset
|
983 { |
432633b3e50d
SetMockTime() for cleaner unit testing
Gavin Andresen <gavinandresen@gmail.com>
parents:
909
diff
changeset
|
984 nMockTime = nMockTimeIn; |
432633b3e50d
SetMockTime() for cleaner unit testing
Gavin Andresen <gavinandresen@gmail.com>
parents:
909
diff
changeset
|
985 } |
432633b3e50d
SetMockTime() for cleaner unit testing
Gavin Andresen <gavinandresen@gmail.com>
parents:
909
diff
changeset
|
986 |
1616
997b708d15b8
Revert "Use standard C99 (and Qt) types for 64-bit integers"
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
1615
diff
changeset
|
987 static int64 nTimeOffset = 0; |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
988 |
1616
997b708d15b8
Revert "Use standard C99 (and Qt) types for 64-bit integers"
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
1615
diff
changeset
|
989 int64 GetAdjustedTime() |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
990 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
991 return GetTime() + nTimeOffset; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
992 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
993 |
1731
4a2d4c136998
Network stack refactor
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1644
diff
changeset
|
994 void AddTimeData(const CNetAddr& ip, int64 nTime) |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
995 { |
1616
997b708d15b8
Revert "Use standard C99 (and Qt) types for 64-bit integers"
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
1615
diff
changeset
|
996 int64 nOffsetSample = nTime - GetTime(); |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
997 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
998 // Ignore duplicates |
1731
4a2d4c136998
Network stack refactor
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1644
diff
changeset
|
999 static set<CNetAddr> setKnown; |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1000 if (!setKnown.insert(ip).second) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1001 return; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1002 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1003 // Add data |
1545
6158c8936f47
Retain only the most recent time samples
Michael Hendricks <michael@ndrix.org>
parents:
1544
diff
changeset
|
1004 vTimeOffsets.input(nOffsetSample); |
6158c8936f47
Retain only the most recent time samples
Michael Hendricks <michael@ndrix.org>
parents:
1544
diff
changeset
|
1005 printf("Added time data, samples %d, offset %+"PRI64d" (%+"PRI64d" minutes)\n", vTimeOffsets.size(), nOffsetSample, nOffsetSample/60); |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1006 if (vTimeOffsets.size() >= 5 && vTimeOffsets.size() % 2 == 1) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1007 { |
1616
997b708d15b8
Revert "Use standard C99 (and Qt) types for 64-bit integers"
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
1615
diff
changeset
|
1008 int64 nMedian = vTimeOffsets.median(); |
997b708d15b8
Revert "Use standard C99 (and Qt) types for 64-bit integers"
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
1615
diff
changeset
|
1009 std::vector<int64> vSorted = vTimeOffsets.sorted(); |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1010 // Only let other nodes change our time by so much |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1011 if (abs64(nMedian) < 70 * 60) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1012 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1013 nTimeOffset = nMedian; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1014 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1015 else |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1016 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1017 nTimeOffset = 0; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1018 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1019 static bool fDone; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1020 if (!fDone) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1021 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1022 // If nobody has a time different than ours but within 5 minutes of ours, give a warning |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1023 bool fMatch = false; |
1616
997b708d15b8
Revert "Use standard C99 (and Qt) types for 64-bit integers"
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
1615
diff
changeset
|
1024 BOOST_FOREACH(int64 nOffset, vSorted) |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1025 if (nOffset != 0 && abs64(nOffset) < 5 * 60) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1026 fMatch = true; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1027 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1028 if (!fMatch) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1029 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1030 fDone = true; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1031 string strMessage = _("Warning: Please check that your computer's date and time are correct. If your clock is wrong Bitcoin will not work properly."); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1032 strMiscWarning = strMessage; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1033 printf("*** %s\n", strMessage.c_str()); |
2153
eb336ea43cc7
qtui.h/noui.h interface cleanup
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
2143
diff
changeset
|
1034 ThreadSafeMessageBox(strMessage+" ", string("Bitcoin"), wxOK | wxICON_EXCLAMATION); |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1035 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1036 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1037 } |
1544
5fba80d924d9
Only log time samples in debug mode
Michael Hendricks <michael@ndrix.org>
parents:
1390
diff
changeset
|
1038 if (fDebug) { |
1616
997b708d15b8
Revert "Use standard C99 (and Qt) types for 64-bit integers"
Wladimir J. van der Laan <laanwj@gmail.com>
parents:
1615
diff
changeset
|
1039 BOOST_FOREACH(int64 n, vSorted) |
1544
5fba80d924d9
Only log time samples in debug mode
Michael Hendricks <michael@ndrix.org>
parents:
1390
diff
changeset
|
1040 printf("%+"PRI64d" ", n); |
5fba80d924d9
Only log time samples in debug mode
Michael Hendricks <michael@ndrix.org>
parents:
1390
diff
changeset
|
1041 printf("| "); |
5fba80d924d9
Only log time samples in debug mode
Michael Hendricks <michael@ndrix.org>
parents:
1390
diff
changeset
|
1042 } |
5fba80d924d9
Only log time samples in debug mode
Michael Hendricks <michael@ndrix.org>
parents:
1390
diff
changeset
|
1043 printf("nTimeOffset = %+"PRI64d" (%+"PRI64d" minutes)\n", nTimeOffset, nTimeOffset/60); |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1044 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1045 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1046 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1047 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1048 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1049 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1050 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1051 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1052 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1053 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1054 string FormatVersion(int nVersion) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1055 { |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1056 if (nVersion%100 == 0) |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1057 return strprintf("%d.%d.%d", nVersion/1000000, (nVersion/10000)%100, (nVersion/100)%100); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1058 else |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1059 return strprintf("%d.%d.%d.%d", nVersion/1000000, (nVersion/10000)%100, (nVersion/100)%100, nVersion%100); |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1060 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1061 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1062 string FormatFullVersion() |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1063 { |
1584
a67f13278e2c
Implement BIP 14 : separate protocol version from client version
Gavin Andresen <gavinandresen@gmail.com>
parents:
1390
diff
changeset
|
1064 string s = FormatVersion(CLIENT_VERSION); |
687
ccaf25d00bd8
FormatFullVersion: build fix related to recent translation improvement
Jeff Garzik <jeff@garzik.org>
parents:
682
diff
changeset
|
1065 if (VERSION_IS_BETA) { |
ccaf25d00bd8
FormatFullVersion: build fix related to recent translation improvement
Jeff Garzik <jeff@garzik.org>
parents:
682
diff
changeset
|
1066 s += "-"; |
ccaf25d00bd8
FormatFullVersion: build fix related to recent translation improvement
Jeff Garzik <jeff@garzik.org>
parents:
682
diff
changeset
|
1067 s += _("beta"); |
ccaf25d00bd8
FormatFullVersion: build fix related to recent translation improvement
Jeff Garzik <jeff@garzik.org>
parents:
682
diff
changeset
|
1068 } |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1069 return s; |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1070 } |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1071 |
1584
a67f13278e2c
Implement BIP 14 : separate protocol version from client version
Gavin Andresen <gavinandresen@gmail.com>
parents:
1390
diff
changeset
|
1072 // Format the subversion field according to BIP 14 spec (https://en.bitcoin.it/wiki/BIP_0014) |
a67f13278e2c
Implement BIP 14 : separate protocol version from client version
Gavin Andresen <gavinandresen@gmail.com>
parents:
1390
diff
changeset
|
1073 std::string FormatSubVersion(const std::string& name, int nClientVersion, const std::vector<std::string>& comments) |
a67f13278e2c
Implement BIP 14 : separate protocol version from client version
Gavin Andresen <gavinandresen@gmail.com>
parents:
1390
diff
changeset
|
1074 { |
a67f13278e2c
Implement BIP 14 : separate protocol version from client version
Gavin Andresen <gavinandresen@gmail.com>
parents:
1390
diff
changeset
|
1075 std::ostringstream ss; |
a67f13278e2c
Implement BIP 14 : separate protocol version from client version
Gavin Andresen <gavinandresen@gmail.com>
parents:
1390
diff
changeset
|
1076 ss << "/"; |
a67f13278e2c
Implement BIP 14 : separate protocol version from client version
Gavin Andresen <gavinandresen@gmail.com>
parents:
1390
diff
changeset
|
1077 ss << name << ":" << FormatVersion(nClientVersion); |
a67f13278e2c
Implement BIP 14 : separate protocol version from client version
Gavin Andresen <gavinandresen@gmail.com>
parents:
1390
diff
changeset
|
1078 if (!comments.empty()) |
a67f13278e2c
Implement BIP 14 : separate protocol version from client version
Gavin Andresen <gavinandresen@gmail.com>
parents:
1390
diff
changeset
|
1079 ss << "(" << boost::algorithm::join(comments, "; ") << ")"; |
a67f13278e2c
Implement BIP 14 : separate protocol version from client version
Gavin Andresen <gavinandresen@gmail.com>
parents:
1390
diff
changeset
|
1080 ss << "/"; |
a67f13278e2c
Implement BIP 14 : separate protocol version from client version
Gavin Andresen <gavinandresen@gmail.com>
parents:
1390
diff
changeset
|
1081 return ss.str(); |
a67f13278e2c
Implement BIP 14 : separate protocol version from client version
Gavin Andresen <gavinandresen@gmail.com>
parents:
1390
diff
changeset
|
1082 } |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1083 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1084 |
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1085 |
872
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1086 #ifdef DEBUG_LOCKORDER |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1087 // |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1088 // Early deadlock detection. |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1089 // Problem being solved: |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1090 // Thread 1 locks A, then B, then C |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1091 // Thread 2 locks D, then C, then A |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1092 // --> may result in deadlock between the two threads, depending on when they run. |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1093 // Solution implemented here: |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1094 // Keep track of pairs of locks: (A before B), (A before C), etc. |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1095 // Complain if any thread trys to lock in a different order. |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1096 // |
575
1a0476fe825f
directory re-organization (keeps the old build system)
Jaromil <jaromil@dyne.org>
parents:
diff
changeset
|
1097 |
872
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1098 struct CLockLocation |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1099 { |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1100 CLockLocation(const char* pszName, const char* pszFile, int nLine) |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1101 { |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1102 mutexName = pszName; |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1103 sourceFile = pszFile; |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1104 sourceLine = nLine; |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1105 } |
909
b0a8d376ead4
If compiled -DDEBUG_LOCKORDER and run with -debug, print out every mutex lock/unlock (helpful for debugging something-is-holding-a-mutex-too-long problems)
Gavin Andresen <gavinandresen@gmail.com>
parents:
891
diff
changeset
|
1106 |
b0a8d376ead4
If compiled -DDEBUG_LOCKORDER and run with -debug, print out every mutex lock/unlock (helpful for debugging something-is-holding-a-mutex-too-long problems)
Gavin Andresen <gavinandresen@gmail.com>
parents:
891
diff
changeset
|
1107 std::string ToString() const |
b0a8d376ead4
If compiled -DDEBUG_LOCKORDER and run with -debug, print out every mutex lock/unlock (helpful for debugging something-is-holding-a-mutex-too-long problems)
Gavin Andresen <gavinandresen@gmail.com>
parents:
891
diff
changeset
|
1108 { |
b0a8d376ead4
If compiled -DDEBUG_LOCKORDER and run with -debug, print out every mutex lock/unlock (helpful for debugging something-is-holding-a-mutex-too-long problems)
Gavin Andresen <gavinandresen@gmail.com>
parents:
891
diff
changeset
|
1109 return mutexName+" "+sourceFile+":"+itostr(sourceLine); |
b0a8d376ead4
If compiled -DDEBUG_LOCKORDER and run with -debug, print out every mutex lock/unlock (helpful for debugging something-is-holding-a-mutex-too-long problems)
Gavin Andresen <gavinandresen@gmail.com>
parents:
891
diff
changeset
|
1110 } |
b0a8d376ead4
If compiled -DDEBUG_LOCKORDER and run with -debug, print out every mutex lock/unlock (helpful for debugging something-is-holding-a-mutex-too-long problems)
Gavin Andresen <gavinandresen@gmail.com>
parents:
891
diff
changeset
|
1111 |
b0a8d376ead4
If compiled -DDEBUG_LOCKORDER and run with -debug, print out every mutex lock/unlock (helpful for debugging something-is-holding-a-mutex-too-long problems)
Gavin Andresen <gavinandresen@gmail.com>
parents:
891
diff
changeset
|
1112 private: |
b0a8d376ead4
If compiled -DDEBUG_LOCKORDER and run with -debug, print out every mutex lock/unlock (helpful for debugging something-is-holding-a-mutex-too-long problems)
Gavin Andresen <gavinandresen@gmail.com>
parents:
891
diff
changeset
|
1113 std::string mutexName; |
b0a8d376ead4
If compiled -DDEBUG_LOCKORDER and run with -debug, print out every mutex lock/unlock (helpful for debugging something-is-holding-a-mutex-too-long problems)
Gavin Andresen <gavinandresen@gmail.com>
parents:
891
diff
changeset
|
1114 std::string sourceFile; |
b0a8d376ead4
If compiled -DDEBUG_LOCKORDER and run with -debug, print out every mutex lock/unlock (helpful for debugging something-is-holding-a-mutex-too-long problems)
Gavin Andresen <gavinandresen@gmail.com>
parents:
891
diff
changeset
|
1115 int sourceLine; |
872
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1116 }; |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1117 |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1118 typedef std::vector< std::pair<CCriticalSection*, CLockLocation> > LockStack; |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1119 |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1120 static boost::interprocess::interprocess_mutex dd_mutex; |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1121 static std::map<std::pair<CCriticalSection*, CCriticalSection*>, LockStack> lockorders; |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1122 static boost::thread_specific_ptr<LockStack> lockstack; |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1123 |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1124 |
891
a3f0b2791d42
Highlight mis-matching locks
Gavin Andresen <gavinandresen@gmail.com>
parents:
872
diff
changeset
|
1125 static void potential_deadlock_detected(const std::pair<CCriticalSection*, CCriticalSection*>& mismatch, const LockStack& s1, const LockStack& s2) |
872
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1126 { |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1127 printf("POTENTIAL DEADLOCK DETECTED\n"); |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1128 printf("Previous lock order was:\n"); |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1129 BOOST_FOREACH(const PAIRTYPE(CCriticalSection*, CLockLocation)& i, s2) |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1130 { |
891
a3f0b2791d42
Highlight mis-matching locks
Gavin Andresen <gavinandresen@gmail.com>
parents:
872
diff
changeset
|
1131 if (i.first == mismatch.first) printf(" (1)"); |
a3f0b2791d42
Highlight mis-matching locks
Gavin Andresen <gavinandresen@gmail.com>
parents:
872
diff
changeset
|
1132 if (i.first == mismatch.second) printf(" (2)"); |
909
b0a8d376ead4
If compiled -DDEBUG_LOCKORDER and run with -debug, print out every mutex lock/unlock (helpful for debugging something-is-holding-a-mutex-too-long problems)
Gavin Andresen <gavinandresen@gmail.com>
parents:
891
diff
changeset
|
1133 printf(" %s\n", i.second.ToString().c_str()); |
872
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1134 } |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1135 printf("Current lock order is:\n"); |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1136 BOOST_FOREACH(const PAIRTYPE(CCriticalSection*, CLockLocation)& i, s1) |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1137 { |
891
a3f0b2791d42
Highlight mis-matching locks
Gavin Andresen <gavinandresen@gmail.com>
parents:
872
diff
changeset
|
1138 if (i.first == mismatch.first) printf(" (1)"); |
a3f0b2791d42
Highlight mis-matching locks
Gavin Andresen <gavinandresen@gmail.com>
parents:
872
diff
changeset
|
1139 if (i.first == mismatch.second) printf(" (2)"); |
909
b0a8d376ead4
If compiled -DDEBUG_LOCKORDER and run with -debug, print out every mutex lock/unlock (helpful for debugging something-is-holding-a-mutex-too-long problems)
Gavin Andresen <gavinandresen@gmail.com>
parents:
891
diff
changeset
|
1140 printf(" %s\n", i.second.ToString().c_str()); |
872
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1141 } |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1142 } |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1143 |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1144 static void push_lock(CCriticalSection* c, const CLockLocation& locklocation) |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1145 { |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1146 bool fOrderOK = true; |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1147 if (lockstack.get() == NULL) |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1148 lockstack.reset(new LockStack); |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1149 |
909
b0a8d376ead4
If compiled -DDEBUG_LOCKORDER and run with -debug, print out every mutex lock/unlock (helpful for debugging something-is-holding-a-mutex-too-long problems)
Gavin Andresen <gavinandresen@gmail.com>
parents:
891
diff
changeset
|
1150 if (fDebug) printf("Locking: %s\n", locklocation.ToString().c_str()); |
872
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1151 dd_mutex.lock(); |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1152 |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1153 (*lockstack).push_back(std::make_pair(c, locklocation)); |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1154 |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1155 BOOST_FOREACH(const PAIRTYPE(CCriticalSection*, CLockLocation)& i, (*lockstack)) |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1156 { |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1157 if (i.first == c) break; |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1158 |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1159 std::pair<CCriticalSection*, CCriticalSection*> p1 = std::make_pair(i.first, c); |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1160 if (lockorders.count(p1)) |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1161 continue; |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1162 lockorders[p1] = (*lockstack); |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1163 |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1164 std::pair<CCriticalSection*, CCriticalSection*> p2 = std::make_pair(c, i.first); |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1165 if (lockorders.count(p2)) |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1166 { |
891
a3f0b2791d42
Highlight mis-matching locks
Gavin Andresen <gavinandresen@gmail.com>
parents:
872
diff
changeset
|
1167 potential_deadlock_detected(p1, lockorders[p2], lockorders[p1]); |
872
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1168 break; |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1169 } |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1170 } |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1171 dd_mutex.unlock(); |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1172 } |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1173 |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1174 static void pop_lock() |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1175 { |
909
b0a8d376ead4
If compiled -DDEBUG_LOCKORDER and run with -debug, print out every mutex lock/unlock (helpful for debugging something-is-holding-a-mutex-too-long problems)
Gavin Andresen <gavinandresen@gmail.com>
parents:
891
diff
changeset
|
1176 if (fDebug) |
b0a8d376ead4
If compiled -DDEBUG_LOCKORDER and run with -debug, print out every mutex lock/unlock (helpful for debugging something-is-holding-a-mutex-too-long problems)
Gavin Andresen <gavinandresen@gmail.com>
parents:
891
diff
changeset
|
1177 { |
b0a8d376ead4
If compiled -DDEBUG_LOCKORDER and run with -debug, print out every mutex lock/unlock (helpful for debugging something-is-holding-a-mutex-too-long problems)
Gavin Andresen <gavinandresen@gmail.com>
parents:
891
diff
changeset
|
1178 const CLockLocation& locklocation = (*lockstack).rbegin()->second; |
b0a8d376ead4
If compiled -DDEBUG_LOCKORDER and run with -debug, print out every mutex lock/unlock (helpful for debugging something-is-holding-a-mutex-too-long problems)
Gavin Andresen <gavinandresen@gmail.com>
parents:
891
diff
changeset
|
1179 printf("Unlocked: %s\n", locklocation.ToString().c_str()); |
b0a8d376ead4
If compiled -DDEBUG_LOCKORDER and run with -debug, print out every mutex lock/unlock (helpful for debugging something-is-holding-a-mutex-too-long problems)
Gavin Andresen <gavinandresen@gmail.com>
parents:
891
diff
changeset
|
1180 } |
b0a8d376ead4
If compiled -DDEBUG_LOCKORDER and run with -debug, print out every mutex lock/unlock (helpful for debugging something-is-holding-a-mutex-too-long problems)
Gavin Andresen <gavinandresen@gmail.com>
parents:
891
diff
changeset
|
1181 dd_mutex.lock(); |
872
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1182 (*lockstack).pop_back(); |
909
b0a8d376ead4
If compiled -DDEBUG_LOCKORDER and run with -debug, print out every mutex lock/unlock (helpful for debugging something-is-holding-a-mutex-too-long problems)
Gavin Andresen <gavinandresen@gmail.com>
parents:
891
diff
changeset
|
1183 dd_mutex.unlock(); |
872
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1184 } |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1185 |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1186 void CCriticalSection::Enter(const char* pszName, const char* pszFile, int nLine) |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1187 { |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1188 push_lock(this, CLockLocation(pszName, pszFile, nLine)); |
1776
c25505b7a486
Add DEBUG_LOCKCONTENTION, to warn each time a thread waits to lock.
Matt Corallo <matt@bluematt.me>
parents:
1752
diff
changeset
|
1189 #ifdef DEBUG_LOCKCONTENTION |
c25505b7a486
Add DEBUG_LOCKCONTENTION, to warn each time a thread waits to lock.
Matt Corallo <matt@bluematt.me>
parents:
1752
diff
changeset
|
1190 bool result = mutex.try_lock(); |
c25505b7a486
Add DEBUG_LOCKCONTENTION, to warn each time a thread waits to lock.
Matt Corallo <matt@bluematt.me>
parents:
1752
diff
changeset
|
1191 if (!result) |
c25505b7a486
Add DEBUG_LOCKCONTENTION, to warn each time a thread waits to lock.
Matt Corallo <matt@bluematt.me>
parents:
1752
diff
changeset
|
1192 { |
c25505b7a486
Add DEBUG_LOCKCONTENTION, to warn each time a thread waits to lock.
Matt Corallo <matt@bluematt.me>
parents:
1752
diff
changeset
|
1193 printf("LOCKCONTENTION: %s\n", pszName); |
c25505b7a486
Add DEBUG_LOCKCONTENTION, to warn each time a thread waits to lock.
Matt Corallo <matt@bluematt.me>
parents:
1752
diff
changeset
|
1194 printf("Locker: %s:%d\n", pszFile, nLine); |
c25505b7a486
Add DEBUG_LOCKCONTENTION, to warn each time a thread waits to lock.
Matt Corallo <matt@bluematt.me>
parents:
1752
diff
changeset
|
1195 mutex.lock(); |
c25505b7a486
Add DEBUG_LOCKCONTENTION, to warn each time a thread waits to lock.
Matt Corallo <matt@bluematt.me>
parents:
1752
diff
changeset
|
1196 printf("Locked\n"); |
c25505b7a486
Add DEBUG_LOCKCONTENTION, to warn each time a thread waits to lock.
Matt Corallo <matt@bluematt.me>
parents:
1752
diff
changeset
|
1197 } |
c25505b7a486
Add DEBUG_LOCKCONTENTION, to warn each time a thread waits to lock.
Matt Corallo <matt@bluematt.me>
parents:
1752
diff
changeset
|
1198 #else |
872
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1199 mutex.lock(); |
1776
c25505b7a486
Add DEBUG_LOCKCONTENTION, to warn each time a thread waits to lock.
Matt Corallo <matt@bluematt.me>
parents:
1752
diff
changeset
|
1200 #endif |
872
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1201 } |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1202 void CCriticalSection::Leave() |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1203 { |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1204 mutex.unlock(); |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1205 pop_lock(); |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1206 } |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1207 bool CCriticalSection::TryEnter(const char* pszName, const char* pszFile, int nLine) |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1208 { |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1209 push_lock(this, CLockLocation(pszName, pszFile, nLine)); |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1210 bool result = mutex.try_lock(); |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1211 if (!result) pop_lock(); |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1212 return result; |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1213 } |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1214 |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1215 #else |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1216 |
1776
c25505b7a486
Add DEBUG_LOCKCONTENTION, to warn each time a thread waits to lock.
Matt Corallo <matt@bluematt.me>
parents:
1752
diff
changeset
|
1217 void CCriticalSection::Enter(const char* pszName, const char* pszFile, int nLine) |
872
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1218 { |
1776
c25505b7a486
Add DEBUG_LOCKCONTENTION, to warn each time a thread waits to lock.
Matt Corallo <matt@bluematt.me>
parents:
1752
diff
changeset
|
1219 #ifdef DEBUG_LOCKCONTENTION |
c25505b7a486
Add DEBUG_LOCKCONTENTION, to warn each time a thread waits to lock.
Matt Corallo <matt@bluematt.me>
parents:
1752
diff
changeset
|
1220 bool result = mutex.try_lock(); |
c25505b7a486
Add DEBUG_LOCKCONTENTION, to warn each time a thread waits to lock.
Matt Corallo <matt@bluematt.me>
parents:
1752
diff
changeset
|
1221 if (!result) |
c25505b7a486
Add DEBUG_LOCKCONTENTION, to warn each time a thread waits to lock.
Matt Corallo <matt@bluematt.me>
parents:
1752
diff
changeset
|
1222 { |
c25505b7a486
Add DEBUG_LOCKCONTENTION, to warn each time a thread waits to lock.
Matt Corallo <matt@bluematt.me>
parents:
1752
diff
changeset
|
1223 printf("LOCKCONTENTION: %s\n", pszName); |
c25505b7a486
Add DEBUG_LOCKCONTENTION, to warn each time a thread waits to lock.
Matt Corallo <matt@bluematt.me>
parents:
1752
diff
changeset
|
1224 printf("Locker: %s:%d\n", pszFile, nLine); |
c25505b7a486
Add DEBUG_LOCKCONTENTION, to warn each time a thread waits to lock.
Matt Corallo <matt@bluematt.me>
parents:
1752
diff
changeset
|
1225 mutex.lock(); |
c25505b7a486
Add DEBUG_LOCKCONTENTION, to warn each time a thread waits to lock.
Matt Corallo <matt@bluematt.me>
parents:
1752
diff
changeset
|
1226 } |
c25505b7a486
Add DEBUG_LOCKCONTENTION, to warn each time a thread waits to lock.
Matt Corallo <matt@bluematt.me>
parents:
1752
diff
changeset
|
1227 #else |
872
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1228 mutex.lock(); |
1776
c25505b7a486
Add DEBUG_LOCKCONTENTION, to warn each time a thread waits to lock.
Matt Corallo <matt@bluematt.me>
parents:
1752
diff
changeset
|
1229 #endif |
872
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1230 } |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1231 |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1232 void CCriticalSection::Leave() |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1233 { |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1234 mutex.unlock(); |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1235 } |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1236 |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1237 bool CCriticalSection::TryEnter(const char*, const char*, int) |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1238 { |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1239 bool result = mutex.try_lock(); |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1240 return result; |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1241 } |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1242 |
3995c6aea28e
Compile with DEBUG_LOCKORDER to detect inconsistent lock orderings that can cause deadlocks
Gavin Andresen <gavinandresen@gmail.com>
parents:
846
diff
changeset
|
1243 #endif /* DEBUG_LOCKORDER */ |
1731
4a2d4c136998
Network stack refactor
Pieter Wuille <pieter.wuille@gmail.com>
parents:
1644
diff
changeset
|
1244 |