source: XmlTools2/trunk/util.cpp@ 940

Last change on this file since 940 was 923, checked in by s10k, 11 years ago

XmlTools2
added --no-verbose mode
The insertion of xml via patch now support multiple nodes inside <xml></xml> tags

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
[906]10QString AppVersion="2.0";
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
109QStringList 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
123QStringList qStringListFromSpacedString(const QString &mySpacedString){
124 return Util::substring(mySpacedString," ");
125}
126
127QList<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
140QList<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
153QString normalizeDecimalSeparator(QString value){
154 return value.replace(',','.');
155}
156
157bool backupFile(QString file){
158 return copyFile(file,file+".bak");
159}
160
161bool 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
169QStringList getAllFilesByWildcard(const QString &wildcard){
170
171 QString pathNormalized;
172 QStringList nameWildCard; // entryList requires a QStringList
[923]173 QStringList resultFiles; // result files with absolute path
[906]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
[923]184 if(pathNormalized.contains("/")){ // If contains full path
[906]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);
[923]192 }
193 else{ // if relative
194 nameWildCard << wildcard;
195 }
[906]196
[923]197 foreach (QFileInfo fileInfo, directory.entryInfoList(nameWildCard)){
198 resultFiles << fileInfo.absoluteFilePath();
[906]199 }
200
[923]201 return resultFiles;
[906]202}
203
204}
Note: See TracBrowser for help on using the repository browser.