forked from ROZ-MOFUMOFU-ME/node-multi-hashing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Util.h
123 lines (99 loc) · 3.29 KB
/
Util.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#ifndef UTIL_H_
#define UTIL_H_
#include "Common.h"
#include <sstream>
#include <iostream>
#include <queue>
#include <string>
#include <boost/date_time.hpp>
#include <boost/thread.hpp>
#include <boost/archive/iterators/base64_from_binary.hpp>
#include <boost/archive/iterators/binary_from_base64.hpp>
#include <boost/archive/iterators/transform_width.hpp>
#include <boost/archive/iterators/insert_linebreaks.hpp>
#include <boost/archive/iterators/remove_whitespace.hpp>
#include <boost/algorithm/string.hpp>
namespace Util
{
std::string Date(const char* format, bool utc = false);
uint32 Date();
std::string FS(const char *str, ...);
std::vector<std::string> Explode(std::string input, std::string delim);
template <typename T>
class SynchronisedQueue
{
public:
SynchronisedQueue() : __endQueue(false) {}
void Enqueue(const T& data)
{
boost::unique_lock<boost::mutex> lock(__mutex);
__queue.push(data);
__cond.notify_one();
}
T Dequeue()
{
boost::unique_lock<boost::mutex> lock(__mutex);
while (__queue.empty() && !__endQueue)
__cond.wait(lock);
if (__endQueue)
return NULL;
T result = __queue.front();
__queue.pop();
return result;
}
void Stop()
{
__endQueue = true;
__cond.notify_all();
}
uint32_t Size()
{
boost::unique_lock<boost::mutex> lock(__mutex);
return __queue.size();
}
private:
bool __endQueue;
std::queue<T> __queue;
boost::mutex __mutex;
boost::condition_variable __cond;
};
class GenericWorker
{
public:
~GenericWorker()
{
delete _thread;
}
void Activate()
{
_thread = new boost::thread(boost::bind(&GenericWorker::Work, this));
}
private:
virtual void Work() = 0;
boost::thread* _thread;
};
// Base64 encode/decode functions by http://stackoverflow.com/users/1132850/piquer
typedef boost::archive::iterators::insert_linebreaks
<
boost::archive::iterators::base64_from_binary
<
boost::archive::iterators::transform_width<std::string::const_iterator, 6, 8>
>, 72
> it_base64_lb_t;
typedef boost::archive::iterators::base64_from_binary
<
boost::archive::iterators::transform_width<std::string::const_iterator, 6, 8>
> it_base64_t;
typedef boost::archive::iterators::transform_width
<
boost::archive::iterators::binary_from_base64<std::string::const_iterator>, 8, 6
> it_binary_t;
std::string ToBase64(std::string input, bool linebreaks = true);
std::string FromBase64(std::string input);
uint8 ASCIIToHex(char ch);
std::vector<byte> ASCIIToBin(std::string str);
std::string BinToASCII(std::vector<byte> data);
std::vector<byte> Reverse(std::vector<byte> data);
std::vector<byte> Join(std::vector<byte> v1, std::vector<byte> v2);
}
#endif