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
// Copyright (c) 2012-2017 VideoStitch SAS
// Copyright (c) 2018 stitchEm
#include "strutils.hpp"
#include "libvideostitch/logging.hpp"
#ifdef _MSC_VER
#include <codecvt>
#endif
#include <fstream>
#include <sstream>
#include <iostream>
#include <regex>
std::string _vs_put_time(const std::tm* tmb, const char* fmt) {
char foo[256];
return (0 < std::strftime(foo, sizeof(foo), fmt, tmb)) ? std::string(foo) : "";
}
namespace VideoStitch {
namespace Util {
std::unique_ptr<std::istream, IStreamDeleter> createIStream(const std::string& filename, std::ios_base::openmode mode) {
std::unique_ptr<std::istream, IStreamDeleter> ifs;
#ifdef _MSC_VER
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
std::wstring wideFilename;
try {
wideFilename = converter.from_bytes(filename);
} catch (std::range_error) {
ifs.reset(new std::ifstream(wideFilename, mode));
return ifs;
}
ifs.reset(new std::ifstream(filename, mode));
#else
ifs.reset(new std::ifstream(filename, mode));
#endif
return ifs;
}
bool splitOnce(const char* str, char delim, std::string* first, std::string* second) {
for (const char* p = str; *p != '\0'; ++p) {
if (*p == delim) {
first->assign(str, p - str);
second->assign(p + 1);
return true;
}
}
first->clear();
second->clear();
return false;
}
void split(const char* str, char delim, std::vector<std::string>* res) {
const char* lastP = str;
for (const char* p = str; *p != '\0'; ++p) {
if (*p == delim) {
res->push_back(std::string(lastP, p - lastP));
lastP = p + 1;
}
}
res->push_back(std::string(lastP));
}
void splitWithComma(const std::string& text, std::vector<std::string>& out) {
size_t start = 0, end = 0;
while ((end = text.find(',', start)) != std::string::npos) {
out.push_back(text.substr(start, end - start));
start = end + 1;
}
out.push_back(text.substr(start));
}
std::string escapeStr(const std::string& in) {
std::string res;
for (std::string::const_iterator it = in.begin(); it != in.end(); ++it) {
switch (*it) {
case '"':
res.push_back('\\');
res.push_back('"');
break;
case '\\':
res.push_back('\\');
res.push_back('\\');
break;
case '\n':
res.push_back('\\');
res.push_back('n');
break;
case '\t':
res.push_back('\\');
res.push_back('t');
break;
case '\r':
res.push_back('\\');
res.push_back('r');
break;
case '\b':
res.push_back('\\');
res.push_back('b');
break;
case '\f':
res.push_back('\\');
res.push_back('f');
break;
default:
res.push_back(*it);
break;
}
}
return res;
}
namespace {
/**
* Decodes a hex value into a decimal value.
* @param hex Two hex digits
* @param out where the output is written.
* @returns false on error.
*/
bool decodeHexChar(const char hex, int& out) {
if ('0' <= hex && hex <= '9') {
out = (int)(hex - '0');
return true;
} else if ('a' <= hex && hex <= 'f') {
out = (int)(10 + hex - 'a');
return true;
}
out = 0;
return false;
}
/**
* Decodes a hex-encoded codepoint.
* @param hex Four hex digits
* @param codepoint where the output is written.
* @returns false on error.
*/
bool decodeAsciiToUnicode(const char* hex, uint16_t& codepoint) {
codepoint = 0;
int comp[4];
for (int i = 0; i < 4; ++i) {
if (!decodeHexChar(hex[i], comp[i])) {
return false;
}
}
codepoint = (uint16_t)((comp[0] << 12) | (comp[1] << 8) | (comp[2] << 4) | comp[3]);
return true;
}
} // namespace
bool unescapeStr(const std::string& in, std::string& res) {
res.clear();
for (std::string::const_iterator it = in.begin(); it != in.end(); ++it) {
if (*it == '\\') {
++it;
if (it == in.end()) {
Logger::get(Logger::Error) << "Invalid escape sequence at end of string literal." << std::endl;
res.clear();
return false;
}
switch (*it) {
case '"':
res.push_back('"');
break;
case '\\':
res.push_back('\\');
break;
case '/':
res.push_back('/');
break;
case 'n':
res.push_back('\n');
break;
case 't':
res.push_back('\t');
break;
case 'r':
res.push_back('\r');
break;
case 'b':
res.push_back('\b');
break;
case 'f':
res.push_back('\f');
break;
case 'u': // Escaping in json is \uc3a9
if (in.size() - (it - in.begin()) <= 4) {
Logger::get(Logger::Error) << "Invalid JSON escape sequence at end of string literal." << std::endl;
return false;
} else {
char hex[4];
hex[0] = *(++it);
hex[1] = *(++it);
hex[2] = *(++it);
hex[3] = *(++it);
uint16_t unicode = 0;
if (!(decodeAsciiToUnicode(hex, unicode))) {
Logger::get(Logger::Error) << "Invalid JSON escape sequence '\\u" << hex[0] << hex[1] << hex[2] << hex[3]
<< "'" << std::endl;
return false;
}
unicodeToUtf8(unicode, res);
}
break;
default:
Logger::get(Logger::Error) << "Invalid escape sequence '\\" << *it << "'" << std::endl;
res.clear();
return false;
}
} else {
res.push_back(*it);
}
}
return true;
}
bool parseHtmlColor(const std::string& str, uint32_t& color) {
std::istringstream iss(str);
if (str.size() == 6) {
iss >> std::hex >> color;
// Color is RGB, convert to ABGR solid.
color = ((color & (uint32_t)0xff) << 16) | ((color & (uint32_t)0xff00)) | ((color & (uint32_t)0xff0000) >> 16) |
(uint32_t)0xff000000;
return true;
} else if (str.size() == 8) {
iss >> std::hex >> color;
// Color is RGBA, convert to ABGR.
color = ((color & (uint32_t)0xff) << 24) | ((color & (uint32_t)0xff00) << 8) | ((color & (uint32_t)0xff0000) >> 8) |
((color & (uint32_t)0xff000000) >> 24);
return true;
}
return false;
}
void unicodeToUtf8(uint16_t codepoint, std::string& sink) {
if (codepoint < 0x0080) {
sink.push_back((char)codepoint);
} else if (codepoint < 0x07ff) {
sink.push_back((char)(0xc0 | ((codepoint & 0x07c0) >> 6)));
sink.push_back((char)(0x80 | ((codepoint & 0x003f))));
} else {
sink.push_back((char)(0xe0 | ((codepoint & 0xf000) >> 12)));
sink.push_back((char)(0x80 | ((codepoint & 0x0fc0) >> 6)));
sink.push_back((char)(0x80 | ((codepoint & 0x003f))));
}
}
} // namespace Util
} // namespace VideoStitch