~ubuntu-branches/ubuntu/utopic/bitcoin/utopic

« back to all changes in this revision

Viewing changes to src/util.h

  • Committer: Bazaar Package Importer
  • Author(s): Jonas Smedegaard, Jonas Smedegaard, Jan Dittberner
  • Date: 2011-07-19 15:08:54 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20110719150854-mhd8cclbbfrkhi1u
Tags: 0.3.24~dfsg-1
* New upstream release.

[ Jonas Smedegaard ]
* Improve various usage hints:
  + Explicitly mention in long description that bitcoind contains
    daemon and command-line interface.
  + Extend README.Debian with section on lack of GUI, and add primary
    headline.
  + Avoid installing upstream README: contains no parts relevant for
    Debian usage.
  Thanks to richard for suggestions (see bug#629443).
* Favor final releases over prereleases in rules and watch file.
  Thanks to Jan Dittberner.
* Track -src (not -linux) tarballs in rules and watch file.
  Thanks to Jan Dittberner.
* Drop patches 1004 and 1005 (integrated upstream) and simplify
  CXXFLAGS in rules file.
* Stop stripping no longer included source-less binaries from upstream
  tarballs.

[ Jan Dittberner ]
* refresh debian/patches/1000_use_system_crypto++.patch

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
// Copyright (c) 2009-2010 Satoshi Nakamoto
2
2
// Distributed under the MIT/X11 software license, see the accompanying
3
3
// file license.txt or http://www.opensource.org/licenses/mit-license.php.
 
4
#ifndef BITCOIN_UTIL_H
 
5
#define BITCOIN_UTIL_H
 
6
 
 
7
#include "uint256.h"
 
8
 
 
9
#ifndef __WXMSW__
 
10
#include <sys/types.h>
 
11
#include <sys/time.h>
 
12
#include <sys/resource.h>
 
13
#endif
 
14
#include <map>
 
15
#include <vector>
 
16
#include <string>
 
17
 
 
18
#include <boost/thread.hpp>
 
19
#include <boost/interprocess/sync/interprocess_recursive_mutex.hpp>
 
20
#include <boost/date_time/gregorian/gregorian_types.hpp>
 
21
#include <boost/date_time/posix_time/posix_time_types.hpp>
 
22
 
 
23
#include <openssl/sha.h>
 
24
#include <openssl/ripemd.h>
4
25
 
5
26
 
6
27
#if defined(_MSC_VER) || defined(__BORLANDC__)
17
38
#define __forceinline  inline
18
39
#endif
19
40
 
20
 
#define foreach             BOOST_FOREACH
21
41
#define loop                for (;;)
22
42
#define BEGIN(a)            ((char*)&(a))
23
43
#define END(a)              ((char*)&((&(a))[1]))
84
104
typedef int socklen_t;
85
105
#else
86
106
#define WSAGetLastError()   errno
 
107
#define WSAEINVAL           EINVAL
 
108
#define WSAEALREADY         EALREADY
87
109
#define WSAEWOULDBLOCK      EWOULDBLOCK
88
110
#define WSAEMSGSIZE         EMSGSIZE
89
111
#define WSAEINTR            EINTR
134
156
 
135
157
 
136
158
 
137
 
extern map<string, string> mapArgs;
138
 
extern map<string, vector<string> > mapMultiArgs;
 
159
extern std::map<std::string, std::string> mapArgs;
 
160
extern std::map<std::string, std::vector<std::string> > mapMultiArgs;
139
161
extern bool fDebug;
140
162
extern bool fPrintToConsole;
141
163
extern bool fPrintToDebugger;
145
167
extern bool fDaemon;
146
168
extern bool fServer;
147
169
extern bool fCommandLine;
148
 
extern string strMiscWarning;
 
170
extern std::string strMiscWarning;
149
171
extern bool fTestNet;
150
172
extern bool fNoListen;
151
173
extern bool fLogTimestamps;
154
176
void RandAddSeedPerfmon();
155
177
int OutputDebugStringF(const char* pszFormat, ...);
156
178
int my_snprintf(char* buffer, size_t limit, const char* format, ...);
157
 
string strprintf(const char* format, ...);
 
179
std::string strprintf(const char* format, ...);
158
180
bool error(const char* format, ...);
159
181
void LogException(std::exception* pex, const char* pszThread);
160
182
void PrintException(std::exception* pex, const char* pszThread);
161
183
void PrintExceptionContinue(std::exception* pex, const char* pszThread);
162
 
void ParseString(const string& str, char c, vector<string>& v);
163
 
string FormatMoney(int64 n, bool fPlus=false);
164
 
bool ParseMoney(const string& str, int64& nRet);
 
184
void ParseString(const std::string& str, char c, std::vector<std::string>& v);
 
185
std::string FormatMoney(int64 n, bool fPlus=false);
 
186
bool ParseMoney(const std::string& str, int64& nRet);
165
187
bool ParseMoney(const char* pszIn, int64& nRet);
166
 
vector<unsigned char> ParseHex(const char* psz);
167
 
vector<unsigned char> ParseHex(const string& str);
 
188
std::vector<unsigned char> ParseHex(const char* psz);
 
189
std::vector<unsigned char> ParseHex(const std::string& str);
168
190
void ParseParameters(int argc, char* argv[]);
169
191
const char* wxGetTranslation(const char* psz);
170
192
bool WildcardMatch(const char* psz, const char* mask);
171
 
bool WildcardMatch(const string& str, const string& mask);
 
193
bool WildcardMatch(const std::string& str, const std::string& mask);
172
194
int GetFilesize(FILE* file);
173
195
void GetDataDir(char* pszDirRet);
174
 
string GetConfigFile();
175
 
string GetPidFile();
176
 
void CreatePidFile(string pidFile, pid_t pid);
177
 
void ReadConfigFile(map<string, string>& mapSettingsRet, map<string, vector<string> >& mapMultiSettingsRet);
 
196
std::string GetConfigFile();
 
197
std::string GetPidFile();
 
198
void CreatePidFile(std::string pidFile, pid_t pid);
 
199
void ReadConfigFile(std::map<std::string, std::string>& mapSettingsRet, std::map<std::string, std::vector<std::string> >& mapMultiSettingsRet);
178
200
#ifdef __WXMSW__
179
 
string MyGetSpecialFolderPath(int nFolder, bool fCreate);
 
201
std::string MyGetSpecialFolderPath(int nFolder, bool fCreate);
180
202
#endif
181
 
string GetDefaultDataDir();
182
 
string GetDataDir();
 
203
std::string GetDefaultDataDir();
 
204
std::string GetDataDir();
183
205
void ShrinkDebugFile();
184
206
int GetRandInt(int nMax);
185
207
uint64 GetRand(uint64 nMax);
186
208
int64 GetTime();
187
209
int64 GetAdjustedTime();
188
210
void AddTimeData(unsigned int ip, int64 nTime);
189
 
string FormatFullVersion();
 
211
std::string FormatFullVersion();
190
212
 
191
213
 
192
214
 
268
290
 
269
291
 
270
292
 
271
 
inline string i64tostr(int64 n)
 
293
inline std::string i64tostr(int64 n)
272
294
{
273
295
    return strprintf("%"PRI64d, n);
274
296
}
275
297
 
276
 
inline string itostr(int n)
 
298
inline std::string itostr(int n)
277
299
{
278
300
    return strprintf("%d", n);
279
301
}
287
309
#endif
288
310
}
289
311
 
290
 
inline int64 atoi64(const string& str)
 
312
inline int64 atoi64(const std::string& str)
291
313
{
292
314
#ifdef _MSC_VER
293
315
    return _atoi64(str.c_str());
296
318
#endif
297
319
}
298
320
 
299
 
inline int atoi(const string& str)
 
321
inline int atoi(const std::string& str)
300
322
{
301
323
    return atoi(str.c_str());
302
324
}
317
339
}
318
340
 
319
341
template<typename T>
320
 
string HexStr(const T itbegin, const T itend, bool fSpaces=false)
 
342
std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
321
343
{
322
344
    if (itbegin == itend)
323
345
        return "";
324
346
    const unsigned char* pbegin = (const unsigned char*)&itbegin[0];
325
347
    const unsigned char* pend = pbegin + (itend - itbegin) * sizeof(itbegin[0]);
326
 
    string str;
 
348
    std::string str;
327
349
    str.reserve((pend-pbegin) * (fSpaces ? 3 : 2));
328
350
    for (const unsigned char* p = pbegin; p != pend; p++)
329
351
        str += strprintf((fSpaces && p != pend-1 ? "%02x " : "%02x"), *p);
330
352
    return str;
331
353
}
332
354
 
333
 
inline string HexStr(const vector<unsigned char>& vch, bool fSpaces=false)
 
