source: XmlTools2/trunk/util.h@ 956

Last change on this file since 956 was 942, checked in by s10k, 11 years ago

XmlTools

File size: 2.9 KB
RevLine 
[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
10namespace GlobalVars{
11extern QString AppName;
[920]12extern QString AppExecutable;
[906]13extern QString AppVersion;
14}
15
16/**
17 Utilities functions (global)
18 **/
19namespace Util{
20QString normalizePath(QString path);
21QString cutName(QString path);
22QString insertQuotes(QString path);
23QString normalizeAndQuote(QString path);
24QString fullTrim(QString str);
25QString normalizeDecimalSeparator(QString value);
[942]26// Use qstring split
27// QStringList substring(const QString &myString, const QString &separator, Qt::CaseSensitivity cs = Qt::CaseSensitive);
[906]28QStringList qStringListFromSpacedString(const QString &mySpacedString);
29QStringList getAllFilesByWildcard(const QString &wildcard);
30QList<int> qListIntFromSpacedString(const QString &mySpacedString);
31QList<double> qListDoubleFromSpacedString(const QString &mySpacedString);
32int indexOfBackward(const QString &myString, const QString &toSearch, int from = -1);
33bool checkEmptySpaces(QStringList toCheck);
34bool checkIfIntegers(QStringList toCheck);
35bool checkIfDoubles(QStringList toCheck);
36bool isStringInteger(QString myString);
37bool isStringDouble(QString myString);
38bool backupFile(QString file);
39bool 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
49inline QString toQString(std::string myString){
50 return QString::fromStdString(myString);
51}
52
[942]53// Needs optimization
[926]54inline 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
Note: See TracBrowser for help on using the repository browser.