-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutl.cpp
569 lines (380 loc) · 9.42 KB
/
utl.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
/***********************************************************
* this code by Peter Semiletov is Public Domain *
**********************************************************/
#include <sys/stat.h>
#include <algorithm>
/*
#ifndef USE_OPENSUSE
#include <filesystem>
#else
#include <experimental/filesystem>
#endif
*/
/*
#ifndef __has_include
static_assert(false, "__has_include not supported");
#else
# if __cplusplus >= 201703L && __has_include(<filesystem>)
# include <filesystem>
namespace fs = std::filesystem;
# elif __has_include(<experimental/filesystem>)
# include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
# endif
#endif
*/
#include <cstdint>
#include <cstdlib>
#include <vector>
#include <iostream>
#include <fstream>
#include <unistd.h>
#include <stdio.h> // for FILENAME_MAX
#include <limits>
#include <unistd.h>
#if defined(_WIN32) || defined(_WIN64)
//#include <pwd.h>
#include <windows.h>
#include <Shlobj.h>
#define GetCurrentDir _getcwd
#define DIR_SEPARATOR '\\'
#else
#include <sys/statvfs.h>
#define DIR_SEPARATOR '/'
//#define GetCurrentDir getcwd
#endif
#include "utl.h"
using namespace std;
size_t get_file_size (const string &fname)
{
if (fname.empty())
return 0;
struct stat buf;
stat (fname.c_str(), &buf);
return buf.st_size;
}
string get_file_ext (const string &fname)
{
std::string::size_type i = fname.rfind('.');
if (i != std::string::npos)
return fname.substr (i+1);
else
return string();
}
string replace_file_ext (const string &fname, const string &ext)
{
string result = fname;
string::size_type i = fname.rfind ('.', fname.length());
if (i != string::npos)
result.replace (i+1, ext.length(), ext);
return result;
}
string get_file_path (const string &path)
{
size_t i = path.rfind (DIR_SEPARATOR, path.length());
if (i != string::npos)
return path.substr (0, i);
return string();
}
string get_home_dir()
{
string result;
#if !defined(_WIN32) || !defined(_WIN64)
const char *homedir = getenv ("HOME");
if (homedir != NULL)
result = homedir;
/*else
homedir = getpwuid(getuid())->pw_dir;
if (homedir != NULL)
result = homedir;
*/
// return result;
#else
char homeDirStr[MAX_PATH];
if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_PERSONAL, NULL, 0, homeDirStr)))
result = homeDirStr;
#endif
return result;
}
/*
string current_path()
{
return fs::current_path().string();
}
*/
string current_path()
{
std::string result;
char buff[FILENAME_MAX];
#if defined(_WIN32) || defined(_WIN64)
if (_getcwd (buff, FILENAME_MAX))
result = buff;
#else
if (getcwd (buff, FILENAME_MAX))
result = buff;
#endif
return result;
}
string get_tmp_dir()
{
std::string result;
#if defined(_WIN32) || defined(_WIN64)
TCHAR szPath[MAX_PATH + 1];
DWORD d = GetTempPath(MAX_PATH + 1, szPath);
if (d != ERROR_SUCCESS)
result = "/tmp";
else
result = szPath;
#else
char const *d = getenv ("TMPDIR");
if (d)
result = d;
else
result = "/tmp";
#endif
return result;
}
/*
string current_path()
{
char path [FILENAME_MAX];
string result;
if (! get_cur_dir (path, sizeof (path)))
return result;
result = path;
return result;
}
*/
size_t get_free_space (const string &path)
{
if (path.empty())
return 0;
size_t result = 0;
#if defined(_WIN32) || defined(_WIN64)
BOOL fResult;
unsigned __int64 i64FreeBytesToCaller,
i64TotalBytes,
i64FreeBytes;
fResult = GetDiskFreeSpaceEx (path.c_str(),
(PULARGE_INTEGER)&i64FreeBytesToCaller,
(PULARGE_INTEGER)&i64TotalBytes,
(PULARGE_INTEGER)&i64FreeBytes);
if (fResult)
{
result = i64FreeBytesToCaller;
/*printf ("\n\nGetDiskFreeSpaceEx reports\n\n");
printf ("Available space to caller = %I64u MB\n",
i64FreeBytesToCaller / (1024*1024));
printf ("Total space = %I64u MB\n",
i64TotalBytes / (1024*1024));
printf ("Free space on drive = %I64u MB\n",
i64FreeBytes / (1024*1024));*/
}
#else
struct statvfs buf;
int r = statvfs (path.c_str(), &buf);
if (r < 0)
result = 0;
else
result = buf.f_bavail * buf.f_bsize;
#endif
return result;
}
/*
size_t get_free_space (const string &path)
{
if (path.empty())
return 0;
fs::path p (path);
const fs::space_info i = fs::space (p);
return i.available;
}
*/
bool file_exists (const string &name)
{
if (name.empty())
return false;
struct stat buffer;
return (stat (name.c_str(), &buffer) == 0);
}
bool is_program_exists (const string &appname)
{
if (appname.empty())
return false;
string cm = "which " + appname + " > /dev/null 2>&1";
if (system (cm.c_str()))
return false;
else
return true;
}
bool is_path_abs (const string &path)
{
if (path.empty())
return false;
if (path[0] == '/')
return true;
if (path.size() > 1)
if (path[1] == ':') //windows
return true;
return false;
}
string str_replace (string &source, const string &text_to_find, const string &replace_with)
{
if (source.empty())
return source;
if (text_to_find.empty())
return source;
if (replace_with.empty())
return source;
size_t pos = source.find (text_to_find);
if (pos == string::npos)
return source;
source.replace (pos, text_to_find.size(), replace_with);
return source;
}
string string_replace_all (const string &s, const string &from, const string &to)
{
string result = s;
size_t i = 0;
do
{
i = result.find (from);
if (i != string::npos)
result.replace (i, from.length(), to);
}
while (i != string::npos);
return result;
}
vector <string> split_string_to_vector (const string& s, const string& delimeter, const bool keep_empty)
{
vector <string> result;
if (delimeter.empty())
{
result.push_back (s);
return result;
}
string::const_iterator substart = s.begin(), subend;
while (true)
{
subend = search (substart, s.end(), delimeter.begin(), delimeter.end());
string temp (substart, subend);
if (keep_empty || ! temp.empty())
result.push_back (temp);
if (subend == s.end())
break;
substart = subend + delimeter.size();
}
return result;
}
string string_to_lower (const string &s)
{
string result = s;
std::for_each (
result.begin(),
result.end(),
[](char & c) {
c = ::tolower(c);
});
return result;
}
size_t string_to_file_size (const string &val)
{
size_t result = 0;
char* end;
const char *st = val.c_str();
if (st)
result = strtoull (st, &end, 10);
string s = string_to_lower (val);
if (s.find ("k") != string::npos)
result = result * 1024;
if (s.find ("m") != string::npos)
result = result * 1048576;
if (s.find ("g") != string::npos)
result = result * 1073741824;
return result;
}
string bytes_to_file_size (size_t val)
{
if (val >= 1073741824)
return std::to_string (val / 1073741824) + " gbytes";
if (val >= 1048576)
return std::to_string (val / 1048576) + " mbytes";
if (val >= 1024)
return std::to_string (val / 1024) + " kbytes";
return std::to_string (val) + " bytes";;
}
string bytes_to_file_size3 (size_t val)
{
if (val >= 1073741824)
return format3 (std::to_string (val / 1073741824)) + " gbytes";
if (val >= 1048576)
return format3 (std::to_string (val / 1048576)) + " mbytes";
if (val >= 1024)
return format3 (std::to_string (val / 1024)) + " kbytes";
return format3 (std::to_string (val)) + " bytes";;
}
string string_file_load (const string &fname)
{
if (fname.empty())
return string();
std::ifstream t (fname.c_str());
std::string s ((std::istreambuf_iterator<char>(t)),
std::istreambuf_iterator<char>());
return s;
}
vector <string> vector_file_load (const string &fname)
{
vector <string> v;
if (fname.empty())
return v;
ifstream infile (fname.c_str());
if (infile.fail())
{
// cout << "CPairFile::CPairFile - Could not open file: " << fname << endl;
return v;
}
string line;
while (getline (infile, line))
{
v.push_back (line);
}
infile.close();
return v;
}
string get_macro_name (const string &value)
{
size_t pos = value.find_first_of (':');
if (pos == string::npos)
pos = value.size();
return value.substr (0, pos);
}
void string_save_to_file (const string &fname, const string &s)
{
std::ofstream file (fname);
if (file.is_open())
file << s;
file.close();
}
bool path_exists (const string &fname)
{
if (fname.empty())
return false;
std::string fpath = get_file_path (fname);
if (fpath.empty())
return false;
return file_exists(fpath);
}
std::string format3 (size_t n)
{
std::string number_str = std::to_string(n);
for (int i = number_str.length() - 3; i > 0; i -= 3)
number_str.insert(i, ",");
return number_str;
}
std::string format3 (const std::string &s)
{
std::string number_str = s;
for (int i = number_str.length() - 3; i > 0; i -= 3)
number_str.insert(i, ",");
return number_str;
}