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 | // 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 | intList << value.toInt();
|
---|
136 | }
|
---|
137 |
|
---|
138 | return intList;
|
---|
139 | }
|
---|
140 |
|
---|
141 | QList<double> qListDoubleFromSpacedString(const QString &mySpacedString){
|
---|
142 | QStringList stringList;
|
---|
143 | QList<double> doubleList;
|
---|
144 |
|
---|
145 | stringList = mySpacedString.split(" ");
|
---|
146 |
|
---|
147 | foreach(QString value, stringList){
|
---|
148 | doubleList << value.toDouble();
|
---|
149 | }
|
---|
150 |
|
---|
151 | return doubleList;
|
---|
152 | }
|
---|
153 |
|
---|
154 | QString normalizeDecimalSeparator(QString value){
|
---|
155 | return value.replace(',','.');
|
---|
156 | }
|
---|
157 |
|
---|
158 | bool backupFile(QString file){
|
---|
159 | return copyFile(file,file+".bak");
|
---|
160 | }
|
---|
161 |
|
---|
162 | bool 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
|
---|
170 | QStringList getAllFilesByWildcard(const QString &wildcard){
|
---|
171 |
|
---|
172 | QString pathNormalized;
|
---|
173 | QStringList nameWildCard; // entryList requires a QStringList
|
---|
174 | QStringList resultFiles; // result files with absolute path
|
---|
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 |
|
---|
185 | if(pathNormalized.contains("/")){ // If contains full path
|
---|
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);
|
---|
193 | }
|
---|
194 | else{ // if relative
|
---|
195 | nameWildCard << wildcard;
|
---|
196 | }
|
---|
197 |
|
---|
198 | foreach (QFileInfo fileInfo, directory.entryInfoList(nameWildCard)){
|
---|
199 | resultFiles << fileInfo.absoluteFilePath();
|
---|
200 | }
|
---|
201 |
|
---|
202 | return resultFiles;
|
---|
203 | }
|
---|
204 |
|
---|
205 | }
|
---|