forked from psemiletov/tea-qt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.cpp
594 lines (439 loc) · 15.1 KB
/
utils.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
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
/*
this code is Public Domain
Peter Semiletov
*/
#include <QTextStream>
#include <QTextCodec>
#include <QDebug>
#include <QDir>
#include <QImageReader>
#include <QImage>
#include <QProcess>
#include "utils.h"
using namespace std;
/* file utils */
bool has_css_file (const QString &path)
{
if (path.isEmpty())
return false;
QDir d (path);
QStringList l = d.entryList();
for (int i = 0; i < l.size(); i++)
{
if (l.at(i).endsWith(".css"))
return true;
}
return false;
}
QString guess_enc_for_file (const QString &fname)
{
if (fname.isEmpty())
return QString();
QString enc = "UTF-8";
QProcess p;
p.start ("enca", QStringList() << "-i" << fname);
if (! p.waitForStarted() || ! p.waitForFinished() )
return "err";
QString s = p.readAllStandardOutput();
if (! s.isEmpty())
enc = s.trimmed();
return enc;
}
bool file_is_writable (const QString &fname)
{
if (fname.isEmpty())
return false;
QFile f (fname);
return f.isWritable();
}
bool file_is_readable (const QString &fname)
{
if (fname.isEmpty())
return false;
QFile f (fname);
return f.isReadable();
}
bool path_is_file (const QString &fname)
{
if (fname.isEmpty())
return false;
QFileInfo fi (fname);
return fi.isFile();
}
bool path_is_dir (const QString &fname)
{
if (fname.isEmpty())
return false;
QFileInfo fi (fname);
return fi.isDir();
}
bool path_is_abs (const QString &fname)
{
if (fname.isEmpty())
return false;
if (fname[0] == '/' || fname.indexOf (':') == 1)
return true;
else
return false;
}
bool dir_exists (const QString &path)
{
if (path.isEmpty())
return false;
QDir d (path);
return d.exists();
}
bool file_exists (const QString &fileName)
{
if (fileName.isEmpty())
return false;
return QFile::exists (fileName);
}
QString change_file_ext (const QString &s, const QString &ext)
{
int i = s.lastIndexOf (".");
if (i == -1)
return (s + "." + ext);
QString r (s);
r.truncate (++i);
r.append (ext);
return r;
}
QString file_get_ext (const QString &file_name)
{
if (file_name.isEmpty())
return QString();
int i = file_name.lastIndexOf (".");
if (i == -1)
return QString();
return file_name.mid (i + 1).toLower();
}
QStringList read_dir_entries (const QString &path)
{
QDir dir (path);
return dir.entryList (QDir::AllEntries | QDir::NoDotAndDotDot);
}
/* io utils */
bool qstring_save (const QString &fileName, const QString &data, const char *enc)
{
if (fileName.isEmpty())
return false;
QFile file (fileName);
if (! file.open (QFile::WriteOnly))
return false;
QTextCodec *codec = QTextCodec::codecForName (enc);
QByteArray ba = codec->fromUnicode(data);
file.write(ba);
file.close();
return true;
}
QString qstring_load (const QString &fileName, const char *enc)
{
if (fileName.isEmpty())
return QString();
QFile file (fileName);
if (! file.open (QFile::ReadOnly))
return QString();
QByteArray ba = file.readAll();
QTextCodec *codec = QTextCodec::codecForName(enc);
file.close();
return codec->toUnicode(ba);
}
QString qstring_load_first_line (const QString &fileName)
{
if (fileName.isEmpty())
return QString();
QFile file (fileName);
if (! file.open (QFile::ReadOnly | QFile::Text))
return QString();
QTextStream in(&file);
return in.readLine();
}
QByteArray file_load (const QString &fileName)
{
if (fileName.isEmpty())
return QByteArray();
QFile file (fileName);
QByteArray b;
if (! file.open (QFile::ReadOnly))
return b;
b = file.readAll();
return b;
}
/* string/stringlist utils */
void strlist_swap (QStringList &l, int a, int b)
{
QString t = l[a];
l[a] = l[b];
l[b] = t;
}
QString string_between (const QString &source,
const QString &sep1,
const QString &sep2)
{
QString result;
int pos1 = source.indexOf (sep1);
if (pos1 == -1)
return result;
int pos2 = source.indexOf (sep2, pos1 + sep1.size());
if (pos2 == -1)
return result;
pos1 += sep1.size();
result = source.mid (pos1, pos2 - pos1);
return result;
}
bool char_is_bad (const QChar &c)
{
if (! c.isNull() && ! c.isLetter())
return true;
return false;
}
void qstring_list_print (const QStringList &l)
{
for (int i = 0; i < l.size(); i++)
qDebug() << l[i];
}
QStringList bytearray_to_stringlist (const QList<QByteArray> &a)
{
QStringList r;
for (int i = 0; i < a.size(); i++)
r.append (a.at(i).data());
return r;
}
/* hash utils */
QString hash_get_val (QHash<QString, QString> &h,
const QString &key,
const QString &def_val)
{
QString result = h.value (key);
if (result.isEmpty())
{
result = def_val;
h.insert (key, def_val);
}
return result;
}
QString qstring_load_value (const QString &fileName, const QString &key, const QString &def)
{
QHash <QString, QString> h = hash_load_keyval (fileName);
return hash_get_val (h, key, def);
}
QHash <QString, QString> hash_load_keyval (const QString &fname)
{
QHash <QString, QString> result;
if (! file_exists (fname))
return result;
QStringList l = qstring_load (fname).split ("\n");
for (QList <QString>::iterator i = l.begin(); i != l.end(); ++i)
{
QStringList sl = i->split ("=");
if (sl.size() > 1)
result.insert (sl.at(0), sl.at(1));
}
return result;
}
/* image utils */
bool is_image (const QString &filename)
{
if (filename.isEmpty())
return false;
QList <QByteArray> a = QImageReader::supportedImageFormats();
for (QList <QByteArray>::iterator i = a.begin(); i != a.end(); ++i)
{
QString t (i->data());
if (filename.endsWith (t.prepend ("."), Qt::CaseInsensitive))
return true;
}
return false;
}
QString get_insert_image (const QString &file_name, const QString &full_path, const QString &markup_mode)
{
if (! is_image (full_path))
return QString();
QFileInfo inf (file_name);
QDir dir (inf.absolutePath());
QImage img;
img.load (full_path);
QString result;
if (markup_mode == "HTML")
result = QString ("<img src=\"%1\" alt=\"\" width=\"%2\" height=\"%3\">").arg (
dir.relativeFilePath (full_path)).arg (img.width()).arg (img.height());
else
if (markup_mode == "XHTML")
result = QString ("<img src=\"%1\" border=\"0\" alt=\"\" width=\"%2\" height=\"%3\" />").arg (
dir.relativeFilePath (full_path)).arg (img.width()).arg (img.height());
else
if (markup_mode == "Docbook")
result = QString ("<mediaobject><imageobject>\n<imagedata fileref=\"%1\"/>\n</imageobject></mediaobject>").arg (
dir.relativeFilePath (full_path)) ;
else
if (markup_mode == "LaTeX")
result = QString ("\\includegraphics{%1}").arg (dir.relativeFilePath (full_path));
else
if (markup_mode == "Lout")
result = QString ("@IncludeGraphic {%1}").arg (dir.relativeFilePath (full_path));
else
if (markup_mode == "Markdown")
result = QString ("![alt_text]({%1})").arg (dir.relativeFilePath (full_path));
return result;
}
/* class functions */
void CFilesList::iterate (QFileInfo &fi)
{
if (fi.isDir())
{
QDir d (fi.absoluteFilePath());
QFileInfoList lfi= d.entryInfoList (QDir::Dirs | QDir::Files | QDir::Readable | QDir::NoDotAndDotDot);
for (int i = 0; i < lfi.count(); i++)
iterate (lfi[i]);
}
else
if (fi.isFile())
list.append (fi.absoluteFilePath());
}
void CFilesList::get (const QString &path)
{
if (path.isEmpty())
return;
list.clear();
QDir d (path);
QFileInfoList lfi= d.entryInfoList (QDir::Dirs | QDir::Files | QDir::Readable | QDir::NoDotAndDotDot);
for (int i = 0; i < lfi.count(); i++)
iterate (lfi[i]);
}
#if QT_VERSION < 0x050000
#define ADDTEXTTYPE(expr) patterns.push_back (QRegExp (expr, Qt::CaseInsensitive));
#else
#define ADDTEXTTYPE(expr) patterns.push_back (QRegularExpression (expr, QRegularExpression::CaseInsensitiveOption));
#endif
CFTypeChecker::CFTypeChecker()
{
ADDTEXTTYPE (".*(readme|news|changelog|todo)$");
ADDTEXTTYPE ("^\\..*(rc)$");
ADDTEXTTYPE ("^.*\\.(txt|conf|md|ini|bat|cfg|sbv|log|odt|docx|kwd|fb2|abw|rtf|epub|sxw)$");
ADDTEXTTYPE ("^.*\\.(cpp|c|h|hh|cxx|hpp|cc|m|mm)$");
ADDTEXTTYPE ("^.*\\.(htm|html|xml|xhtml|ts|osm|xsl)$");
#if defined(POPPLER_ENABLE)
ADDTEXTTYPE ("^.*\\.(pdf)$");
#endif
#if defined(DJVU_ENABLE)
ADDTEXTTYPE ("^.*\\.(djvu)$");
#endif
ADDTEXTTYPE ("^.*\\.(awk)$");
ADDTEXTTYPE ("^.*\\.(sh)$");
ADDTEXTTYPE ("^.*\\.(bas|bi|vbs|vbe)$");
ADDTEXTTYPE ("^.*\\.(d)$");
ADDTEXTTYPE ("^.*\\.(f|for|f90|f95)$");
ADDTEXTTYPE ("^.*\\.(java|js)$");
ADDTEXTTYPE ("^.*\\.(ly)$");
ADDTEXTTYPE ("^.*\\.(lout)$");
ADDTEXTTYPE ("^.*\\.(lua)$");
ADDTEXTTYPE ("^.*\\.(asm)$");
ADDTEXTTYPE ("^.*\\.(nsi)$");
ADDTEXTTYPE ("^.*\\.(pp|pas|dpr)$");
ADDTEXTTYPE ("^.*\\.(pl|pm)$");
ADDTEXTTYPE ("^.*\\.(php)$");
ADDTEXTTYPE ("^.*\\.(po)$");
ADDTEXTTYPE ("^.*\\.(py)$");
ADDTEXTTYPE ("^.*\\.(r)$");
ADDTEXTTYPE ("^.*\\.(sd7)$");
ADDTEXTTYPE ("^.*\\.(tex|lyx)$");
ADDTEXTTYPE ("^.*\\.(vala)$");
ADDTEXTTYPE ("^.*\\.(v)$");
ADDTEXTTYPE ("^.*\\.(wiki)$");
}
/*
CFTypeChecker::CFTypeChecker()
{
#if QT_VERSION < 0x050000
patterns.push_back (QRegExp (".*(readme|news|changelog|todo)$", Qt::CaseInsensitive));
patterns.push_back (QRegExp ("^\\..*(rc)$", Qt::CaseInsensitive));
patterns.push_back (QRegExp ("^.*\\.(txt|conf|md|ini|bat|cfg|sbv|log|odt|docx|kwd|fb2|abw|rtf|epub|sxw)$", Qt::CaseInsensitive));
patterns.push_back (QRegExp ("^.*\\.(cpp|c|h|hh|cxx|hpp|cc|m|mm)$", Qt::CaseInsensitive));
patterns.push_back (QRegExp ("^.*\\.(htm|html|xml|xhtml|ts|osm|xsl)$", Qt::CaseInsensitive));
#if defined(POPPLER_ENABLE)
patterns.push_back (QRegExp ("^.*\\.(pdf)$", Qt::CaseInsensitive));
#endif
#if defined(DJVU_ENABLE)
patterns.push_back (QRegExp ("^.*\\.(djvu)$", Qt::CaseInsensitive));
#endif
patterns.push_back (QRegExp ("^.*\\.(awk)$", Qt::CaseInsensitive));
patterns.push_back (QRegExp ("^.*\\.(sh)$", Qt::CaseInsensitive));
patterns.push_back (QRegExp ("^.*\\.(bas|bi|vbs|vbe)$", Qt::CaseInsensitive));
patterns.push_back (QRegExp ("^.*\\.(d)$", Qt::CaseInsensitive));
patterns.push_back (QRegExp ("^.*\\.(f|for|f90|f95)$", Qt::CaseInsensitive));
patterns.push_back (QRegExp ("^.*\\.(java|js)$", Qt::CaseInsensitive));
patterns.push_back (QRegExp ("^.*\\.(ly)$", Qt::CaseInsensitive));
patterns.push_back (QRegExp ("^.*\\.(lout)$", Qt::CaseInsensitive));
patterns.push_back (QRegExp ("^.*\\.(lua)$", Qt::CaseInsensitive));
patterns.push_back (QRegExp ("^.*\\.(asm)$", Qt::CaseInsensitive));
patterns.push_back (QRegExp ("^.*\\.(nsi)$", Qt::CaseInsensitive));
patterns.push_back (QRegExp ("^.*\\.(pp|pas|dpr)$", Qt::CaseInsensitive));
patterns.push_back (QRegExp ("^.*\\.(pl|pm)$", Qt::CaseInsensitive));
patterns.push_back (QRegExp ("^.*\\.(php)$", Qt::CaseInsensitive));
patterns.push_back (QRegExp ("^.*\\.(po)$", Qt::CaseInsensitive));
patterns.push_back (QRegExp ("^.*\\.(py)$", Qt::CaseInsensitive));
patterns.push_back (QRegExp ("^.*\\.(r)$", Qt::CaseInsensitive));
patterns.push_back (QRegExp ("^.*\\.(sd7)$", Qt::CaseInsensitive));
patterns.push_back (QRegExp ("^.*\\.(tex|lyx)$", Qt::CaseInsensitive));
patterns.push_back (QRegExp ("^.*\\.(vala)$", Qt::CaseInsensitive));
patterns.push_back (QRegExp ("^.*\\.(v)$", Qt::CaseInsensitive));
patterns.push_back (QRegExp ("^.*\\.(wiki)$", Qt::CaseInsensitive));
#else
patterns.push_back (QRegularExpression (".*(readme|news|changelog|todo)$", QRegularExpression::CaseInsensitiveOption));
patterns.push_back (QRegularExpression ("^\\..*(rc)$", QRegularExpression::CaseInsensitiveOption));
patterns.push_back (QRegularExpression ("^.*\\.(txt|conf|md|ini|bat|cfg|sbv|log|odt|docx|kwd|fb2|abw|rtf|epub|sxw)$", QRegularExpression::CaseInsensitiveOption));
patterns.push_back (QRegularExpression ("^.*\\.(cpp|c|h|hh|cxx|hpp|cc|m|mm)$", QRegularExpression::CaseInsensitiveOption));
patterns.push_back (QRegularExpression ("^.*\\.(htm|html|xml|xhtml|ts|osm|xsl)$", QRegularExpression::CaseInsensitiveOption));
#if defined(POPPLER_ENABLE)
patterns.push_back (QRegularExpression ("^.*\\.(pdf)$", QRegularExpression::CaseInsensitiveOption));
#endif
#if defined(DJVU_ENABLE)
patterns.push_back (QRegularExpression ("^.*\\.(djvu)$", QRegularExpression::CaseInsensitiveOption));
#endif
patterns.push_back (QRegularExpression ("^.*\\.(awk)$", QRegularExpression::CaseInsensitiveOption));
patterns.push_back (QRegularExpression ("^.*\\.(sh)$", QRegularExpression::CaseInsensitiveOption));
patterns.push_back (QRegularExpression ("^.*\\.(bas|bi|vbs|vbe)$", QRegularExpression::CaseInsensitiveOption));
patterns.push_back (QRegularExpression ("^.*\\.(d)$", QRegularExpression::CaseInsensitiveOption));
patterns.push_back (QRegularExpression ("^.*\\.(f|for|f90|f95)$", QRegularExpression::CaseInsensitiveOption));
patterns.push_back (QRegularExpression ("^.*\\.(java|js)$", QRegularExpression::CaseInsensitiveOption));
patterns.push_back (QRegularExpression ("^.*\\.(ly)$", QRegularExpression::CaseInsensitiveOption));
patterns.push_back (QRegularExpression ("^.*\\.(lout)$", QRegularExpression::CaseInsensitiveOption));
patterns.push_back (QRegularExpression ("^.*\\.(lua)$", QRegularExpression::CaseInsensitiveOption));
patterns.push_back (QRegularExpression ("^.*\\.(asm)$", QRegularExpression::CaseInsensitiveOption));
patterns.push_back (QRegularExpression ("^.*\\.(nsi)$", QRegularExpression::CaseInsensitiveOption));
patterns.push_back (QRegularExpression ("^.*\\.(pp|pas|dpr)$", QRegularExpression::CaseInsensitiveOption));
patterns.push_back (QRegularExpression ("^.*\\.(pl|pm)$", QRegularExpression::CaseInsensitiveOption));
patterns.push_back (QRegularExpression ("^.*\\.(php)$", QRegularExpression::CaseInsensitiveOption));
patterns.push_back (QRegularExpression ("^.*\\.(po)$", QRegularExpression::CaseInsensitiveOption));
patterns.push_back (QRegularExpression ("^.*\\.(py)$", QRegularExpression::CaseInsensitiveOption));
patterns.push_back (QRegularExpression ("^.*\\.(r)$", QRegularExpression::CaseInsensitiveOption));
patterns.push_back (QRegularExpression ("^.*\\.(sd7)$", QRegularExpression::CaseInsensitiveOption));
patterns.push_back (QRegularExpression ("^.*\\.(tex|lyx)$", QRegularExpression::CaseInsensitiveOption));
patterns.push_back (QRegularExpression ("^.*\\.(vala)$", QRegularExpression::CaseInsensitiveOption));
patterns.push_back (QRegularExpression ("^.*\\.(v)$", QRegularExpression::CaseInsensitiveOption));
patterns.push_back (QRegularExpression ("^.*\\.(wiki)$", QRegularExpression::CaseInsensitiveOption));
#endif
}
*/
bool CFTypeChecker::check (const QString &fname) const
{
#if QT_VERSION < 0x050000
for (size_t i = 0; i < patterns.size(); ++i)
{
if (patterns[i].exactMatch(fname))
return true;
}
#else
for (size_t i = 0; i < patterns.size(); ++i)
{
QRegularExpressionMatch match = patterns[i].match(fname);
if (match.hasMatch())
return true;
}
#endif
return false;
}
/*
GUI functions
*/