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