Debian Codegraph @

src/ApvlvUtil.cc

raw
1 /*
2 * This file is part of the apvlv package
3 *
4 * Copyright (C) 2008 Alf.
5 *
6 * Contact: Alf <[email protected]>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2.0 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public
19 * License along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 */
23 /* @CPPFILE ApvlvUtil.cc
24 *
25 * Author: Alf <[email protected]>
26 */
27
28 #include <QByteArray>
29 #include <QCoreApplication>
30 #include <QDir>
31 #include <QProcessEnvironment>
32 #include <QRegularExpression>
33 #include <filesystem>
34 #include <memory>
35 #include <optional>
36 #include <string>
37
38 #include "ApvlvUtil.h"
39
40 namespace apvlv
41 {
42
43 using namespace std;
44
45 string ScriptDir;
46 string HelpPdf;
47 string IniExam;
48 string IconDir;
49 string IconFile;
50 string IconPage;
51 string Translations;
52
53 string IniFile;
54 string SessionFile;
55 string LogFile;
56 string NotesDir;
57 string UserScriptDir;
58
59 static void
60 getXdgOrHomeIni (const QString &appdir)
61 {
62 auto sysenv = QProcessEnvironment::systemEnvironment ();
63 auto xdgdir = sysenv.value ("XDG_CONFIG_DIR").toStdString ();
64 auto homedir = sysenv.value ("HOME").toStdString ();
65
66 if (homedir.empty ())
67 {
68 homedir = QDir::homePath ().toStdString ();
69 }
70
71 IniFile = appdir.toStdString () + "/.apvlvrc";
72
73 if (!xdgdir.empty ())
74 {
75 IniFile = xdgdir + "/apvlv/apvlvrc";
76 }
77 else if (!homedir.empty ())
78 {
79 IniFile = homedir + "/.config/apvlv/apvlvrc";
80 if (!filesystem::is_regular_file (IniFile))
81 {
82 IniFile = homedir + "/.apvlvrc";
83 }
84 }
85 }
86
87 static void
88 getXdgOrCachePath (const QString &appdir)
89 {
90 auto sysenv = QProcessEnvironment::systemEnvironment ();
91 auto xdgdir = sysenv.value ("XDG_CACHE_HOME").toStdString ();
92 auto homedir = sysenv.value ("HOME").toStdString ();
93 if (homedir.empty ())
94 {
95 homedir = QDir::homePath ().toStdString ();
96 }
97
98 SessionFile = appdir.toStdString () + "/apvlvinfo";
99 if (!xdgdir.empty ())
100 {
101 SessionFile = xdgdir + "/apvlvinfo";
102 }
103 else if (!homedir.empty ())
104 {
105 SessionFile = homedir + "/.cache/apvlvinfo";
106 }
107 }
108
109 void
110 getRuntimePaths ()
111 {
112 auto dirpath = QDir (QCoreApplication::applicationDirPath ());
113 dirpath.cdUp ();
114 auto prefix = dirpath.path ().toStdString ();
115
116 ScriptDir = prefix + "/share/scripts";
117
118 auto apvlvdir = prefix + "/share/doc/apvlv";
119
120 HelpPdf = apvlvdir + "/Startup.pdf";
121 IconDir = apvlvdir + "/icons/dir.png";
122 IconFile = apvlvdir + "/icons/pdf.png";
123 IconPage = apvlvdir + "/icons/reg.png";
124 Translations = apvlvdir + "/translations";
125
126 #ifndef WIN32
127 IniExam = string (SYSCONFDIR) + "/apvlvrc.example";
128 #else
129 IniExam = apvlvdir + "/apvlvrc.example";
130 #endif
131
132 getXdgOrHomeIni (dirpath.path ());
133 getXdgOrCachePath (dirpath.path ());
134
135 auto homedir = QDir::homePath ().toStdString ();
136 NotesDir = homedir + "/" + "ApvlvNotes";
137 }
138
139 optional<unique_ptr<QXmlStreamReader>>
140 xmlContentGetElement (const char *content, size_t length,
141 const vector<string> &names)
142 {
143 auto bytes = QByteArray{ content, (qsizetype)length };
144 auto xml = make_unique<QXmlStreamReader> (bytes);
145 std::ptrdiff_t state = 0;
146 while (!xml->atEnd ())
147 {
148 if (xml->isStartElement ())
149 {
150 auto name = xml->name ().toString ().toStdString ();
151 auto iter = std::ranges::find (names, name);
152 if (iter == names.end ())
153 {
154 xml->readNextStartElement ();
155 continue;
156 }
157
158 auto pos = distance (names.begin (), iter);
159 if (state == pos)
160 {
161 state++;
162 }
163
164 if (state == static_cast<ptrdiff_t> (names.size ()))
165 {
166 return xml;
167 }
168 }
169
170 xml->readNextStartElement ();
171 }
172
173 return nullopt;
174 }
175
176 string
177 xmlStreamGetAttributeValue (QXmlStreamReader *xml, const string &key)
178 {
179 auto attrs = xml->attributes ();
180 for (auto &attr : attrs)
181 {
182 if (attr.name ().toString ().toStdString () == key)
183 return attr.value ().toString ().toStdString ();
184 }
185
186 return "";
187 }
188
189 string
190 xmlContentGetAttributeValue (const char *content, size_t length,
191 const vector<string> &names, const string &key)
192 {
193 auto optxml = xmlContentGetElement (content, length, names);
194 if (!optxml)
195 return "";
196
197 auto xml = optxml->get ();
198 return xmlStreamGetAttributeValue (xml, key);
199 }
200
201 string
202 filenameExtension (const string &filename)
203 {
204 auto pointp = filename.rfind ('.');
205 if (pointp == string::npos)
206 return "";
207
208 string ext = filename.substr (pointp);
209 std::ranges::transform (ext, ext.begin (), ::tolower);
210 return ext;
211 }
212
213 void
214 imageArgb32ToRgb32 (QImage &image, int left, int top, int right, int bottom)
215 {
216 for (auto x = left; x < right; ++x)
217 {
218 for (auto y = top; y < bottom; ++y)
219 {
220 auto c = image.pixelColor (x, y);
221 double ra = double (c.alpha ()) / 255.0;
222 auto nr = static_cast<int> (c.red () * ra + 255 * (1.0 - ra));
223 auto ng = static_cast<int> (c.green () * ra + 255 * (1.0 - ra));
224 auto nb = static_cast<int> (c.blue () * ra + 255 * (1.0 - ra));
225 auto pc = QColor::fromRgb (nr, ng, nb, 255);
226 image.setPixelColor (x, y, pc);
227 }
228 }
229 }
230
231 string
232 templateBuild (string_view temp, string_view token, string_view real)
233 {
234 auto pos = temp.find (token);
235 if (pos == string::npos)
236 return string (temp);
237
238 auto first = temp.substr (0, pos);
239 auto second = temp.substr (pos + token.length ());
240 auto outstr = string (first);
241 outstr += string (real);
242 outstr += string (second);
243 return outstr;
244 }
245
246 qint64
247 parseFormattedDataSize (const QString &sizeStr)
248 {
249 QString cleanStr = sizeStr.simplified ().toUpper ();
250
251 QRegularExpression re ("^(\\d+(?:\\.\\d+)?)(\\s*)(B?|KB?|MB?|GB?|TB?)$");
252 QRegularExpressionMatch match = re.match (cleanStr);
253
254 if (match.hasMatch ())
255 {
256 double number = match.captured (1).toDouble ();
257 QString unit = match.captured (3);
258 if (unit.isEmpty ())
259 return static_cast<qint64> (number);
260
261 switch (unit[0].unicode ())
262 {
263 case 'T':
264 return static_cast<qint64> (number * 1024 * 1024 * 1024 * 1024);
265 case 'G':
266 return static_cast<qint64> (number * 1024 * 1024 * 1024);
267 case 'M':
268 return static_cast<qint64> (number * 1024 * 1024);
269 case 'K':
270 return static_cast<qint64> (number * 1024);
271 default:
272 return static_cast<qint64> (number);
273 }
274 }
275
276 return -1;
277 }
278
279 qint64
280 filesystemTimeToMSeconds (std::filesystem::file_time_type ftt)
281 {
282 auto epoch = ftt.time_since_epoch () + chrono::seconds{ 6437664000 };
283 auto milliseconds = chrono::duration_cast<chrono::seconds> (epoch);
284 return static_cast<qint64> (milliseconds.count ());
285 }
286
287 }
288
289 // Local Variables:
290 // mode: c++
291 // End: