-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathmisc.cpp
More file actions
426 lines (409 loc) · 15.6 KB
/
Copy pathmisc.cpp
File metadata and controls
426 lines (409 loc) · 15.6 KB
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
#include "misc.h"
#include "strutil.h"
#include "platformutil.h"
#include "metadata.h"
#include "expand.h"
#include <cctype>
#ifdef _WIN32
#pragma warning(push)
#pragma warning(disable: 4091)
#include <shlobj.h>
#pragma warning(pop)
#else
#include <cstdlib>
#include <cerrno>
#include <iconv.h>
#endif
#include <uchardet.h>
#include <regex>
namespace misc
{
#ifdef _WIN32
int getCodePageFromCharset(const char* charset)
{
std::cmatch match;
std::regex windowscp("(?:IBM|CP|WINDOWS)-?(\\d+)");
std::regex iso8859("ISO-8859-(\\d+)");
std::regex iso2022("ISO-2022-(\\w+)");
std::regex euc("EUC-(\\w+)");
std::regex mac("MAC-(\\w+)");
if (!charset)
return -1;
if (std::strcmp(charset, "ASCII") == 0)
return 20127;
if (std::strcmp(charset, "UTF-8") == 0)
return 65001;
else if (std::strcmp(charset, "UTF-16") == 0)
return 0;
else if (std::strcmp(charset, "SHIFT_JIS") == 0)
return 932;
else if (std::strcmp(charset, "BIG5") == 0)
return 950;
else if (std::strcmp(charset, "KOI8-R") == 0)
return 20866;
else if (std::strcmp(charset, "HZ-GB-2312") == 0)
return 52936;
else if (std::strcmp(charset, "GB18030") == 0)
return 54936;
else if (std::regex_match(charset, match, windowscp))
return atoi(match[1].str().c_str());
else if (std::regex_match(charset, match, iso8859))
return 28590 + atoi(match[1].str().c_str());
else if (std::regex_match(charset, match, iso2022)) {
if (match[1].str() == "JP")
return 50220;
if (match[1].str() == "KR")
return 50225;
if (match[1].str() == "CN")
return 50227;
}
else if (std::regex_match(charset, match, euc)) {
if (match[1].str() == "JP")
return 51932;
if (match[1].str() == "KR")
return 51949;
if (match[1].str() == "TW")
return 51950;
}
else if (std::regex_match(charset, match, mac)) {
if (match[1].str() == "CYRILLIC")
return 10007;
if (match[1].str() == "CENTRALEUROPE")
return 10029;
}
return -1;
}
std::string codepage_to_utf8(int codepage, const char *data, size_t len)
{
if (codepage == 0) {
return strutil::w2us(std::wstring(
reinterpret_cast<const wchar_t*>(data), len / sizeof(wchar_t)));
}
int nc = MultiByteToWideChar(codepage, 0, data,
static_cast<int>(len), nullptr, 0);
std::vector<wchar_t> obuf(nc);
if (nc > 0)
MultiByteToWideChar(codepage, 0, data, static_cast<int>(len),
obuf.data(), nc);
return strutil::w2us(std::wstring(obuf.data(), obuf.size()));
}
#else
std::string codepageToIconvName(int codepage)
{
switch (codepage) {
case 0: return "UTF-16";
case 20127: return "ASCII";
case 65001: return "UTF-8";
case 932: return "SHIFT_JIS";
case 950: return "BIG5";
case 20866: return "KOI8-R";
case 52936: return "HZ-GB-2312";
case 54936: return "GB18030";
case 50220: return "ISO-2022-JP";
case 50225: return "ISO-2022-KR";
case 50227: return "ISO-2022-CN";
case 51932: return "EUC-JP";
case 51949: return "EUC-KR";
case 51950: return "EUC-TW";
case 10007: return "MAC-CYRILLIC";
case 10029: return "MAC-CENTRALEUROPE";
}
if (codepage >= 28591 && codepage <= 28599)
return strutil::format("ISO-8859-%d", codepage - 28590);
return strutil::format("CP%d", codepage);
}
std::string charset_to_utf8(const char *charset, const char *data, size_t len)
{
iconv_t cd = iconv_open("UTF-8", charset);
if (cd == (iconv_t)-1)
throw std::runtime_error(std::string("unsupported charset: ") + charset);
std::shared_ptr<void> cd_closer(0, [cd](void*) { iconv_close(cd); });
std::string result;
std::vector<char> outbuf(len * 4 + 16);
char *inp = const_cast<char*>(data);
size_t inleft = len;
char *outp = outbuf.data();
size_t outleft = outbuf.size();
while (inleft > 0) {
size_t rc = iconv(cd, &inp, &inleft, &outp, &outleft);
if (rc == (size_t)-1) {
if (errno == E2BIG) {
size_t used = outbuf.size() - outleft;
outbuf.resize(outbuf.size() * 2);
outp = outbuf.data() + used;
outleft = outbuf.size() - used;
continue;
}
throw std::runtime_error(
std::string("charset conversion failed (") + charset + ")");
}
}
result.assign(outbuf.data(), outbuf.size() - outleft);
return result;
}
#endif
std::string loadTextFile(const std::string &path, int codepage)
{
auto fp = std::shared_ptr<FILE>(platform::fopen(path, "rb"), std::fclose);
fseeko(fp.get(), 0, SEEK_END);
int64_t fileSize = ftello(fp.get());
fseeko(fp.get(), 0, SEEK_SET);
if (fileSize > 0x100000) {
throw std::runtime_error(path + ": file too big");
}
std::vector<char> ibuf(fileSize);
int n = std::fread(ibuf.data(), 1, fileSize, fp.get());
ibuf.resize(n);
std::string detectedCharset;
if (!codepage) {
auto detector = std::shared_ptr<uchardet>(uchardet_new(), uchardet_delete);
if (uchardet_handle_data(detector.get(), ibuf.data(), ibuf.size())) {
throw std::runtime_error(path + ": uchardet_handle_data() failed");
}
uchardet_data_end(detector.get());
auto detected = uchardet_get_charset(detector.get());
if (!detected || !*detected)
throw std::runtime_error(path + ": cannot detect code page");
detectedCharset = detected;
}
#ifdef _WIN32
int cp = codepage;
if (!cp) {
cp = getCodePageFromCharset(detectedCharset.c_str());
if (cp < 0)
throw std::runtime_error(path + ": unknown charset");
}
std::string decoded = codepage_to_utf8(cp, ibuf.data(), ibuf.size());
#else
std::string charset = codepage ? codepageToIconvName(codepage) : detectedCharset;
std::string decoded = charset_to_utf8(charset.c_str(), ibuf.data(), ibuf.size());
#endif
// chop off BOM
if (decoded.size() >= 3 &&
(unsigned char)decoded[0] == 0xef &&
(unsigned char)decoded[1] == 0xbb &&
(unsigned char)decoded[2] == 0xbf)
decoded.erase(0, 3);
return strutil::normalize_crlf(decoded.c_str(), "\n");
}
class TagLookup {
typedef std::map<std::string, std::string> meta_t;
const meta_t &tracktags;
public:
TagLookup(const meta_t &tags): tracktags(tags) {}
std::string operator()(const std::string &name) {
std::string key = TextBasedTag::normalizeTagName(name.c_str());
meta_t::const_iterator iter = tracktags.find(key);
if (iter == tracktags.end())
return "";
else if (key == "track number" || key == "DISC NUMBER") {
strutil::Tokenizer<char> tok(iter->second, "/");
unsigned n = 0;
sscanf(tok.next(), "%u", &n);
return strutil::format("%02u", n);
}
return strutil::strtransform(iter->second, [](char c)->char {
return std::strchr(":/\\?|<>*\"", c) ? '_' : c;
});
}
};
std::string generateFileName(const std::string &spec,
const std::map<std::string, std::string> &tag)
{
auto spec2 = strutil::strtransform(spec, [](char c)->char {
return c == '\\' ? '/' : c;
});
auto res = process_template(spec2, TagLookup(tag));
std::vector<std::string> comp;
strutil::Tokenizer<char> tokens(res, "/");
char *tok;
while ((tok = tokens.next())) {
std::string t(tok);
size_t b = t.find_first_not_of(" \t");
t = (b == std::string::npos) ? ""
: t.substr(b, t.find_last_not_of(" \t") - b + 1);
if (t.size() > 250) {
t.resize(250);
size_t i = t.size();
while (i > 0 && (static_cast<unsigned char>(t[i-1]) & 0xC0) == 0x80)
--i;
if (i > 0) {
unsigned char lead = static_cast<unsigned char>(t[i-1]);
size_t seqlen = (lead & 0x80) == 0 ? 1
: (lead & 0xE0) == 0xC0 ? 2
: (lead & 0xF0) == 0xE0 ? 3
: (lead & 0xF8) == 0xF0 ? 4 : 1;
if (i - 1 + seqlen > t.size())
--i;
}
t.resize(i);
}
comp.push_back(t);
}
res.clear();
for (size_t i = 0; i < comp.size() - 1; ++i)
res += comp[i] + "/";
res += comp[comp.size() - 1];
return res;
}
void add_chapter_entry(std::vector<chapter_t> &chapters,
const char *name,
int h, int m, double s)
{
std::string sname = name ? name : "";
double stamp = ((h * 60) + m) * 60 + s;
if (!chapters.size() && stamp != 0.0)
throw std::runtime_error("Non zero timestamp on the first chapter "
"entry is not allowed");
else if (chapters.size()) {
chapter_t &prev = chapters.back();
if (prev.second >= stamp)
throw std::runtime_error("Chapter timestamps is required to "
"be strictly increasing");
}
chapters.push_back(std::make_pair(sname, stamp));
}
std::vector<chapter_t> loadChapterFile(const char *path,
uint32_t codepage)
{
std::vector<chapter_t> chaps;
std::string str = misc::loadTextFile(path, codepage);
const char *tfmt = "%02d:%02d:%lf";
int h = 0, m = 0;
double s = 0.0;
strutil::Tokenizer<char> tokens(str, "\n");
char *tok;
while ((tok = tokens.next())) {
if (*tok && tok[0] == '#')
continue;
if (std::sscanf(tok, tfmt, &h, &m, &s) == 3) {
strutil::strsep(&tok, "\t ");
add_chapter_entry(chaps, tok, h, m, s);
} else if (strncmp(tok, "Chapter", 7) == 0) {
int hh, mm;
double ss;
char *key = strutil::strsep(&tok, "=");
if (std::strstr(key, "NAME"))
add_chapter_entry(chaps, tok, h, m, s);
else if (std::sscanf(tok, tfmt, &hh, &mm, &ss) == 3)
h = hh, m = mm, s = ss;
}
}
return chaps;
}
// converts absolute timestamp to time delta
std::vector<chapter_t>
convertChaptersToQT(const std::vector<chapter_t> &chapters,
double total_duration)
{
std::vector<chapter_t> result;
auto first = chapters.begin();
auto last = chapters.end();
if (first != last) {
auto prev_name = first->first;
auto prev_stamp = first->second;
for (auto it = ++first; it != last; ++it) {
double delta = it->second - prev_stamp;
result.push_back(std::make_pair(prev_name, delta));
prev_name = it->first;
prev_stamp = it->second;
}
double last_delta = total_duration - prev_stamp;
result.push_back(std::make_pair(prev_name, last_delta));
}
return result;
}
std::shared_ptr<FILE> openConfigFile(const char *file)
{
std::vector<std::string> search_paths;
#ifdef _WIN32
const wchar_t *home = _wgetenv(L"HOME");
if (home)
search_paths.push_back(
strutil::format("%s\\%s", strutil::w2us(home).c_str(), ".qaac"));
wchar_t path[MAX_PATH];
if (SUCCEEDED(SHGetFolderPathW(0, CSIDL_APPDATA, 0, 0, path)))
search_paths.push_back(
strutil::format("%s\\%s", strutil::w2us(path).c_str(), "qaac"));
search_paths.push_back(platform::get_module_directory());
const char *sep = "\\";
#else
const char *home = getenv("HOME");
if (home)
search_paths.push_back(strutil::format("%s/%s", home, ".qaac"));
const char *xdg = getenv("XDG_CONFIG_HOME");
if (xdg)
search_paths.push_back(strutil::format("%s/qaac", xdg));
else if (home)
search_paths.push_back(strutil::format("%s/.config/qaac", home));
search_paths.push_back(platform::get_module_directory());
const char *sep = "/";
#endif
for (size_t i = 0; i < search_paths.size(); ++i) {
try {
std::string pathtry =
strutil::format("%s%s%s", search_paths[i].c_str(), sep, file);
return std::shared_ptr<FILE>(platform::fopen(pathtry, "r"),
std::fclose);
} catch (...) {
if (i == search_paths.size() - 1) throw;
}
}
return 0;
}
static
std::vector<std::vector<complex_t>>
loadRemixerMatrix(std::shared_ptr<FILE> fileptr)
{
FILE *fp = fileptr.get();
int c;
std::vector<std::vector<complex_t> > matrix;
std::vector<complex_t> row;
while ((c = std::getc(fp)) != EOF) {
if (c == '\n') {
if (row.size()) {
matrix.push_back(row);
row.clear();
}
} else if (std::isspace(c)) {
while (c != '\n' && std::isspace(c = std::getc(fp)))
;
std::ungetc(c, fp);
} else if (std::isdigit(c) || c == '-') {
std::ungetc(c, fp);
double v;
if (std::fscanf(fp, "%lf", &v) != 1)
throw std::runtime_error("invalid matrix preset file");
c = std::getc(fp);
if (std::strchr("iIjJ", c))
row.push_back(complex_t(0.0, v));
else if (std::strchr("kK", c))
row.push_back(complex_t(0.0, -v));
else {
std::ungetc(c, fp);
row.push_back(complex_t(v, 0.0));
}
} else
throw std::runtime_error("invalid char in matrix preset file");
}
if (row.size())
matrix.push_back(row);
return matrix;
}
std::vector<std::vector<complex_t>>
loadRemixerMatrixFromFile(const char *path)
{
return loadRemixerMatrix(
std::shared_ptr<FILE>(platform::fopen(path, "r"), std::fclose));
}
std::vector<std::vector<complex_t>>
loadRemixerMatrixFromPreset(const char *preset_name)
{
#ifdef _WIN32
std::string path = strutil::format("matrix\\%s.txt", preset_name);
#else
std::string path = strutil::format("matrix/%s.txt", preset_name);
#endif
return loadRemixerMatrix(openConfigFile(path.c_str()));
}
}