1 |
#include "util.h" |
2 |
|
3 |
namespace GlobalVars{ |
4 |
QString AppName="XmlTools"; |
5 |
#ifdef Q_OS_WIN |
6 |
QString AppExecutable=AppName+".exe"; |
7 |
#else |
8 |
QString AppExecutable="./"+AppName; // Mac OS needs unix like executing |
9 |
#endif |
10 |
QString AppVersion="2.0"; |
11 |
} |
12 |
|
13 |
namespace Util{ |
14 |
|
15 |
QString normalizePath(QString path){ |
16 |
return path.replace("\\","/"); |
17 |
} |
18 |
|
19 |
QString cutName(QString path){ |
20 |
return path.remove(0,path.lastIndexOf('/')).remove('"'); |
21 |
} |
22 |
|
23 |
QString insertQuotes(QString path){ |
24 |
return "\""+path+"\""; |
25 |
} |
26 |
|
27 |
QString normalizeAndQuote(QString path){ |
28 |
return insertQuotes(normalizePath(path)); |
29 |
} |
30 |
|
31 |
bool checkEmptySpaces(QStringList toCheck){ |
32 |
foreach (QString current, toCheck){ |
33 |
if(current.trimmed().isEmpty()){ |
34 |
return true; //There are empty spaces |
35 |
} |
36 |
} |
37 |
return false; |
38 |
} |
39 |
|
40 |
bool checkIfIntegers(QStringList toCheck){ |
41 |
foreach (QString current, toCheck){ |
42 |
if(!isStringInteger(current)){ |
43 |
return true; // Some aren't valid integers |
44 |
} |
45 |
} |
46 |
return false; |
47 |
} |
48 |
|
49 |
bool checkIfDoubles(QStringList toCheck){ |
50 |
foreach (QString current, toCheck){ |
51 |
if(!isStringDouble(current)){ |
52 |
return true; // Some aren't valid doubles |
53 |
} |
54 |
} |
55 |
return false; |
56 |
} |
57 |
|
58 |
bool isStringInteger(QString myString){ |
59 |
bool isNumber; |
60 |
|
61 |
myString.toInt(&isNumber); //convert to int and see if it succeeds |
62 |
|
63 |
return isNumber; |
64 |
} |
65 |
|
66 |
bool isStringDouble(QString myString){ |
67 |
bool isDouble; |
68 |
|
69 |
myString.toDouble(&isDouble); //convert to double and see if it succeeds |
70 |
|
71 |
return isDouble; |
72 |
} |
73 |
|
74 |
QString fullTrim(QString str) { |
75 |
|
76 |
str = str.simplified(); //convert all invisible chars in normal whitespaces |
77 |
str.replace( " ", "" ); |
78 |
|
79 |
return str; |
80 |
} |
81 |
|
82 |
//Searches for the QString "toSearch" in the "myString" variable backward |
83 |
//Returns the index of the first match or -1 if not found |
84 |
int indexOfBackward(const QString &myString, const QString &toSearch, int from){ |
85 |
int myStringSize=myString.size(); |
86 |
int toSearchSize=toSearch.size(); |
87 |
|
88 |
if(from==-1){ |
89 |
from=myStringSize; |
90 |
} |
91 |
|
92 |
int i=from; |
93 |
|
94 |
while(i>=0){ |
95 |
for(int j=toSearchSize-1; j>=0; j--){ |
96 |
i--; |
97 |
if(myString.at(i)!=toSearch.at(j)){ |
98 |
break; |
99 |
} |
100 |
if(j==0){ |
101 |
return i; |
102 |
} |
103 |
} |
104 |
} |
105 |
|
106 |
return -1; |
107 |
} |
108 |
|
109 |
QStringList substring(const QString &myString,const QString &separator, Qt::CaseSensitivity cs){ |
110 |
QStringList result = QStringList(); |
111 |
int currIdx=0, nextIdx=0; |
112 |
|
113 |
while(true){ |
114 |
nextIdx=myString.indexOf(separator,currIdx,cs); |
115 |
result << myString.mid(currIdx,nextIdx-currIdx); |
116 |
if(nextIdx==-1) break; |
117 |
currIdx=nextIdx+1; |
118 |
} |
119 |
|
120 |
return result; |
121 |
} |
122 |
|
123 |
QStringList qStringListFromSpacedString(const QString &mySpacedString){ |
124 |
return Util::substring(mySpacedString," "); |
125 |
} |
126 |
|
127 |
QList<int> qListIntFromSpacedString(const QString &mySpacedString){ |
128 |
QStringList stringList; |
129 |
QList<int> intList; |
130 |
|
131 |
stringList = Util::substring(mySpacedString," "); |
132 |
|
133 |
foreach(QString value, stringList){ |
134 |
intList << value.toInt(); |
135 |
} |
136 |
|
137 |
return intList; |
138 |
} |
139 |
|
140 |
QList<double> qListDoubleFromSpacedString(const QString &mySpacedString){ |
141 |
QStringList stringList; |
142 |
QList<double> doubleList; |
143 |
|
144 |
stringList = Util::substring(mySpacedString," "); |
145 |
|
146 |
foreach(QString value, stringList){ |
147 |
doubleList << value.toDouble(); |
148 |
} |
149 |
|
150 |
return doubleList; |
151 |
} |
152 |
|
153 |
QString normalizeDecimalSeparator(QString value){ |
154 |
return value.replace(',','.'); |
155 |
} |
156 |
|
157 |
bool backupFile(QString file){ |
158 |
return copyFile(file,file+".bak"); |
159 |
} |
160 |
|
161 |
bool copyFile(QString src, QString dest){ |
162 |
return QFile::copy(src, dest); |
163 |
} |
164 |
|
165 |
|
166 |
// Supports wildcards, and directory with wildcard e.g.: |
167 |
// *.xml |
168 |
// C:/myXmls/*.xml |
169 |
QStringList getAllFilesByWildcard(const QString &wildcard){ |
170 |
|
171 |
QString pathNormalized; |
172 |
QStringList nameWildCard; // entryList requires a QStringList |
173 |
QStringList resultFiles; // result files with absolute path |
174 |
int endOfPathIdx; |
175 |
QDir directory; |
176 |
|
177 |
if(wildcard==""){ |
178 |
std::cout << "You need to specify a wildcard! Aborting..." << std::endl; |
179 |
exit(1); |
180 |
} |
181 |
|
182 |
pathNormalized=Util::normalizePath(wildcard); // Convert slashes to work in both mac and windows |
183 |
|
184 |
if(pathNormalized.contains("/")){ // If contains full path |
185 |
endOfPathIdx=Util::indexOfBackward(pathNormalized,"/"); // get last slash |
186 |
|
187 |
nameWildCard.append(pathNormalized.right(pathNormalized.size()-1-endOfPathIdx)); // get the names wildcard // -1 because starts with zeo |
188 |
|
189 |
pathNormalized=pathNormalized.left(endOfPathIdx); // get the complete path |
190 |
|
191 |
directory=QDir(pathNormalized); |
192 |
} |
193 |
else{ // if relative |
194 |
nameWildCard << wildcard; |
195 |
} |
196 |
|
197 |
foreach (QFileInfo fileInfo, directory.entryInfoList(nameWildCard)){ |
198 |
resultFiles << fileInfo.absoluteFilePath(); |
199 |
} |
200 |
|
201 |
return resultFiles; |
202 |
} |
203 |
|
204 |
} |