-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.cpp
More file actions
123 lines (101 loc) · 2.74 KB
/
Utils.cpp
File metadata and controls
123 lines (101 loc) · 2.74 KB
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
#include "Utils.h"
#include "PQueue.h"
#include <ctype.h>
#include <string.h>
#ifdef ENABLE_DBG_OUTPUT
#include <stdarg.h>
#endif
#include <ctime>
void * ADS::allocateArray (unsigned int uiLen, unsigned int uiElementSize)
{
return calloc (uiLen, uiElementSize);
}
void * ADS::reallocateArray (void *pArray, unsigned int uiCurrLen,
unsigned int uiNewLen, unsigned int uiElementSize)
{
if (uiNewLen <= uiCurrLen)
return pArray;
pArray = realloc (pArray, uiNewLen*uiElementSize);
unsigned int uiDiff = uiNewLen - uiCurrLen;
memset (static_cast<char*>(pArray)+(uiCurrLen*uiElementSize), NULL, uiDiff*uiElementSize);
return pArray;
}
void ADS::setArray (int iDefValue, void *pArray, unsigned int uiLen,
unsigned int uiElementSize)
{
memset (pArray, iDefValue, uiLen*uiElementSize);
}
void ADS::deallocate (void **ppPtr)
{
if (ppPtr != NULL && *ppPtr != NULL) {
free (*ppPtr);
ppPtr = NULL;
}
}
void ADS::emptyQueue (PQueue *pQueue)
{
if (pQueue == NULL)
return;
while (!pQueue->isEmpty())
delete pQueue->pop();
}
bool ADS::isNumber (const char *pszString)
{
for (int i = static_cast<int>(strlen (pszString))-1; i >= 0; i--)
if (!isdigit (pszString[i]))
return false;
return true;
}
bool ADS::isPowerOf2 (unsigned int value)
{
if (value == 0) {
return false;
}
return !(value & (value - 1));
}
void ADS::log (const char *pszMsg, ...)
{
#ifdef ENABLE_DBG_OUTPUT
va_list vargs;
va_start (vargs, pszMsg);
time_t now = time (NULL);
struct tm *ptm = localtime (&now);
fprintf (stdout, "%02d:%02d:%02d - ", ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
vfprintf (stdout, pszMsg, vargs);
fflush (stdout);
va_end (vargs);
#endif
}
unsigned int ADS::minimum (unsigned int ui1, unsigned int ui2)
{
if (ui1 < ui2)
return ui1;
return ui2;
}
float ADS::random (unsigned int uiMax)
{
static bool initWithSeed = true;
if (initWithSeed) {
srand (static_cast<unsigned int>(time (NULL)));
initWithSeed = false;
}
return random (static_cast<unsigned int>(0), uiMax);
}
float ADS::random (unsigned int uiMin, unsigned int uiMax)
{
float r = static_cast<float>(rand()) / static_cast<float>(RAND_MAX);
return uiMin + r * (uiMax - uiMin);
}
char * ADS::strDup (const char *pszString)
{
if (pszString == NULL)
return NULL;
unsigned int uiLen = strlen (pszString);
if (uiLen == 0)
return NULL;
char *pszCpy = static_cast<char *>(calloc (uiLen+1, sizeof (char)));
if (pszCpy == NULL)
return NULL;
memcpy (pszCpy, pszString, uiLen * sizeof(char));
return pszCpy;
}