Add --nostatslog option
[novacoin-seeder.git] / util.h
1 #ifndef _UTIL_H_
2 #define _UTIL_H_ 1
3
4 #include <pthread.h>
5 #include <errno.h>
6 #include <openssl/sha.h>
7 #include <stdarg.h>
8
9 #include "uint256.h"
10
11 #define loop                for (;;)
12 #define BEGIN(a)            ((char*)&(a))
13 #define END(a)              ((char*)&((&(a))[1]))
14 #define UBEGIN(a)           ((unsigned char*)&(a))
15 #define UEND(a)             ((unsigned char*)&((&(a))[1]))
16 #define ARRAYLEN(array)     (sizeof(array)/sizeof((array)[0]))
17
18 #define WSAGetLastError()   errno
19 #define WSAEINVAL           EINVAL
20 #define WSAEALREADY         EALREADY
21 #define WSAEWOULDBLOCK      EWOULDBLOCK
22 #define WSAEMSGSIZE         EMSGSIZE
23 #define WSAEINTR            EINTR
24 #define WSAEINPROGRESS      EINPROGRESS
25 #define WSAEADDRINUSE       EADDRINUSE
26 #define WSAENOTSOCK         EBADF
27 #define INVALID_SOCKET      (SOCKET)(~0)
28 #define SOCKET_ERROR        -1
29
30 // Wrapper to automatically initialize mutex
31 class CCriticalSection
32 {
33 protected:
34     pthread_rwlock_t mutex;
35 public:
36     explicit CCriticalSection() { pthread_rwlock_init(&mutex, NULL); }
37     ~CCriticalSection() { pthread_rwlock_destroy(&mutex); }
38     void Enter(bool fShared = false) { 
39       if (fShared) {
40         pthread_rwlock_rdlock(&mutex);
41       } else {
42         pthread_rwlock_wrlock(&mutex);
43       }
44     }
45     void Leave() { pthread_rwlock_unlock(&mutex); }
46 };
47
48 // Automatically leave critical section when leaving block, needed for exception safety
49 class CCriticalBlock
50 {
51 protected:
52     CCriticalSection* pcs;
53 public:
54     CCriticalBlock(CCriticalSection& cs, bool fShared = false) : pcs(&cs) { pcs->Enter(fShared); }
55     operator bool() const { return true; }
56     ~CCriticalBlock() { pcs->Leave(); }
57 };
58
59 #define CRITICAL_BLOCK(cs)     \
60     if (CCriticalBlock criticalblock = CCriticalBlock(cs))
61
62 #define SHARED_CRITICAL_BLOCK(cs)     \
63     if (CCriticalBlock criticalblock = CCriticalBlock(cs, true))
64
65 template<typename T1> inline uint256 Hash(const T1 pbegin, const T1 pend)
66 {
67     static unsigned char pblank[1];
68     uint256 hash1;
69     SHA256((pbegin == pend ? pblank : (unsigned char*)&pbegin[0]), (pend - pbegin) * sizeof(pbegin[0]), (unsigned char*)&hash1);
70     uint256 hash2;
71     SHA256((unsigned char*)&hash1, sizeof(hash1), (unsigned char*)&hash2);
72     return hash2;
73 }
74
75 void static inline Sleep(int nMilliSec) {
76     struct timespec wa;
77     wa.tv_sec = nMilliSec/1000;
78     wa.tv_nsec = (nMilliSec % 1000) * 1000000;
79     nanosleep(&wa, NULL);
80 }
81
82
83 std::string vstrprintf(const std::string &format, va_list ap);
84
85 std::string static inline strprintf(const std::string &format, ...) {
86     va_list arg_ptr;
87     va_start(arg_ptr, format);
88     std::string ret = vstrprintf(format, arg_ptr);
89     va_end(arg_ptr);
90     return ret;
91 }
92
93 bool static inline error(std::string err, ...) {
94     return false;
95 }
96
97 bool static inline my_printf(std::string err, ...) {
98     return true;
99 }
100
101 std::vector<unsigned char> DecodeBase32(const char* p, bool* pfInvalid = NULL);
102 std::string DecodeBase32(const std::string& str);
103 std::string EncodeBase32(const unsigned char* pch, size_t len);
104 std::string EncodeBase32(const std::string& str);
105
106 #endif