forked from AdrianEddy/AIMPYouTube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTools.cpp
175 lines (152 loc) · 5.64 KB
/
Tools.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
#include "Tools.h"
#include <windows.h>
#include <locale>
#include <sstream>
#include <iomanip>
#include <codecvt>
#include <cctype>
#include <string>
#include <algorithm>
static std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> wStrConverter;
std::wstring Tools::ToWString(const std::string &string) {
return wStrConverter.from_bytes(string);
}
std::wstring Tools::ToWString(const char *string) {
return wStrConverter.from_bytes(string);
}
std::wstring Tools::ToWString(const rapidjson::Value &val) {
if (val.IsString() && val.GetStringLength() > 0) {
const char *ptr = val.GetString();
return wStrConverter.from_bytes(ptr, ptr + val.GetStringLength());
}
return std::wstring();
}
std::string Tools::ToString(const std::wstring &string) {
return wStrConverter.to_bytes(string);
}
void Tools::OutputLastError() {
DWORD errorMessageID = ::GetLastError();
if (errorMessageID == 0)
return;
LPWSTR messageBuffer = nullptr;
size_t size = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR)&messageBuffer, 0, NULL);
DebugW(L"0x%x: %s", errorMessageID, messageBuffer);
LocalFree(messageBuffer);
}
std::wstring Tools::UrlEncode(const std::wstring &url) {
std::wostringstream escaped;
escaped.fill('0');
escaped << std::hex;
for (std::wstring::const_iterator i = url.begin(), n = url.end(); i != n; ++i) {
const std::wstring::value_type c = (*i);
if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') {
escaped << c;
continue;
}
escaped << '%' << std::setw(2) << int((unsigned char)c);
}
return escaped.str();
}
char letter_to_hex(char ch) {
return isdigit(ch) ? ch - '0' : tolower(ch) - 'a' + 10;
}
std::string Tools::UrlDecode(const std::string &input) {
std::string decoded;
auto it = input.begin();
auto out = std::back_inserter(decoded);
while (it != input.end()) {
if (*it == '%') {
++it;
auto v0 = letter_to_hex(*it);
++it;
auto v1 = letter_to_hex(*it);
++it;
*out++ = 0x10 * v0 + v1;
} else if (*it == '+') {
*out++ = ' ';
++it;
} else {
*out++ = *it++;
}
}
return decoded;
}
std::wstring Tools::TrackIdFromUrl(const std::wstring &url) {
std::wstring id;
std::wstring::size_type pos, pos_end;
if (url.find(L"youtube.com") != std::wstring::npos) {
if ((pos = url.find(L"?v=")) != std::wstring::npos) {
id = url.c_str() + pos + 3;
} else if ((pos = url.find(L"&v=")) != std::wstring::npos) {
id = url.c_str() + pos + 3;
} else {
return id;
}
if ((pos = id.find(L'&')) != std::wstring::npos)
id.resize(pos);
return id;
} else if (url.find(L"youtu.be") != std::wstring::npos) {
if ((pos = url.find(L"/", 8)) != std::wstring::npos) {
id = url.c_str() + pos + 1;
if ((pos = id.find(L'?')) != std::wstring::npos)
id.resize(pos);
if ((pos = id.find(L'&')) != std::wstring::npos)
id.resize(pos);
return id;
}
} else if (url.find(L"googleapis.com/youtube/v3") != std::wstring::npos) {
if ((pos = url.find(L"&id=")) != std::wstring::npos) {
id = url.c_str() + pos + 4;
} else if ((pos = url.find(L"?id=")) != std::wstring::npos) {
id = url.c_str() + pos + 4;
} else {
return id;
}
if ((pos = id.find(L'&')) != std::wstring::npos)
id.resize(pos);
} else if ((pos = url.find(L"youtube://")) != std::wstring::npos) {
pos += 10;
if ((pos_end = url.find(L"/", pos)) != std::wstring::npos) {
return url.substr(pos, pos_end - pos);
} else {
return url.substr(pos);
}
}
return id;
}
void Tools::ReplaceString(const std::string &search, const std::string &replace, std::string &subject) {
size_t pos = 0;
while ((pos = subject.find(search, pos)) != std::string::npos) {
subject.replace(pos, search.length(), replace);
pos += replace.length();
}
}
void Tools::SplitString(const std::string &s, const std::string &delimiter, std::function<void(const std::string &token)> callback) {
auto start = 0U;
auto end = s.find(delimiter);
while (end != std::string::npos) {
callback(s.substr(start, end - start));
start = end + delimiter.length();
end = s.find(delimiter, start);
}
callback(s.substr(start, end));
}
std::string Tools::Trim(const std::string &s) {
auto wsfront = std::find_if_not(s.begin() , s.end(), [](int c) { return std::isspace(c); });
auto wsback = std::find_if_not(s.rbegin(), s.rend(), [](int c) { return std::isspace(c); }).base();
return (wsback <= wsfront ? std::string() : std::string(wsfront, wsback));
}
Config::TrackInfo *Tools::TrackInfo(const std::wstring &id) {
if (!id.empty()) {
if (Config::TrackInfos.find(id) == Config::TrackInfos.end()) {
if (!Config::ResolveTrackInfo(id))
return nullptr;
}
return &Config::TrackInfos[id];
}
return nullptr;
}
Config::TrackInfo *Tools::TrackInfo(IAIMPString *FileName) {
return TrackInfo(Tools::TrackIdFromUrl(FileName->GetData()));
}