source: XmlTools2/trunk/util.h@ 1057

Last change on this file since 1057 was 1055, checked in by s10k, 8 years ago

XmlTools 2.0c

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