ViewVC Help
View File | Revision Log | View Changeset | Root Listing
root/Oni2/XmlTools2/trunk/util.cpp
Revision: 906
Committed: Sat Feb 1 14:27:58 2014 UTC (11 years, 8 months ago) by s10k
Content type: text/x-c++src
File size: 4746 byte(s)
Log Message:

File Contents

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