-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUtil.cpp
363 lines (301 loc) · 7.33 KB
/
Util.cpp
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
* $Id$
*
* Library of useful functions
* Part of Equinox Desktop Environment (EDE).
* Copyright (c) 2000-2006 EDE Authors.
*
* This program is licenced under terms of the
* GNU General Public Licence version 2 or newer.
* See COPYING for details.
*/
#include "Util.h"
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h>
#include <sys/stat.h>
#include <time.h>
#ifdef _WIN32
# include <io.h>
# include <direct.h>
# include <windows.h>
# define access(a,b) _access(a,b)
# define mkdir(a,b) _mkdir(a)
# define R_OK 4
#else
# include <unistd.h>
#endif /* _WIN32 */
// From Enumerations.h
#ifdef _WIN32
# undef slash
# define slash '\\'
#else
# undef slash
# define slash '/'
#endif
// End Enumerations.h
//using namespace fltk;
//using namespace edelib;
// Test if path is absolute or relative
int is_path_rooted(const char *fn)
{
#ifdef _WIN32
if (fn[0] == '/' || fn[0] == '.' || fn[0] == '\\' || fn[1]==':')
#else
if (fn[0] == '/' || fn[0] == '.')
#endif
return 1;
return 0;
}
// recursively create a path in the file system
bool make_path( const char *path )
{
if(access(path, 0))
{
const char *s = strrchr( path, slash );
if ( !s ) return 0;
int len = s-path;
char *p = (char*)malloc( len+1 );
memcpy( p, path, len );
p[len] = 0;
make_path( (const char*)p );
free( p );
return ( mkdir( path, 0777 ) == 0 );
}
return true;
}
// create the path needed for file using make_path
bool make_path_for_file( const char *path )
{
const char *s = strrchr( path, slash );
if ( !s ) return false;
int len = s-path;
char *p = (char*)malloc( len+1 );
memcpy( p, path, len );
p[len] = 0;
bool ret=make_path( (const char*)p );
free( p );
return ret;
}
// Cross-platform function for system path
char* get_sys_dir()
{
#ifndef _WIN32
return SYSTEM_PATH;
#else
static char path[PATH_MAX];
HKEY hKey;
if(RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\Windows\\CurrentVersion", 0, KEY_READ, &hKey)==ERROR_SUCCESS)
{
DWORD size=4096;
RegQueryValueExW(hKey, L"CommonFilesDir", NULL, NULL, (LPBYTE)path, &size);
RegCloseKey(hKey);
return path;
}
return "C:\\EDE\\";
#endif
}
// Cross-platform function for home directory...
// I don't see the purpose since getenv("HOME") works just fine
/*char* get_homedir() {
char *path = new char[PATH_MAX];
const char *str1;
str1=getenv("HOME");
if (str1) {
memcpy(path, str1, strlen(str1)+1);
return path;
}
return 0;
}*/
// strdupcat() - it's cool to strcat with implied realloc
// -- NOTE: due to use of realloc *always* use strdupcat return value:
// dest = strdupcat(dest,src);
// and *never* use it like:
// strdupcat(dest,src);
char* strdupcat(char *dest, const char *src)
{
if (!dest) {
dest=(char*)malloc(strlen(src));
} else {
dest=(char*)realloc (dest, strlen(dest)+strlen(src)+1);
}
strcat(dest,src);
return dest;
}
// wstrim() - for trimming characters (used in parser)
// parts of former fl_trimleft and fl_trimright from Fl_Util.cpp
char* wstrim(char *string)
{
if(!string)
return NULL;
char *start;
int len = strlen(string);
if (len) {
char *p = string + len;
do {
p--;
if ( !isspace(*p) ) break;
} while ( p != string );
if ( !isspace(*p) ) p++;
*p = 0;
}
for(start = string; *start && isspace (*start); start++);
memmove(string, start, strlen(start) + 1);
return string;
}
const char* twstrim(const char* string)
{
static char buffer[4096];
if (strlen(string)>4095) {
strncpy(buffer,string,4095);
buffer[4095]='\0';
} else
strcpy(buffer,string);
wstrim((char*)buffer);
return (const char*)buffer;
}
// hmmmh?
/*
char* wstrim(const char *string)
{
char *newstring = strdup(string);
return wstrim(newstring);
}*/
// Returns nicely formatted string for byte sizes
const char* nice_size(double size) {
static char buffer[256];
if (size<1024) {
snprintf(buffer,255,"%.0f B",size);
} else if (size<1024*10) {
snprintf(buffer,255,"%.2f kB",size/1024);
} else if (size<1024*100) {
snprintf(buffer,255,"%.1f kB",size/1024);
} else if (size<1024*1024) {
snprintf(buffer,255,"%.0f kB",size/1024);
} else if (size<1024*1024*10) {
snprintf(buffer,255,"%.2f MB",size/(1024*1024));
} else if (size<1024*1024*10) {
snprintf(buffer,255,"%.1f MB",size/(1024*1024));
} else if (size<1024*1024*1024) {
snprintf(buffer,255,"%.0f MB",size/(1024*1024));
} else if (size<1024*1024*1024*10) {
snprintf(buffer,255,"%.2f GB",size/(1024*1024*1024));
} else {
snprintf(buffer,255,"%.1f GB",size/(1024*1024*1024));
}
return (const char*) buffer;
}
const char* nice_time(long int epoch) {
static char buffer[256];
const time_t k = (const time_t)epoch;
const struct tm *timeptr = localtime(&k);
// Date/time format should be moved to configuration
snprintf(buffer,255,"%.2d.%.2d.%.4d. %.2d:%.2d", timeptr->tm_mday, timeptr->tm_mon+1, 1900+timeptr->tm_year, timeptr->tm_hour, timeptr->tm_min);
return buffer;
}
// Find in haystack any of needles (divided with separator)
const char* strstrmulti(const char *haystack, const char *needles, const char *separator) {
if (!haystack || !needles || (strlen(haystack)==0) || (strlen(needles)==0))
return (char*)haystack; // this means that empty search returns true
char *copy = strdup(needles);
char *token = strtok(copy, separator);
const char *result = 0;
do {
if ((result = strstr(haystack,token))) break;
} while ((token = strtok(NULL, separator)));
free (copy);
if (!result && (strcmp(separator,needles+strlen(needles)-strlen(separator))==0))
return (char*)haystack; // again
return result;
}
// vec_from_string() - similar to explode() in PHP or split() in Perl
// adapted from Fl_String_List to use vector
/*std::vector<char*> vec_from_string(const char *str, const char *separator)
{
if(!str) return std::vector<char*> ();
const char *ptr = str;
const char *s = strstr(ptr, separator);
std::vector<char*> retval;
if(s) {
unsigned separator_len = strlen(separator);
do {
unsigned len = s - ptr;
if (len) {
retval.push_back(strndup(ptr,len));
} else {
retval.push_back(NULL);
}
ptr = s + separator_len;
s = strstr(ptr, separator);
}
while(s);
if(*ptr) {
retval.push_back(strdup(ptr));
}
} else {
retval.push_back(strdup(ptr));
}
return retval;
}*/
// Print to a static char[] and return pointer
const char* tsprintf(const char *format, ...)
{
static char buffer[4096];
va_list args;
va_start(args, format);
vsnprintf(buffer, 4095, format, args);
va_end(args);
return (const char*)buffer;
}
char* tasprintf(const char *format, ...)
{
char buffer[4096];
va_list args;
va_start(args, format);
vsnprintf(buffer, 4095, format, args);
va_end(args);
return strdup(buffer);
}
// This function exists on some OSes and is mentioned in C textbooks
// However, we can just use sprintf instead
/*
char *
itoa(int value, char *string, int radix)
{
char tmp[33];
char *tp = tmp;
int i;
unsigned v;
int sign;
char *sp;
if (radix > 36 || radix <= 1)
{
return 0;
}
sign = (radix == 10 && value < 0);
if (sign)
v = -value;
else
v = (unsigned)value;
while (v || tp == tmp)
{
i = v % radix;
v = v / radix;
if (i < 10)
*tp++ = i+'0';
else
*tp++ = i + 'a' - 10;
}
if (string == 0)
string = (char *)malloc((tp-tmp)+sign+1);
sp = string;
if (sign)
*sp++ = '-';
while (tp > tmp)
*sp++ = *--tp;
*sp = 0;
return string;
}*/