-
Notifications
You must be signed in to change notification settings - Fork 432
/
Utils.hpp
383 lines (336 loc) · 12.9 KB
/
Utils.hpp
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
/******************************************************************************
* Copyright (c) 2011, Michael P. Gerlek (mpg@flaxen.com)
*
* 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.
****************************************************************************/
#pragma once
#include <pdal/pdal_internal.hpp>
#include <algorithm>
#include <string>
#include <cassert>
#include <cctype>
#include <stdexcept>
#include <cmath>
#include <fstream>
#include <istream>
#include <limits>
#include <cstring>
#include <sstream>
#include <vector>
#include <map>
#include <boost/numeric/conversion/cast.hpp>
namespace pdal
{
namespace Utils
{
template<typename T>
char *as_buffer(T& data)
{ return static_cast<char*>(static_cast<void*>(&data)); }
template<typename T>
char *as_buffer(T* data)
{ return static_cast<char*>(static_cast<void*>(data)); }
template <typename C, typename T>
bool check_stream_state(std::basic_ios<C, T>& srtm)
{
// Test stream state bits
if (srtm.eof())
throw std::out_of_range("end of file encountered");
else if (srtm.fail())
throw std::runtime_error("non-fatal I/O error occured");
else if (srtm.bad())
throw std::runtime_error("fatal I/O error occured");
return true;
}
PDAL_DLL void random_seed(unsigned int seed);
PDAL_DLL double random(double minimum, double maximum);
PDAL_DLL double uniform(const double& minimum=0.0f,
const double& maximum=1.0f, uint32_t seed=0);
PDAL_DLL double normal(const double& mean=0.0f, const double& sigma=1.0f,
uint32_t seed=0);
// compares two values to within a given tolerance
// the value |tolerance| is compared to |actual - expected|
template<class T>
bool compare_approx(const T& actual, const T& expected, const T& tolerance)
{
double diff = std::abs((double)actual - (double)expected);
return diff <= std::abs((double) tolerance);
}
// compares two values to within the datatype's epsilon
template<class T>
bool compare_distance(const T& actual, const T& expected)
{
const T epsilon = std::numeric_limits<T>::epsilon();
return compare_approx<T>(actual, expected, epsilon);
}
// From http://stackoverflow.com/questions/485525/round-for-float-in-c
inline double sround(double r)
{ return (r > 0.0) ? floor(r + 0.5) : ceil(r - 0.5); }
inline std::vector<uint8_t>hex_string_to_binary(
std::string const& source)
{
// Stolen from http://stackoverflow.com/questions/7363774/ ...
// c-converting-binary-data-to-a-hex-string-and-back
static int nibbles[] =
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0,
10, 11, 12, 13, 14, 15 };
std::vector<unsigned char> retval;
for (auto it = source.begin(); it < source.end(); it += 2) {
unsigned char v = 0;
if (::isxdigit(*it))
v = (unsigned char)nibbles[::toupper(*it) - '0'] << 4;
if (it + 1 < source.end() && ::isxdigit(*(it + 1)))
v += (unsigned char)nibbles[::toupper(*(it + 1)) - '0'];
retval.push_back(v);
}
return retval;
}
inline void binary_to_hex_stream(unsigned char* source,
std::ostream& destination, int start, int end )
{
static char syms[] = "0123456789ABCDEF";
for (int i = start; i != end; i++)
destination << syms[((source[i] >> 4) & 0xf)] <<
syms[source[i] & 0xf];
}
inline std::string binary_to_hex_string(
const std::vector<unsigned char>& source)
{
static char syms[] = "0123456789ABCDEF";
std::stringstream ss;
for (std::vector<unsigned char>::const_iterator it = source.begin();
it != source.end(); it++)
ss << syms[((*it >> 4) & 0xf)] << syms[*it & 0xf];
return ss.str();
}
inline std::string tolower(const std::string& s)
{
std::string out;
for (size_t i = 0; i < s.size(); ++i)
out += (char)std::tolower(s[i]);
return out;
}
inline std::string toupper(const std::string& s)
{
std::string out;
for (size_t i = 0; i < s.size(); ++i)
out += (char)std::toupper(s[i]);
return out;
}
inline bool iequals(const std::string& s, const std::string& s2)
{
if (s.length() != s2.length())
return false;
for (size_t i = 0; i < s.length(); ++i)
if (std::toupper(s[i]) != std::toupper(s2[i]))
return false;
return true;
}
inline bool startsWith(const std::string& s, const std::string& prefix)
{
if (prefix.size() > s.size())
return false;
return (strncmp(prefix.data(), s.data(), prefix.size()) == 0);
}
inline int cksum(char *buf, size_t size)
{
int i = 0;
while (size--)
i += *buf++;
return i;
}
template<typename Target, typename Source>
Target saturation_cast(Source const& src)
{
try
{
return boost::numeric_cast<Target>(src);
}
catch (boost::numeric::negative_overflow const&)
{
return std::numeric_limits<Target>::min();
}
catch (boost::numeric::positive_overflow const&)
{
return std::numeric_limits<Target>::max();
}
}
PDAL_DLL void *registerPlugin(void *stageFactoryPtr,
std::string const& filename, std::string const& registerMethodName,
std::string const& versionMethodName);
PDAL_DLL char *getenv(const char *env);
PDAL_DLL std::string getenv(std::string const& name);
PDAL_DLL int putenv(const char *env);
// aid to operator>> parsers
PDAL_DLL void eatwhitespace(std::istream& s);
PDAL_DLL void trimLeading(std::string& s);
PDAL_DLL void trimTrailing(std::string& s);
inline void trim(std::string& s)
{
trimLeading(s);
trimTrailing(s);
}
// if char found, eats it and returns true; otherwise, don't eat it and
// then return false
PDAL_DLL bool eatcharacter(std::istream& s, char x);
PDAL_DLL uint32_t getStreamPrecision(double scale);
PDAL_DLL void *getDLLSymbol(std::string const& library,
std::string const& name);
PDAL_DLL std::string base64_encode(const unsigned char *buf, size_t size);
inline std::string base64_encode(std::vector<uint8_t> const& bytes)
{ return base64_encode(bytes.data(), bytes.size()); }
PDAL_DLL std::vector<uint8_t>
base64_decode(std::string const& input);
PDAL_DLL FILE* portable_popen(const std::string& command,
const std::string& mode);
PDAL_DLL int portable_pclose(FILE* fp);
PDAL_DLL int run_shell_command(const std::string& cmd, std::string& output);
PDAL_DLL std::string replaceAll(std::string result,
const std::string& replaceWhat, const std::string& replaceWithWhat);
PDAL_DLL void wordWrap(std::string const& inputString,
std::vector<std::string>& outputString, unsigned int lineLength);
PDAL_DLL std::string escapeJSON(const std::string &s);
PDAL_DLL std::string demangle(const std::string& s);
/// Split a string into substrings. Characters matching the predicate are
/// discarded.
/// \param[in] s String to split.
/// \param[in] p Predicate returns true if a char in a string is a split
/// location.
/// \return Vector of substrings.
template<typename PREDICATE>
PDAL_DLL std::vector<std::string> split(const std::string& s, PREDICATE p)
{
std::vector<std::string> result;
if (s.empty())
return result;
auto it = s.begin();
decltype(it) endIt;
do
{
endIt = std::find_if(it, s.end(), p);
result.push_back(std::string(it, endIt));
it = endIt + 1;
} while (endIt != s.end());
return result;
}
/// Split a string into substrings. Characters matching the predicate are
/// discarded, as are empty strings otherwise produced by split().
/// \param[in] s String to split.
/// \param[in] p Predicate returns true if a char in a string is a split
/// location.
/// \return Vector of substrings.
template<typename PREDICATE>
PDAL_DLL std::vector<std::string> split2(const std::string& s, PREDICATE p)
{
std::vector<std::string> result;
if (s.empty())
return result;
auto it = s.begin();
decltype(it) endIt;
do
{
endIt = std::find_if(it, s.end(), p);
if (it != endIt)
result.push_back(std::string(it, endIt));
it = endIt + 1;
} while (endIt != s.end());
return result;
}
inline PDAL_DLL std::vector<std::string>
split(const std::string& s, char tChar)
{
auto pred = [tChar](char c){ return(c == tChar); };
return split(s, pred);
}
inline PDAL_DLL std::vector<std::string>
split2(const std::string& s, char tChar)
{
auto pred = [tChar](char c){ return(c == tChar); };
return split2(s, pred);
}
template<typename T>
std::string typeidName()
{ return Utils::demangle(typeid(T).name()); }
template<typename KEY, typename VALUE>
bool contains(const std::map<KEY, VALUE>& c, const KEY& v)
{ return c.find(v) != c.end(); }
template<typename COLLECTION, typename VALUE>
bool contains(const COLLECTION& c, const VALUE& v)
{ return (std::find(c.begin(), c.end(), v) != c.end()); }
struct RedirectStream
{
std::ofstream *m_out;
std::streambuf *m_buf;
};
/// Redirect a stream to some file, by default /dev/null.
/// \param[in] out Stream to redirect.
/// \param[in] file Name of file where stream should be redirected.
/// \return Context for stream restoration (see restore()).
inline RedirectStream redirect(std::ostream& out,
const std::string& file = "/dev/null")
{
RedirectStream redir;
redir.m_out = new std::ofstream(file);
redir.m_buf = out.rdbuf();
out.rdbuf(redir.m_out->rdbuf());
return redir;
}
/// Restore a stream redirected with redirect().
/// \param[in] out Stream to be restored.
/// \param[in] redir RedirectStream returned from corresponding
/// redirect() call.
inline void restore(std::ostream& out, RedirectStream redir)
{
out.rdbuf(redir.m_buf);
redir.m_out->close();
}
// Determine whether a value of a given input type may be safely
// statically casted to the given output type without over/underflow. If
// the output type is integral, inRange() will determine whether the
// rounded input value, rather than truncated, may be safely converted.
template<typename T_OUT>
bool inRange(double in)
{
if (std::is_integral<T_OUT>::value)
{
in = sround(in);
}
return std::is_same<double, T_OUT>::value ||
(in >= static_cast<double>(std::numeric_limits<T_OUT>::lowest()) &&
in <= static_cast<double>(std::numeric_limits<T_OUT>::max()));
}
template<typename T_IN, typename T_OUT>
bool inRange(T_IN in)
{
return std::is_same<T_IN, T_OUT>::value ||
inRange<T_OUT>(static_cast<double>(in));
}
} // namespace Utils
} // namespace pdal