1 | #ifndef UTIL_H
|
---|
2 | #define UTIL_H
|
---|
3 |
|
---|
4 | #include <QFile>
|
---|
5 | #include <QDir>
|
---|
6 | #include <QString>
|
---|
7 | #include <QStringList>
|
---|
8 | #include <QVector>
|
---|
9 | #include <iostream> // cout, cin etc.
|
---|
10 |
|
---|
11 | namespace GlobalVars{
|
---|
12 | extern QString AppName;
|
---|
13 | extern QString AppExecutable;
|
---|
14 | extern QString AppVersion;
|
---|
15 | }
|
---|
16 |
|
---|
17 | /**
|
---|
18 | Utilities functions (global)
|
---|
19 | **/
|
---|
20 | namespace Util{
|
---|
21 | QString normalizePath(QString path);
|
---|
22 | QString cutName(QString path);
|
---|
23 | QString insertQuotes(QString path);
|
---|
24 | QString normalizeAndQuote(QString path);
|
---|
25 | QString fullTrim(QString str);
|
---|
26 | QString normalizeDecimalSeparator(QString value);
|
---|
27 | // Use qstring split
|
---|
28 | // QStringList substring(const QString &myString, const QString &separator, Qt::CaseSensitivity cs = Qt::CaseSensitive);
|
---|
29 | QStringList qStringListFromSpacedString(const QString &mySpacedString);
|
---|
30 | QStringList getAllFilesByWildcard(const QString &wildcard);
|
---|
31 | QList<int> qListIntFromSpacedString(const QString &mySpacedString);
|
---|
32 | QList<double> qListDoubleFromSpacedString(const QString &mySpacedString);
|
---|
33 | int indexOfBackward(const QString &myString, const QString &toSearch, int from = -1);
|
---|
34 | bool checkEmptySpaces(QStringList toCheck);
|
---|
35 | bool checkIfIntegers(QStringList toCheck);
|
---|
36 | bool checkIfDoubles(QStringList toCheck);
|
---|
37 | bool isStringInteger(QString myString);
|
---|
38 | bool isStringDouble(QString myString);
|
---|
39 | bool backupFile(QString file);
|
---|
40 | bool copyFile(QString src, QString dest);
|
---|
41 | // The commented code bellow is a big mistake because toLatin1() creates a temp object that gets
|
---|
42 | // destroyed after the semicolon: https://qt-project.org/forums/viewthread/12885 (Volker answer)
|
---|
43 | //// Convert a QString to a c string
|
---|
44 | //// Caution don't use as for example: std::cout << toCstr(a) << " " << toCstr(b);
|
---|
45 | //// as the result will be always a.
|
---|
46 | //inline const char* toCstr(const QString &myString){
|
---|
47 | // return myString.toUtf8().constData();
|
---|
48 | //}
|
---|
49 | // Converts a std::string to QString
|
---|
50 | inline QString toQString(std::string myString){
|
---|
51 | return QString::fromStdString(myString);
|
---|
52 | }
|
---|
53 |
|
---|
54 | //TODO Needs optimization
|
---|
55 | inline QStringList QStringToArgsArray(const QString &args){
|
---|
56 | QStringList result;
|
---|
57 | int startIdx=0, endIdx=0;
|
---|
58 |
|
---|
59 | if(!args.isEmpty()){ // if non empty arguments
|
---|
60 |
|
---|
61 | while(endIdx<args.length()){
|
---|
62 |
|
---|
63 | startIdx=endIdx;
|
---|
64 |
|
---|
65 | if(args.at(startIdx)==' '){ // Ignore spaces until a different char is found
|
---|
66 | endIdx++;
|
---|
67 | continue;
|
---|
68 | }
|
---|
69 | else if(args.at(startIdx)!='"'){ // if first character is different from quote it ends with space
|
---|
70 | endIdx=args.indexOf(' ',startIdx+1);
|
---|
71 | }
|
---|
72 | else{ // If is a quote try to get the all the string until next quote
|
---|
73 | endIdx=args.indexOf('"',startIdx+1);
|
---|
74 | }
|
---|
75 |
|
---|
76 | if(endIdx==-1) break;
|
---|
77 |
|
---|
78 | endIdx++;
|
---|
79 |
|
---|
80 | result << args.mid(startIdx,endIdx-startIdx).replace("\"","").trimmed(); // remove quotes
|
---|
81 | }
|
---|
82 |
|
---|
83 | }
|
---|
84 |
|
---|
85 | return result;
|
---|
86 | }
|
---|
87 |
|
---|
88 | }
|
---|
89 |
|
---|
90 | #endif // UTIL_H
|
---|