-
Notifications
You must be signed in to change notification settings - Fork 1
/
Utils.cpp
673 lines (565 loc) · 16.4 KB
/
Utils.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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
/******************************************************************************
* Copyright (c) 2011, Michael P. Gerlek ([email protected])
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following
* conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided
* with the distribution.
* * Neither the name of Hobu, Inc. or Flaxen Geo Consulting nor the
* names of its contributors may be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
****************************************************************************/
#include <pdal/util/Utils.hpp>
#include <array>
#include <cassert>
#include <cstdlib>
#include <cctype>
#include <memory>
#include <random>
#include <sstream>
#ifndef _WIN32
#include <cxxabi.h>
#include <sys/ioctl.h>
#include <sys/wait.h> // WIFEXITED, WEXITSTATUS
#endif
#pragma warning(disable: 4127) // conditional expression is constant
#include <stdio.h>
#include <iomanip>
#include "private/BacktraceImpl.hpp"
typedef std::vector<std::string> StringList;
namespace pdal
{
void Utils::random_seed(unsigned int seed)
{
srand(seed);
}
double Utils::random(double minimum, double maximum)
{
double r = (double)rand(); // [0..32767]
double v = (maximum - minimum) / (double)RAND_MAX;
double s = r * v; // [0..(max-min)]
double t = minimum + s; // [min..max]
assert(t >= minimum);
assert(t <= maximum);
return t;
}
int Utils::getenv(const std::string& name, std::string& val)
{
char* value = ::getenv(name.c_str());
if (value)
val = value;
else
val.clear();
return value ? 0 : -1;
}
int Utils::setenv(const std::string& env, const std::string& val)
{
#ifdef _WIN32
return ::_putenv_s(env.c_str(), val.c_str()) ? -1 : 0;
#else
return ::setenv(env.c_str(), val.c_str(), 1);
#endif
}
int Utils::unsetenv(const std::string& env)
{
#ifdef _WIN32
return ::_putenv_s(env.c_str(), "") ? -1 : 0;
#else
return ::unsetenv(env.c_str());
#endif
}
void Utils::eatwhitespace(std::istream& s)
{
while (true)
{
const char c = (char)s.peek();
if (!isspace(c))
break;
// throw it away
s.get();
}
}
void Utils::trimLeading(std::string& s)
{
size_t pos = 0;
// Note, that this should be OK in C++11, which guarantees a NULL.
while (isspace(s[pos]))
pos++;
s = s.substr(pos);
}
void Utils::trimTrailing(std::string& s)
{
if (s.empty())
return;
size_t pos = s.size() - 1;
while (isspace(s[pos]))
{
if (pos == 0)
{
s.clear();
return;
}
else
pos--;
}
s = s.substr(0, pos + 1);
}
bool Utils::eatcharacter(std::istream& s, char x)
{
const char c = (char)s.peek();
if (c != x)
return false;
// throw it away
s.get();
return true;
}
std::string Utils::base64_encode(const unsigned char *bytes_to_encode,
size_t in_len)
{
/*
base64.cpp and base64.h
Copyright (C) 2004-2008 René Nyffenegger
This source code is provided 'as-is', without any express or implied
warranty. In no event will the author be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this source code must not be misrepresented;
you must not claim that you wrote the original source code. If you
use this source code in a product, an acknowledgment in the product
documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must
not be misrepresented as being the original source code.
3. This notice may not be removed or altered from any source
distribution.
René Nyffenegger [email protected]
*/
if (in_len == 0)
return std::string();
const std::string base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
std::string ret;
int i = 0;
int j = 0;
uint8_t char_array_3[3];
uint8_t char_array_4[4];
while (in_len--)
{
char_array_3[i++] = *(bytes_to_encode++);
if (i == 3)
{
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) +
((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) +
((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;
for (i = 0; (i <4) ; i++)
ret += base64_chars[char_array_4[i]];
i = 0;
}
}
if (i)
{
for (j = i; j < 3; j++)
char_array_3[j] = '\0';
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) +
((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) +
((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f;
for (j = 0; (j < i + 1); j++)
ret += base64_chars[char_array_4[j]];
while ((i++ < 3))
ret += '=';
}
return ret;
}
static inline bool is_base64(unsigned char c)
{
return (isalnum(c) || (c == '+') || (c == '/'));
}
std::vector<uint8_t> Utils::base64_decode(std::string const& encoded_string)
{
const std::string base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
std::string::size_type in_len = encoded_string.size();
int i = 0;
int j = 0;
int in_ = 0;
unsigned char char_array_4[4], char_array_3[3];
std::vector<uint8_t> ret;
while (in_len-- && (encoded_string[in_] != '=') && is_base64(encoded_string[in_]))
{
char_array_4[i++] = encoded_string[in_];
in_++;
if (i == 4)
{
for (i = 0; i <4; i++)
char_array_4[i] = static_cast<unsigned char>(
base64_chars.find(char_array_4[i]));
char_array_3[0] = (char_array_4[0] << 2) +
((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) +
((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (i = 0; (i < 3); i++)
ret.push_back(char_array_3[i]);
i = 0;
}
}
if (i)
{
for (j = i; j <4; j++)
char_array_4[j] = 0;
for (j = 0; j <4; j++)
char_array_4[j] = static_cast<unsigned char>(base64_chars.find(char_array_4[j]));
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (j = 0; (j < i - 1); j++)
ret.push_back(char_array_3[j]);
}
return ret;
}
FILE* Utils::portable_popen(const std::string& command, const std::string& mode)
{
#ifdef _WIN32
const std::string dos_command = Utils::replaceAll(command, "/", "\\");
return _popen(dos_command.c_str(), mode.c_str());
#else
return popen(command.c_str(), mode.c_str());
#endif
}
int Utils::portable_pclose(FILE* fp)
{
int status = 0;
#ifdef _WIN32
status = _pclose(fp);
#else
status = pclose(fp);
if (status == -1)
{
throw std::runtime_error("Error closing pipe for subprocess");
}
if (WIFEXITED(status) != 0)
{
status = WEXITSTATUS(status);
}
else
{
status = 0;
}
#endif
return status;
}
int Utils::run_shell_command(const std::string& cmd, std::string& output)
{
const int maxbuf = 4096;
char buf[maxbuf];
output = "";
FILE* fp = portable_popen(cmd.c_str(), "r");
if (fp == NULL)
return 1;
while (!feof(fp))
{
if (fgets(buf, maxbuf, fp) == NULL)
{
if (feof(fp)) break;
if (ferror(fp)) break;
}
output += buf;
}
return portable_pclose(fp);
}
std::string Utils::replaceAll(std::string result,
const std::string& replaceWhat, const std::string& replaceWithWhat)
{
size_t pos = 0;
while (1)
{
pos = result.find(replaceWhat, pos);
if (pos == std::string::npos)
break;
result.replace(pos, replaceWhat.size(), replaceWithWhat);
pos += replaceWithWhat.size();
if (pos >= result.size())
break;
}
return result;
}
std::string Utils::escapeJSON(const std::string &str)
{
std::string s(str);
std::array<std::string, 35> replacements {
{ "\\u0000", "\\u0001", "\\u0002", "\\u0003", "\\u0004",
"\\u0005", "\\u0006", "\\u0007", "\\u0008", "\\t",
"\\n", "\\b", "\\f", "\\r", "\\u000E",
"\\u000F", "\\u0010", "\\u0011", "\\0012", "\\u0013",
"\\u0014", "\\u0015", "\\u0016", "\\u0017", "\\u0018",
"\\u0019", "\\u001A", "\\u001B", "\\u001C", "\\u001D",
"\\u001E", "\\u001F", " ", "!", "\\\"" }
};
for (std::string::size_type i = 0; i < s.size();)
{
unsigned char val = s[i];
if (val < replacements.size())
{
s.replace(i, 1, replacements[val]);
i += replacements[val].size();
}
else if (val == '\\')
{
s.replace(i, 1, "\\\\");
i += 2;
}
else
i++;
}
return s;
}
StringList Utils::wordWrap(std::string const& s, size_t lineLength,
size_t firstLength)
{
std::vector<std::string> output;
if (s.empty())
return output;
if (firstLength == 0)
firstLength = lineLength;
size_t len = firstLength;
std::istringstream iss(s);
std::string line;
do
{
std::string word;
iss >> word;
if ((line.length() + word.length() > len) && line.length())
{
trimTrailing(line);
output.push_back(line);
len = lineLength;
line.clear();
}
while (word.length() > len)
{
output.push_back(word.substr(0, len));
word = word.substr(len);
len = lineLength;
}
line += word + " ";
} while (iss);
trimTrailing(line);
if (!line.empty())
output.push_back(line);
return output;
}
StringList Utils::wordWrap2(std::string const& s, size_t lineLength,
size_t firstLength)
{
std::vector<std::string> output;
if (s.empty())
return output;
if (firstLength == 0)
firstLength = lineLength;
auto pushWord = [&s, &output](size_t start, size_t end)
{
if (start != end)
output.push_back(s.substr(start, end - start + 1));
};
size_t len = firstLength;
size_t startPos = 0;
while (true)
{
size_t endPos = (std::min)(startPos + len - 1, s.size() - 1);
if (endPos + 1 == s.size())
{
pushWord(startPos, endPos);
return output;
}
size_t pos = endPos;
while (pos > startPos)
{
if (std::isspace(s[pos]) && !std::isspace(s[pos + 1]))
{
endPos = pos;
break;
}
pos--;
}
pushWord(startPos, endPos);
len = lineLength;
startPos = endPos + 1;
}
return output;
}
/// Demangle strings using the compiler-provided demangle function.
/// \param[in] s String to be demangled.
/// \return Demangled string
std::string Utils::demangle(const std::string& s)
{
#ifndef _WIN32
int status;
std::unique_ptr<char[], void (*)(void*)> result(
abi::__cxa_demangle(s.c_str(), 0, 0, &status), std::free);
if (status == 0)
return std::string(result.get());
#endif
return s;
}
int Utils::screenWidth()
{
#ifdef _WIN32
return 80;
#else
struct winsize ws;
if (ioctl(0, TIOCGWINSZ, &ws))
return 80;
return ws.ws_col;
#endif
}
std::string Utils::escapeNonprinting(const std::string& s)
{
std::string out;
for (size_t i = 0; i < s.size(); ++i)
{
if (s[i] == '\n')
out += "\\n";
else if (s[i] == '\a')
out += "\\a";
else if (s[i] == '\b')
out += "\\b";
else if (s[i] == '\r')
out += "\\r";
else if (s[i] == '\v')
out += "\\v";
else if (s[i] < 32)
{
std::stringstream oss;
oss << std::hex << std::setfill('0') << std::setw(2) << (int)s[i];
out += "\\x" + oss.str();
}
else
out += s[i];
}
return out;
}
double Utils::normalizeLongitude(double longitude)
{
longitude = fmod(longitude, 360.0);
if (longitude <= -180)
longitude += 360;
else if (longitude > 180)
longitude -= 360;
return longitude;
}
// Useful for debug on occasion.
std::string Utils::hexDump(const char *buf, size_t count)
{
const unsigned char *cp = reinterpret_cast<const unsigned char *>(buf);
char foo[80];
int bytes, i, address = 0;
std::string out;
bytes = (count > 16) ? 16 : count;
while (bytes) {
sprintf(foo, "0x%06x ", address);
address += 16;
for (i = 0; i < 16; i++) {
if (i < bytes) {
sprintf(foo, "%02X ", cp[i]);
out += foo;
}
else
out += " ";
}
out += "|";
for (i = 0; i < bytes; i++) {
sprintf(foo, "%c", isprint(cp[i]) ? cp[i] : '.');
out += foo;
}
out += "|\n";
count -= bytes;
cp += bytes;
bytes = (count > 16) ? 16 : count;
}
return (out);
}
std::vector<std::string> Utils::simpleWordexp(const std::string& cmdline)
{
std::string temp;
bool instring = false;
bool escape = false;
std::vector<std::string> cmdArgs;
for (size_t i = 0; i < cmdline.size(); ++i)
{
if (instring)
{
if (escape)
{
if (cmdline[i] != '"' && cmdline[i] != '\\')
temp += '\\';
escape = false;
temp += cmdline[i];
}
else if (cmdline[i] == '"')
instring = false;
else if (cmdline[i] == '\\')
escape = true;
else
temp += cmdline[i];
}
else
{
if (escape)
{
escape = false;
temp += cmdline[i];
}
else if (cmdline[i] == '"')
instring = true;
else if (cmdline[i] == '\\')
escape = true;
else if (std::isspace(cmdline[i]))
{
if (temp.size())
{
cmdArgs.push_back(temp);
temp.clear();
}
}
else
temp += cmdline[i];
}
}
if (!instring && temp.size())
cmdArgs.push_back(temp);
return cmdArgs;
}
} // namespace pdal