summaryrefslogtreecommitdiff
path: root/protocols/WhatsApp/src/utils.h
blob: 7f0d903d1bce67166bab783e14b5a4b9094e1521 (plain)
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
#if !defined(WHATS_NG_UTILS_H)
#define WHATS_NG_UTILS_H

#include "WhatsAPI++/IMutex.h"

class ScopedLock
{
public:
	ScopedLock(HANDLE h, int t = INFINITE) : handle_(h), timeout_(t)
	{
		WaitForSingleObject(handle_, timeout_);
	}
	~ScopedLock()
	{
		if (handle_)
			ReleaseMutex(handle_);
	}
	void Unlock()
	{
		ReleaseMutex(handle_);
		handle_ = 0;
	}
private:
	HANDLE handle_;
	int timeout_;
};

class Mutex : public IMutex
{
private:
	HANDLE handle;
public:
	Mutex() : handle(NULL) {}

	virtual ~Mutex()
	{
		if (this->handle != NULL) {
			ReleaseMutex(this->handle);
		}
	}

	virtual void lock()
	{
		if (this->handle == NULL) {
			this->handle = CreateMutex(NULL, FALSE, NULL);
		}
	}

	virtual void unlock()
	{
		ReleaseMutex(this->handle);
		this->handle = NULL;
	}
};


std::string getLastErrorMsg();

namespace utils
{
	void setStatusMessage(MCONTACT hContact, const TCHAR *ptszMessage);
		
	BYTE* md5string(const BYTE*, int, BYTE* digest);
	__forceinline BYTE* md5string(const std::string &str, BYTE* digest)
	{
		return md5string((BYTE*)str.data(), (int)str.length(), digest);
	}
};


#endif