/
Utils.cpp
220 lines (195 loc) · 5.14 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
//-----------------------------------------------------------------------------
//
// Utils.h
//
// Miscellaneous helper functions
//
// Copyright (c) 2010 Mal Lansell <openzwave@lansell.org>
//
// SOFTWARE NOTICE AND LICENSE
//
// This file is part of OpenZWave.
//
// OpenZWave is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// OpenZWave is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenZWave. If not, see <http://www.gnu.org/licenses/>.
//
//-----------------------------------------------------------------------------
#include "Defs.h"
#include "Utils.h"
#include <functional>
using namespace OpenZWave;
//-----------------------------------------------------------------------------
// <OpenZWave::ToUpper>
// Convert a string to all upper-case.
//-----------------------------------------------------------------------------
string OpenZWave::ToUpper
(
string const& _str
)
{
string upper = _str;
transform( upper.begin(), upper.end(), upper.begin(), ::toupper );
return upper;
}
//-----------------------------------------------------------------------------
// <OpenZWave::ToLower>
// Convert a string to all lower-case.
//-----------------------------------------------------------------------------
string OpenZWave::ToLower
(
string const& _str
)
{
string lower = _str;
transform( lower.begin(), lower.end(), lower.begin(), ::tolower );
return lower;
}
//-----------------------------------------------------------------------------
// <OpenZWave::trim>
// Remove WhiteSpaces from the begining and end of a string
//-----------------------------------------------------------------------------
std::string &OpenZWave::removewhitespace
(
std::string &s
)
{
if(s.size() == 0)
{
return s;
}
int val = 0;
for (size_t cur = 0; cur < s.size(); cur++)
{
if(s[cur] != ' ' && isalnum(s[cur]))
{
s[val] = s[cur];
val++;
}
}
s.resize(val);
return s;
}
std::string& OpenZWave::ltrim(std::string& s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(),
[](unsigned char c){ return isgraph(c); }));
return s;
}
std::string& OpenZWave::rtrim(std::string& s) {
s.erase(std::find_if(s.rbegin(), s.rend(),
[](unsigned char c){ return isgraph(c); }).base(), s.end());
return s;
}
std::string& OpenZWave::trim(std::string& s) {
return OpenZWave::ltrim(OpenZWave::rtrim(s));
}
//-----------------------------------------------------------------------------
// <OpenZWave::split>
// Split a String into a vector, seperated by anything specified in seperators.
//-----------------------------------------------------------------------------
void OpenZWave::split
(
std::vector<std::string>& lst,
const std::string& input,
const std::string& separators,
bool remove_empty
)
{
std::ostringstream word;
for (size_t n = 0; n < input.size(); ++n)
{
if (std::string::npos == separators.find(input[n]))
word << input[n];
else
{
if (!word.str().empty() || !remove_empty)
lst.push_back(word.str());
word.str("");
}
}
if (!word.str().empty() || !remove_empty)
lst.push_back(word.str());
}
void OpenZWave::PrintHex(std::string prefix, uint8_t const *data, uint32 const length) {
Log::Write(LogLevel_Info, "%s: %s", prefix.c_str(), PktToString(data, length).c_str());
}
string OpenZWave::PktToString(uint8 const *data, uint32 const length) {
char byteStr[5];
std::string str;
for( uint32 i=0; i<length; ++i )
{
if( i )
{
str += ", ";
}
snprintf( byteStr, sizeof(byteStr), "0x%.2x", data[i] );
str += byteStr;
}
return str;
}
static const char* separators()
{
#if __unix__
return "/";
#else // __unix__
return "\\/";
#endif // __unix__
}
string OpenZWave::ozwdirname(string m_path)
{
const size_t lastSlash = m_path.find_last_of(separators());
if (lastSlash == std::string::npos)
return "";
return m_path.substr(0, lastSlash);
}
string OpenZWave::intToString( int x ) {
#if __cplusplus==201103L || __APPLE__
return to_string(x);
#else
return static_cast< std::ostringstream & >( ( std::ostringstream() << std::dec << x ) ).str();
#endif
}
const char* OpenZWave::rssi_to_string(uint8 _data) {
static char buf[8];
switch (_data) {
case 127: {
return "---";
break;
}
case 126: {
return "MAX";
break;
}
case 125: {
return "MIN";
break;
}
default:
if (_data >= 11 && _data <= 124)
{
return "UNK";
}
else
{
snprintf(buf, 5, "%4d", (unsigned int)_data - 256);
return buf;
}
}
}
#if (defined _WINDOWS || defined WIN32 || defined _MSC_VER) && (!defined MINGW && !defined __MINGW32__ && !defined __MINGW64__)
/* Windows doesn't have localtime_r - use the "secure" version instead */
struct tm *localtime_r(time_t *_clock, struct tm *_result)
{
_localtime64_s(_result, _clock);
return _result;
}
#endif