355
inline std::string HexStr(const std::vector<unsigned char>& vch, bool fSpaces=false)
334
356
{
335
357
    return HexStr(vch.begin(), vch.end(), fSpaces);
336
358
}
337
359
 
338
360
template<typename T>
339
 
string HexNumStr(const T itbegin, const T itend, bool f0x=true)
 
361
std::string HexNumStr(const T itbegin, const T itend, bool f0x=true)
340
362
{
341
363
    if (itbegin == itend)
342
364
        return "";
343
365
    const unsigned char* pbegin = (const unsigned char*)&itbegin[0];
344
366
    const unsigned char* pend = pbegin + (itend - itbegin) * sizeof(itbegin[0]);
345
 
    string str = (f0x ? "0x" : "");
 
367
    std::string str = (f0x ? "0x" : "");
346
368
    str.reserve(str.size() + (pend-pbegin) * 2);
347
369
    for (const unsigned char* p = pend-1; p >= pbegin; p--)
348
370
        str += strprintf("%02x", *p);
349
371
    return str;
350
372
}
351
373
 
352
 
inline string HexNumStr(const vector<unsigned char>& vch, bool f0x=true)
 
374
inline std::string HexNumStr(const std::vector<unsigned char>& vch, bool f0x=true)
353
375
{
354
376
    return HexNumStr(vch.begin(), vch.end(), f0x);
355
377
}
360
382
    printf(pszFormat, HexStr(pbegin, pend, fSpaces).c_str());
361
383
}
362
384
 
363
 
inline void PrintHex(const vector<unsigned char>& vch, const char* pszFormat="%s", bool fSpaces=true)
 
385
inline void PrintHex(const std::vector<unsigned char>& vch, const char* pszFormat="%s", bool fSpaces=true)
364
386
{
365
387
    printf(pszFormat, HexStr(vch, fSpaces).c_str());
366
388
}
380
402
 
381
403
inline int64 GetTimeMillis()
382
404
{
383
 
    return (posix_time::ptime(posix_time::microsec_clock::universal_time()) -
384
 
            posix_time::ptime(gregorian::date(1970,1,1))).total_milliseconds();
 
405
    return (boost::posix_time::ptime(boost::posix_time::microsec_clock::universal_time()) -
 
406
            boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_milliseconds();
385
407
}
386
408
 
387
 
inline string DateTimeStrFormat(const char* pszFormat, int64 nTime)
 
409
inline std::string DateTimeStrFormat(const char* pszFormat, int64 nTime)
388
410
{
389
411
    time_t n = nTime;
390
412
    struct tm* ptmTime = gmtime(&n);
409
431
#endif
410
432
}
411
433
 
412
 
inline string GetArg(const string& strArg, const string& strDefault)
 
434
inline std::string GetArg(const std::string& strArg, const std::string& strDefault)
413
435
{
414
436
    if (mapArgs.count(strArg))
415
437
        return mapArgs[strArg];
416
438
    return strDefault;
417
439
}
418
440
 
419
 
inline int64 GetArg(const string& strArg, int64 nDefault)
 
441
inline int64 GetArg(const std::string& strArg, int64 nDefault)
420
442
{
421
443
    if (mapArgs.count(strArg))
422
444
        return atoi64(mapArgs[strArg]);
423
445
    return nDefault;
424
446
}
425
447
 
426
 
inline bool GetBoolArg(const string& strArg)
 
448
inline bool GetBoolArg(const std::string& strArg)
427
449
{
428
450
    if (mapArgs.count(strArg))
429
451
    {
538
560
    return Hash(ss.begin(), ss.end());
539
561
}
540
562
 
541
 
inline uint160 Hash160(const vector<unsigned char>& vch)
 
563
inline uint160 Hash160(const std::vector<unsigned char>& vch)
542
564
{
543
565
    uint256 hash1;
544
566
    SHA256(&vch[0], vch.size(), (unsigned char*)&hash1);
655
677
#endif
656
678
    return false;
657
679
}
 
680
 
 
681
#endif