-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathutilities.cpp
More file actions
240 lines (208 loc) · 4.87 KB
/
utilities.cpp
File metadata and controls
240 lines (208 loc) · 4.87 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include <sys/stat.h>
#include <string>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "utilities.hpp"
#ifdef WINDOWS
#include <windows.h>
#else
#include <sys/time.h>
#endif
using namespace std;
#ifndef WINDOWS
unsigned int GetTickCount()
{
struct timeval tv;
gettimeofday( &tv, NULL );
return (unsigned int)(tv.tv_sec * 1000 + tv.tv_usec / 1000);
}
#endif
char *absolute_path(char *source, char *destination)
{
#ifdef WINDOWS
return _fullpath(destination, source, 10000);
#else
return realpath(source, destination);
#endif
}
string lcase(string &str)
{
//change each element of the string to lower case
string s = str;
for(unsigned int i=0;i<s.length();i++)
{
s[i] = tolower(s[i]);
}
return s;
}
void utils_yield(void)
{
// don't use sleep(0) because it yields multiple time slices
#ifdef WINDOWS
sched_yield();
#elif defined(__SVR4) && defined(__sun)
yield(); // Solaris
#else
sched_yield(); // other *nix
#endif
}
/* reverse: reverse string s in place */
void reverse(char s[])
{
size_t j;
unsigned char c;
unsigned int i;
for (i = 0, j = strlen(s)-1; i<j; i++, j--) {
c = s[i];
s[i] = s[j];
s[j] = c;
}
}
/* itoa: convert n to characters in s */
void itoa(int n, char s[])
{
int i, sign;
if ((sign = n) < 0) /* record sign */
n = -n; /* make n positive */
i = 0;
do { /* generate digits in reverse order */
s[i++] = n % 10 + '0'; /* get next digit */
} while ((n /= 10) > 0); /* delete it */
if (sign < 0)
s[i++] = '-';
s[i] = '\0';
reverse(s);
}
void *aligned_malloc(size_t size, size_t alignment)
{
void *pa, *ptr;
pa = malloc((size + alignment - 1) + 2*sizeof(void *));
if(!pa)
return NULL;
ptr = (void *)(((size_t)pa + 2*sizeof(void *) + alignment - 1) & ~(alignment - 1));
*((void **)ptr - 1) = pa;
*((size_t *)ptr - 2) = size;
return ptr;
}
void aligned_free(void *ptr)
{
free(*((void **)ptr - 1));
}
void *aligned_realloc(void *ptr, size_t size, size_t alignment)
{
void *p = aligned_malloc(size, alignment);
if(!p)
return NULL;
if(!ptr)
return p;
size_t c = *((size_t *)ptr - 2);
if(c > size)
c = size;
memmove(p, *((void **)ptr - 1), c);
aligned_free(ptr);
return p;
}
unsigned int adler(unsigned char *data, size_t len, unsigned int crc)
{
unsigned int a = crc >> 16;
unsigned int b = crc & 0xffff;
while (len > 0)
{
size_t tlen = len > 5550 ? 5550 : len;
len -= tlen;
do
{
a += *data++;
b += a;
} while (--tlen);
a %= 65521;
b %= 65521;
}
return (b << 16) | a;
}
bool exists(string file)
{
// warning: may return false even though file does exist, but not vice versa
FILE *f = fopen(file.c_str(), "r");
if (f == 0)
return false;
else
{
fclose(f);
return true;
}
}
string remove_leading_curdir(string path)
{
if(path.length() >= 2 && (path.substr(0, 2) == ".\\" || path.substr(0, 2) == "./"))
return path.substr(2, path.length() - 2);
else
return path;
}
string remove_delimitor(string path)
{
size_t r = path.find_last_of("/\\");
if(r == path.length() - 1)
return path.substr(0, r);
else
return path;
}
bool is_dir(string path)
{
struct stat st_ifile;
#ifdef WINDOWS
if(path.length() == 2 && path.substr(1, 1) == ":")
path = path + "\\";
else if(path.length() == 3 && (path.substr(1, 2) == ":\\" || path.substr(1, 1) == ":/"))
{
// drive letter, do nothing
}
else
path = remove_delimitor(path);
#endif
stat(path.c_str(), &st_ifile);
#ifdef WINDOWS
const DWORD attributes = ::GetFileAttributes(path.c_str());
if (INVALID_FILE_ATTRIBUTES == attributes)
{
const DWORD error = ::GetLastError();
if (ERROR_FILE_NOT_FOUND == error || ERROR_PATH_NOT_FOUND == error)
return false;
else
return false;
}
return ((FILE_ATTRIBUTE_DIRECTORY & attributes) != 0);
#else
return S_ISDIR(st_ifile.st_mode);
#endif
}
string str(int intValue)
{
char myBuff[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
itoa(intValue,myBuff);
return((string)myBuff);
}
string delimiter(long long l)
{
char s[25], d[25];
unsigned int i, j = 0;
memset(s, 0, 25);
memset(d, 0, 25);
#ifdef WINDOWS
sprintf(s, "%I64d", l);
#else
snprintf(s, sizeof(s), "%lld", l);
#endif
for(i = 0; i < strlen(s); i++)
{
if((strlen(s) - i) % 3 == 0 && i != 0)
{
d[j] = ',';
j++;
}
d[j] = s[i];
j++;
}
return string(d);
}