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