source: XmlTools2/trunk/util.cpp@ 968

Last change on this file since 968 was 958, checked in by s10k, 11 years ago

XmlTools
More updates.

File size: 5.0 KB
RevLine 
[906]1#include "util.h"
2
3namespace GlobalVars{
4QString AppName="XmlTools";
[920]5#ifdef Q_OS_WIN
6QString AppExecutable=AppName+".exe";
7#else
8QString AppExecutable="./"+AppName; // Mac OS needs unix like executing
9#endif
[958]10QString AppVersion="2.0a";
[906]11}
12
13namespace Util{
14
15QString normalizePath(QString path){
16 return path.replace("\\","/");
17}
18
19QString cutName(QString path){
20 return path.remove(0,path.lastIndexOf('/')).remove('"');
21}
22
23QString insertQuotes(QString path){
24 return "\""+path+"\"";
25}
26
27QString normalizeAndQuote(QString path){
28 return insertQuotes(normalizePath(path));
29}
30
31bool 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
40bool 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
49bool 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
58bool 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
66bool 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
74QString 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
84int 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
[942]109// Use qstring split
110//QStringList substring(const QString &myString,const QString &separator, Qt::CaseSensitivity cs){
111// QStringList result = QStringList();
112// int currIdx=0, nextIdx=0;
[906]113
[942]114// while(true){
115// nextIdx=myString.indexOf(separator,currIdx,cs);
116// result << myString.mid(currIdx,nextIdx-currIdx);
117// if(nextIdx==-1) break;
118// currIdx=nextIdx+1;
119// }
[906]120
[942]121// return result;
122//}
[906]123
124QStringList qStringListFromSpacedString(const QString &mySpacedString){
[942]125 return mySpacedString.split(" ");
[906]126}
127
128QList<int> qListIntFromSpacedString(const QString &mySpacedString){
129 QStringList stringList;
130 QList<int> intList;
131
[942]132 stringList = mySpacedString.split(" ");
[906]133
134 foreach(QString value, stringList){
135 intList << value.toInt();
136 }
137
138 return intList;
139}
140
141QList<double> qListDoubleFromSpacedString(const QString &mySpacedString){
142 QStringList stringList;
143 QList<double> doubleList;
144
[942]145 stringList = mySpacedString.split(" ");
[906]146
147 foreach(QString value, stringList){
148 doubleList << value.toDouble();
149 }
150
151 return doubleList;
152}
153
154QString normalizeDecimalSeparator(QString value){
155 return value.replace(',','.');
156}
157
158bool backupFile(QString file){
159 return copyFile(file,file+".bak");
160}
161
162bool copyFile(QString src, QString dest){
163 return QFile::copy(src, dest);
164}
165
166
167// Supports wildcards, and directory with wildcard e.g.:
168// *.xml
169// C:/myXmls/*.xml
170QStringList getAllFilesByWildcard(const QString &wildcard){
171
172 QString pathNormalized;
173 QStringList nameWildCard; // entryList requires a QStringList
[923]174 QStringList resultFiles; // result files with absolute path
[906]175 int endOfPathIdx;
176 QDir directory;
177
178 if(wildcard==""){
179 std::cout << "You need to specify a wildcard! Aborting..." << std::endl;
180 exit(1);
181 }
182
183 pathNormalized=Util::normalizePath(wildcard); // Convert slashes to work in both mac and windows
184
[923]185 if(pathNormalized.contains("/")){ // If contains full path
[906]186 endOfPathIdx=Util::indexOfBackward(pathNormalized,"/"); // get last slash
187
188 nameWildCard.append(pathNormalized.right(pathNormalized.size()-1-endOfPathIdx)); // get the names wildcard // -1 because starts with zeo
189
190 pathNormalized=pathNormalized.left(endOfPathIdx); // get the complete path
191
192 directory=QDir(pathNormalized);
[923]193 }
194 else{ // if relative
195 nameWildCard << wildcard;
196 }
[906]197
[923]198 foreach (QFileInfo fileInfo, directory.entryInfoList(nameWildCard)){
199 resultFiles << fileInfo.absoluteFilePath();
[906]200 }
201
[923]202 return resultFiles;
[906]203}
204
205}
Note: See TracBrowser for help on using the repository browser.