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.0c";
|
---|
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 | // 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;
|
---|
113 |
|
---|
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 | // }
|
---|
120 |
|
---|
121 | // return result;
|
---|
122 | //}
|
---|
123 |
|
---|
124 | QStringList qStringListFromSpacedString(const QString &mySpacedString){
|
---|
125 | return mySpacedString.split(" ");
|
---|
126 | }
|
---|
127 |
|
---|
128 | QList<int> qListIntFromSpacedString(const QString &mySpacedString){
|
---|
129 | QStringList stringList;
|
---|
130 | QList<int> intList;
|
---|
131 |
|
---|
132 | stringList = mySpacedString.split(" ");
|
---|
133 |
|
---|
134 | foreach(QString value, stringList){
|
---|
135 | bool ok;
|
---|
136 |
|
---|
137 | intList << value.toInt(&ok);
|
---|
138 |
|
---|
139 | if(!ok){
|
---|
140 | throw std::runtime_error(QString("Impossible to convert string '" + value + "' to int!").toUtf8().constData());
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | return intList;
|
---|
145 | }
|
---|
146 |
|
---|
147 | QList<double> qListDoubleFromSpacedString(const QString &mySpacedString){
|
---|
148 | QStringList stringList;
|
---|
149 | QList<double> doubleList;
|
---|
150 |
|
---|
151 | stringList = mySpacedString.split(" ");
|
---|
152 |
|
---|
153 | foreach(QString value, stringList){
|
---|
154 | bool ok;
|
---|
155 |
|
---|
156 | doubleList << value.toDouble(&ok);
|
---|
157 |
|
---|
158 | if(!ok){
|
---|
159 | throw std::runtime_error(QString("Impossible to convert string '" + value + "' to double!").toUtf8().constData());
|
---|
160 | }
|
---|
161 | }
|
---|
162 |
|
---|
163 | return doubleList;
|
---|
164 | }
|
---|
165 |
|
---|
166 | QString normalizeDecimalSeparator(QString value){
|
---|
167 | return value.replace(',','.');
|
---|
168 | }
|
---|
169 |
|
---|
170 | bool backupFile(QString file){
|
---|
171 | return copyFile(file,file+".bak");
|
---|
172 | }
|
---|
173 |
|
---|
174 | bool copyFile(QString src, QString dest){
|
---|
175 | return QFile::copy(src, dest);
|
---|
176 | }
|
---|
177 |
|
---|
178 |
|
---|
179 | // Supports wildcards, and directory with wildcard e.g.:
|
---|
180 | // *.xml
|
---|
181 | // C:/myXmls/*.xml
|
---|
182 | QStringList getAllFilesByWildcard(const QString &wildcard){
|
---|
183 |
|
---|
184 | QString pathNormalized;
|
---|
185 | QStringList nameWildCard; // entryList requires a QStringList
|
---|
186 | QStringList resultFiles; // result files with absolute path
|
---|
187 | int endOfPathIdx;
|
---|
188 | QDir directory;
|
---|
189 |
|
---|
190 | if(wildcard==""){
|
---|
191 | std::cout << "You need to specify a wildcard! Aborting..." << std::endl;
|
---|
192 | exit(1);
|
---|
193 | }
|
---|
194 |
|
---|
195 | pathNormalized=Util::normalizePath(wildcard); // Convert slashes to work in both mac and windows
|
---|
196 |
|
---|
197 | if(pathNormalized.contains("/")){ // If contains full path
|
---|
198 | endOfPathIdx=Util::indexOfBackward(pathNormalized,"/"); // get last slash
|
---|
199 |
|
---|
200 | nameWildCard.append(pathNormalized.right(pathNormalized.size()-1-endOfPathIdx)); // get the names wildcard // -1 because starts with zeo
|
---|
201 |
|
---|
202 | pathNormalized=pathNormalized.left(endOfPathIdx); // get the complete path
|
---|
203 |
|
---|
204 | directory=QDir(pathNormalized);
|
---|
205 | }
|
---|
206 | else{ // if relative
|
---|
207 | nameWildCard << wildcard;
|
---|
208 | }
|
---|
209 |
|
---|
210 | foreach (QFileInfo fileInfo, directory.entryInfoList(nameWildCard)){
|
---|
211 | resultFiles << fileInfo.absoluteFilePath();
|
---|
212 | }
|
---|
213 |
|
---|
214 | return resultFiles;
|
---|
215 | }
|
---|
216 |
|
---|
217 | }
|
---|