Debian Codegraph @

src/Utils.cpp

raw
1 /*
2 * taken from pvr:zattoo
3 */
4
5 #include "Utils.h"
6
7 #include "kodi/Filesystem.h"
8 #include "kodi/General.h"
9 #include "kodi/tools/StringUtils.h"
10
11 #include <algorithm>
12 #include <chrono>
13 #include <iomanip>
14 #include <iostream>
15 #include <iterator>
16 #include <sstream>
17
18 std::string Utils::GetFilePath(std::string strPath, bool bUserPath)
19 {
20 return (bUserPath ? kodi::addon::GetUserPath(strPath) : kodi::addon::GetAddonPath(strPath));
21 }
22
23 // http://stackoverflow.com/a/17708801
24 std::string Utils::UrlEncode(const std::string& value)
25 {
26 std::ostringstream escaped;
27 escaped.fill('0');
28 escaped << std::hex;
29
30 for (char c : value)
31 {
32 // Keep alphanumeric and other accepted characters intact
33 if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~')
34 {
35 escaped << c;
36 continue;
37 }
38
39 // Any other characters are percent-encoded
40 escaped << '%' << std::setw(2) << int((unsigned char)c);
41 }
42
43 return escaped.str();
44 }
45
46 std::string Utils::ReadFile(const std::string& path)
47 {
48 kodi::vfs::CFile file;
49 file.CURLCreate(path);
50 if (!file.CURLCreate(path) || !file.CURLOpen(0))
51 {
52 kodi::Log(ADDON_LOG_ERROR, "Failed to open file [%s].", path.c_str());
53 return "";
54 }
55
56 char buf[1025];
57 ssize_t nbRead;
58 std::string content;
59 while ((nbRead = file.Read(buf, 1024)) > 0)
60 {
61 buf[nbRead] = 0;
62 content.append(buf);
63 }
64
65 return content;
66 }
67
68 time_t Utils::StringToTime(std::string timeString)
69 {
70 // expected timeString "2019-01-20T15:40:00+0100"
71 struct tm tm
72 {
73 };
74
75 int year, month, day, h, m, s, tzh, tzm;
76 if (sscanf(timeString.c_str(), "%d-%d-%dT%d:%d:%d%d", &year, &month, &day, &h, &m, &s, &tzh) < 7)
77 {
78 tzh = 0;
79 }
80 tzm = tzh % 100;
81 tzh = tzh / 100;
82
83 tm.tm_year = year - 1900;
84 tm.tm_mon = month - 1;
85 tm.tm_mday = day;
86 tm.tm_hour = h - tzh;
87 tm.tm_min = m - tzm;
88 tm.tm_sec = s;
89
90 time_t ret = timegm(&tm);
91 return ret;
92 }
93
94 std::string Utils::TimeToString(const time_t time)
95 {
96 char time_str[21] = "";
97 std::tm* pstm = std::localtime(&time);
98 // 2019-01-20T23:59:59
99 std::strftime(time_str, sizeof(time_str), "%Y-%m-%dT%H:%M:%S", pstm);
100 return time_str;
101 }
102
103 int Utils::GetIDDirty(std::string str)
104 {
105 // str= "_1035245078" or = "misc-rand-int-whatever"
106 if (str.rfind("_", 0) == 0)
107 {
108 // str starts with _
109 return StringToInt(kodi::tools::StringUtils::TrimLeft(str, "\t\n\v\f\r _"), 1);
110 }
111 // fallback to deterministic int
112 return Hash(str);
113 }
114
115 int Utils::Hash(const std::string& str)
116 {
117 const char* s = str.c_str();
118
119 int hash = 0;
120 while (*s)
121 hash = ((hash << 5) + hash) + *s++;
122
123 return std::abs(hash);
124 }
125
126 int Utils::StringToInt(std::string str, int defaultValue)
127 {
128 try
129 {
130 return std::stoi(str);
131 }
132 catch (std::exception& e)
133 {
134 return defaultValue;
135 }
136 }
137
138 std::string Utils::Replace(std::string str, const std::string& from, const std::string& to)
139 {
140 // replaces the first occurrence
141 // taken from: https://stackoverflow.com/questions/3418231/replace-part-of-a-string-with-another-string
142 size_t start_pos = str.find(from);
143 if (start_pos != std::string::npos)
144 str.replace(start_pos, from.length(), to);
145 return str;
146 }
147
148 std::string Utils::CreateUUID()
149 {
150 // taken from pvr.dvblink
151 using namespace std::chrono;
152
153 std::string uuid;
154 int64_t seed_value =
155 duration_cast<milliseconds>(
156 time_point_cast<milliseconds>(high_resolution_clock::now()).time_since_epoch())
157 .count();
158 seed_value = seed_value % 1000000000;
159 srand((unsigned int)seed_value);
160
161 //fill in uuid string from a template
162 std::string template_str = "xxxxxxxx-xxxx-4xxx-8xxx-xxxxxxxxxxxx";
163 for (size_t i = 0; i < template_str.size(); i++)
164 {
165 if (template_str[i] == 'x')
166 {
167 double a1 = rand();
168 double a3 = RAND_MAX;
169 unsigned char ch = (unsigned char)(a1 * 15 / a3);
170 char buf[8];
171 sprintf(buf, "%x", ch);
172 uuid += buf;
173 }
174 else
175 {
176 uuid += template_str[i];
177 }
178 }
179 return uuid;
180 }
181
182 bool Utils::FileDownload(std::string source, std::string target)
183 {
184 kodi::vfs::CFile inputFile;
185 if (inputFile.OpenFile(source, ADDON_READ_NO_CACHE))
186 {
187 kodi::vfs::CFile outputFile;
188 if (outputFile.OpenFileForWrite(target, true))
189 {
190 char buffer[1024];
191 int bytesRead = 0;
192 while ((bytesRead = inputFile.Read(buffer, sizeof(buffer) - 1)) > 0)
193 {
194 outputFile.Write(buffer, bytesRead);
195 }
196 inputFile.Close();
197 outputFile.Close();
198 return true;
199 }
200 }
201 return false;
202 }
203
204 bool Utils::CheckInputstreamInstalledAndEnabled(const std::string& inputstreamName)
205 {
206 std::string version;
207 bool enabled;
208
209 if (kodi::IsAddonAvailable(inputstreamName, version, enabled))
210 {
211 if (!enabled)
212 {
213 std::string message = kodi::tools::StringUtils::Format(
214 kodi::addon::GetLocalizedString(30502).c_str(), inputstreamName.c_str());
215 kodi::QueueNotification(QueueMsg::QUEUE_ERROR, kodi::addon::GetLocalizedString(30500),
216 message);
217 return false;
218 }
219 }
220 else // Not installed
221 {
222 std::string message = kodi::tools::StringUtils::Format(
223 kodi::addon::GetLocalizedString(30501).c_str(), inputstreamName.c_str());
224 kodi::QueueNotification(QueueMsg::QUEUE_ERROR, kodi::addon::GetLocalizedString(30500), message);
225 return false;
226 }
227
228 return true;
229 }