GCC Code Coverage Report | |||||||||||||||||||||
|
|||||||||||||||||||||
Line | Branch | Exec | Source |
1 |
|||
2 |
#include <unistd.h> //for get_current_dir_name |
||
3 |
#include <dirent.h> //for opendir |
||
4 |
|||
5 |
#include <sys/stat.h> |
||
6 |
#include <sys/types.h> |
||
7 |
|||
8 |
#include "string_utils.h" |
||
9 |
#include "string_system.h" |
||
10 |
#include "string_filename.h" |
||
11 |
|||
12 |
using namespace std; |
||
13 |
|||
14 |
///Say if the given path name exsits or not |
||
15 |
/** @param fileName : name of the file or dir to be checked |
||
16 |
* @return if the file exsits, false otherwise |
||
17 |
*/ |
||
18 |
5 |
bool isFileOrDirExist(const std::string & fileName){ |
|
19 |
✓✓ | 5 |
if(fileName == "") return false; |
20 |
4 |
return access(fileName.c_str(), F_OK) != -1; |
|
21 |
} |
||
22 |
|||
23 |
///Say if a file exsits or not |
||
24 |
/** @param fileName : name of the file to be checked |
||
25 |
* @return true if the file exsits, false otherwise |
||
26 |
*/ |
||
27 |
11 |
bool isFileExist(const std::string & fileName){ |
|
28 |
✓✓ | 11 |
if(fileName == ""){return false;} |
29 |
struct stat path_stat; |
||
30 |
✓✓ | 10 |
if(stat(fileName.c_str(), &path_stat) != 0){ |
31 |
4 |
return false; |
|
32 |
} |
||
33 |
6 |
return S_ISREG(path_stat.st_mode) != 0; |
|
34 |
} |
||
35 |
|||
36 |
|||
37 |
///Get the fileName with the directory to get a readable file |
||
38 |
/** @param fileName : file name to be opened |
||
39 |
* @param vecDirectory : vector of possible directories to look at |
||
40 |
* @return readable file name with appropriate directory, or empty string if the file is not found |
||
41 |
*/ |
||
42 |
5 |
std::string getExistingFileName(const std::string & fileName, const std::vector<std::string> & vecDirectory){ |
|
43 |
✓✓✓✓ ✓✓✓ |
5 |
if(vecDirectory.size() == 0lu || fileName == ""){return "";} |
44 |
2 |
std::vector<std::string>::const_iterator it(vecDirectory.begin()); |
|
45 |
✓✓ | 4 |
while(it != vecDirectory.end()){ |
46 |
✓✓ | 3 |
std::string tmpFile(*it + "/" + fileName); |
47 |
✓✓✓ | 3 |
if(isFileExist(tmpFile)){return tmpFile;} |
48 |
2 |
++it; |
|
49 |
} |
||
50 |
✓ | 1 |
return ""; |
51 |
} |
||
52 |
|||
53 |
///Says if the given direcotry exists |
||
54 |
/** @param dirName : name of the directory |
||
55 |
* @return true if the directory exists, false if not |
||
56 |
*/ |
||
57 |
27 |
bool isDirectoryExist(const std::string & dirName){ |
|
58 |
✓✓ | 27 |
if(dirName == ""){return false;} |
59 |
struct stat path_stat; |
||
60 |
✓✓ | 26 |
if(stat(dirName.c_str(), &path_stat) != 0){ |
61 |
8 |
return false; |
|
62 |
} |
||
63 |
18 |
return S_ISDIR(path_stat.st_mode) != 0; |
|
64 |
} |
||
65 |
|||
66 |
///Returns the current directory |
||
67 |
/** @return current directory |
||
68 |
*/ |
||
69 |
10 |
std::string getCurrentDirectory(){ |
|
70 |
#ifndef __APPLE__ |
||
71 |
10 |
char* ptr = get_current_dir_name(); |
|
72 |
✓ | 10 |
std::string str(ptr); |
73 |
10 |
free(ptr); |
|
74 |
10 |
return str; |
|
75 |
#else |
||
76 |
return phoenix_getenv("PWD"); |
||
77 |
#endif |
||
78 |
} |
||
79 |
|||
80 |
///Tel if a path is absolute or not |
||
81 |
/** @param path : path to be checked |
||
82 |
* @return true if the path is absolute, false otherwise |
||
83 |
*/ |
||
84 |
3 |
bool isAbsolutePath(const std::string & path){ |
|
85 |
✓✓ | 3 |
if(path == ""){return false;} |
86 |
2 |
return path[0] == '/'; |
|
87 |
} |
||
88 |
|||
89 |
///Make an absolute path of the given path |
||
90 |
/** @param path : relative or absolut path (file or dir) |
||
91 |
* @return corresponding absolute path |
||
92 |
*/ |
||
93 |
10 |
std::string makeAbsolutePath(const std::string & path){ |
|
94 |
✓✓✓ | 11 |
if(path == ""){return getCurrentDirectory() + "/";} |
95 |
✓✓ | 9 |
else if(path[0] == '/'){return path;} |
96 |
else{ |
||
97 |
✓✓ | 2 |
return getCurrentDirectory() + "/" + path; |
98 |
} |
||
99 |
} |
||
100 |
|||
101 |
///Remove dots from the path |
||
102 |
/** @param path : path to be cleaned |
||
103 |
* @return path wthout dots |
||
104 |
*/ |
||
105 |
20 |
std::string removePathDots(const std::string path){ |
|
106 |
✓✓✓ | 20 |
if(path == ""){return path;} |
107 |
✓ | 38 |
std::list<std::string> listDir(cutStringList(path, '/')); |
108 |
19 |
std::list<std::string>::reverse_iterator rit = listDir.rbegin(); |
|
109 |
✓✓✓ | 130 |
while(rit != listDir.rend()){ |
110 |
✓✓✓✓ ✓✓ |
111 |
if(*rit == "." || *rit == ""){ |
111 |
// rit = listDir.erase(rit); //No erase in reverse_iterator, so anoying |
||
112 |
✓ | 19 |
*rit = ""; |
113 |
✓✓ | 92 |
}else if(*rit == ".."){ |
114 |
// rit = listDir.erase(rit); //No erase in reverse_iterator, so anoying |
||
115 |
✓ | 10 |
*rit = ""; |
116 |
10 |
++rit; |
|
117 |
✓✓✗ | 10 |
if(rit != listDir.rend()){ |
118 |
// rit = listDir.erase(rit); //No erase in reverse_iterator, so anoying |
||
119 |
✓ | 10 |
*rit = ""; |
120 |
} |
||
121 |
} |
||
122 |
// else{ |
||
123 |
// ++rit; //No erase in reverse_iterator, so anoying |
||
124 |
// } |
||
125 |
111 |
++rit; |
|
126 |
} |
||
127 |
✓✓ | 57 |
std::string out(""), separator(""); |
128 |
✓✓ | 19 |
if(path[0] == '/'){ |
129 |
✓ | 13 |
out = "/"; |
130 |
} |
||
131 |
✓✓ | 140 |
for(std::list<std::string>::iterator it(listDir.begin()); it != listDir.end(); ++it){ |
132 |
✓✓ | 121 |
if(*it == ""){continue;} |
133 |
✓✓ | 82 |
out += separator + (*it); |
134 |
✓ | 82 |
separator = "/"; |
135 |
} |
||
136 |
19 |
return out; |
|
137 |
} |
||
138 |
|||
139 |
///fonction qui renvoie le dossier parent du fichier |
||
140 |
/** @param fileName : nom du fichier dont on veut le dossier |
||
141 |
* @return nom du dossier parent du fichier passé en paramètre |
||
142 |
*/ |
||
143 |
20 |
std::string getDirectory(const std::string & fileName){ |
|
144 |
✓ | 20 |
std::string buffer(""); |
145 |
20 |
bool find(false); |
|
146 |
20 |
std::string::const_reverse_iterator rit = fileName.rbegin(); |
|
147 |
✓✓✓ | 1936 |
while(rit != fileName.rend()){ |
148 |
✓✓ | 1916 |
if(find){ |
149 |
✓ | 1653 |
buffer = *rit + buffer; |
150 |
}else{ |
||
151 |
263 |
find = (*rit == '/'); |
|
152 |
} |
||
153 |
1916 |
rit++; |
|
154 |
} |
||
155 |
40 |
return buffer; |
|
156 |
} |
||
157 |
|||
158 |
///Get path which is under the given pathPart ('some/dir/path' with 'dir' will return 'path') |
||
159 |
/** @param fileName : path to be used |
||
160 |
* @param pathPart : directory in the fileName we are looking for |
||
161 |
* @return rest of the path under the pathPart |
||
162 |
*/ |
||
163 |
3 |
std::string getUnderPath(const std::string & fileName, const std::string & pathPart){ |
|
164 |
✓✓✓ | 3 |
if(pathPart == ""){return fileName;} |
165 |
✓ | 4 |
std::list<std::string> listDir(cutStringList(fileName, '/')); |
166 |
2 |
std::list<std::string>::iterator it(listDir.begin()); |
|
167 |
2 |
bool isSearch(true); |
|
168 |
✓✓✓✓ ✓✓ |
9 |
while(isSearch && it != listDir.end()){ |
169 |
7 |
isSearch = *it != pathPart; |
|
170 |
7 |
++it; |
|
171 |
} |
||
172 |
✓✓ | 8 |
std::string out(""), separator(""); |
173 |
✓✓ | 3 |
while(it != listDir.end()){ |
174 |
✓✓ | 1 |
out += separator + (*it); |
175 |
✓ | 1 |
separator = "/"; |
176 |
1 |
++it; |
|
177 |
} |
||
178 |
2 |
return out; |
|
179 |
} |
||
180 |
|||
181 |
///Check if the given file starts with the given begning |
||
182 |
/** @param fileName : name of the file to be checked |
||
183 |
* @param expectedBegining : expected begening of the file |
||
184 |
* @return true if the file starts with the expected begning, false otherwise |
||
185 |
*/ |
||
186 |
4 |
bool checkFileBegning(const std::string & fileName, const std::string & expectedBegining){ |
|
187 |
4 |
FILE* fp = fopen(fileName.c_str(), "r"); |
|
188 |
✓✓✗✓ ✓✓ |
4 |
if(fp == NULL || expectedBegining.size() == 0lu){return false;} |
189 |
2 |
int val(0); |
|
190 |
2 |
size_t i(0lu); |
|
191 |
2 |
bool isMatch(true); |
|
192 |
✓✓✓✗ ✓✓✓✓ |
41 |
while(isMatch && !feof(fp) && i < expectedBegining.size()){ |
193 |
39 |
val = fgetc(fp); |
|
194 |
39 |
isMatch = (char)val == expectedBegining[i]; |
|
195 |
39 |
++i; |
|
196 |
} |
||
197 |
2 |
fclose(fp); |
|
198 |
2 |
return isMatch; |
|
199 |
} |
||
200 |
|||
201 |
///fonction qui renvoie le nom du fichier du nom complet de fichier passé en paramètre |
||
202 |
/** @param fileName : nom complet de fichier dont on veut le nom (/nom/du/fichier -> fichier) |
||
203 |
* @return nom du fichier passé en paramètre |
||
204 |
*/ |
||
205 |
5 |
std::string getFileName(const std::string & fileName){ |
|
206 |
✓ | 5 |
std::string buffer(""); |
207 |
5 |
std::string::const_reverse_iterator rit = fileName.rbegin(); |
|
208 |
✓✓✗ | 59 |
while(rit != fileName.rend()){ |
209 |
✓✓ | 59 |
if(*rit == '/') break; |
210 |
✓ | 54 |
buffer = *rit + buffer; |
211 |
54 |
rit++; |
|
212 |
} |
||
213 |
10 |
return buffer; |
|
214 |
} |
||
215 |
|||
216 |
///Get the name of the deeper directory |
||
217 |
/** @param path : input path |
||
218 |
* @return name of the deeper directory |
||
219 |
*/ |
||
220 |
2 |
std::string getDirName(const std::string & path){ |
|
221 |
✓ | 2 |
std::string buffer(""); |
222 |
2 |
std::string::const_reverse_iterator rit = path.rbegin(); |
|
223 |
✓✓✗ | 11 |
while(rit != path.rend()){ |
224 |
✓✓ | 11 |
if(*rit == '/'){ |
225 |
✓✓ | 3 |
if(buffer != ""){ |
226 |
2 |
break; |
|
227 |
}else{ |
||
228 |
1 |
rit++; |
|
229 |
1 |
continue; |
|
230 |
} |
||
231 |
} |
||
232 |
✓ | 8 |
buffer = *rit + buffer; |
233 |
8 |
rit++; |
|
234 |
} |
||
235 |
4 |
return buffer; |
|
236 |
} |
||
237 |
|||
238 |
///fonction qui renvoie tout le contenu d'un fichier texte dans une chaîne |
||
239 |
/** @param filename : nom du fichier |
||
240 |
* @return chaîne contenant le fichier |
||
241 |
*/ |
||
242 |
14 |
std::string getFileContent(const std::string & filename){ |
|
243 |
✓ | 14 |
FILE * fp = fopen(filename.c_str(), "r"); |
244 |
✓✓ | 14 |
if(fp == NULL){ |
245 |
✓✓✓✓ |
2 |
cerr << "Impossible d'ouvrir le fichier '" << filename << "'" << endl; |
246 |
✓ | 2 |
return ""; |
247 |
} |
||
248 |
✓ | 24 |
string tmp(getFileContent(fp)); |
249 |
✓ | 12 |
fclose(fp); |
250 |
12 |
return tmp; |
|
251 |
} |
||
252 |
|||
253 |
///fonction qui renvoie tout le contenu d'un fichier texte dans une chaîne |
||
254 |
/** @param fp : pointeur vers le fichier dont on veut mettre le contenu dans une string |
||
255 |
* @return chaîne contenant le fichier |
||
256 |
*/ |
||
257 |
44 |
std::string getFileContent(FILE* fp){ |
|
258 |
✓✓✓ | 44 |
if(fp == NULL) return ""; |
259 |
✓ | 129 |
std::string bufferAllFile(""); |
260 |
int buffer; |
||
261 |
✓✗ | 26860 |
while(!feof(fp)){ |
262 |
✓ | 26860 |
buffer = fgetc(fp); |
263 |
✓✓✓ | 26860 |
if(buffer != EOF) bufferAllFile += (char)buffer; |
264 |
43 |
else break; |
|
265 |
} |
||
266 |
43 |
return bufferAllFile; |
|
267 |
} |
||
268 |
|||
269 |
///fonction qui permet de sauvegader le contenu d'un fichier |
||
270 |
/** @param filename : nom du fichier dans lequel on veut écrire |
||
271 |
* @param content : contenu du fichier |
||
272 |
* @return true si la fonction à réussie false sinon |
||
273 |
*/ |
||
274 |
12 |
bool saveFileContent(const std::string & filename, const std::string & content){ |
|
275 |
12 |
FILE * fp = fopen(filename.c_str(), "w"); |
|
276 |
✓✓ | 12 |
if(fp == NULL){ |
277 |
1 |
cerr << "Impossible d'ouvrir le fichier'" << filename << "'" << endl; |
|
278 |
1 |
return false; |
|
279 |
} |
||
280 |
11 |
bool result(saveFileContent(fp, content)); |
|
281 |
11 |
fclose(fp); |
|
282 |
11 |
return result; |
|
283 |
} |
||
284 |
|||
285 |
///fonction qui permet de sauvegader le contenu d'un fichier |
||
286 |
/** @param fp : pointeur vers le fichier dans lequel on veut écrire |
||
287 |
* @param content : contenu du fichier |
||
288 |
* @return true si la fonction à réussie false sinon |
||
289 |
*/ |
||
290 |
13 |
bool saveFileContent(FILE* fp, const std::string & content){ |
|
291 |
✓✓ | 13 |
if(fp == NULL) return false; |
292 |
11 |
fprintf(fp, "%s", content.c_str()); |
|
293 |
11 |
return true; |
|
294 |
} |
||
295 |
|||
296 |
///fonction qui renvoie l'extention d'un fichier |
||
297 |
/** @param fileName : nom de fichier fichier |
||
298 |
* @return extention du fichier |
||
299 |
*/ |
||
300 |
39 |
std::string getExtention(const std::string & fileName){ |
|
301 |
✓✓✓✓ |
39 |
if(!findInString(fileName, '.')) return ""; |
302 |
✓ | 40 |
std::string extention(""); |
303 |
20 |
bool run(true); |
|
304 |
20 |
std::string::const_reverse_iterator rit(fileName.rbegin()); |
|
305 |
✓✓✓✓ ✗✓✓ |
106 |
while(run && rit != fileName.rend()){ |
306 |
✓✓✗✓ ✓✓ |
86 |
if(*rit == '.' || *rit == '/') run = false; |
307 |
✓ | 66 |
else extention = *rit + extention; |
308 |
86 |
rit++; |
|
309 |
} |
||
310 |
20 |
return extention; |
|
311 |
} |
||
312 |
|||
313 |
///efface l'extension du fichier passé en paramètre |
||
314 |
/** @param fileName : nom d'un fichier |
||
315 |
* @return nom du fichier sans l'extention |
||
316 |
*/ |
||
317 |
40 |
std::string eraseExtension(const std::string & fileName){ |
|
318 |
✓ | 40 |
int nbPoint(countNbChar(fileName, '.')); |
319 |
✓✓✓ | 40 |
if(nbPoint == 0) return fileName; |
320 |
✓ | 42 |
string buffer(""); |
321 |
21 |
int findNbPoint(0); |
|
322 |
21 |
string::const_iterator it = fileName.begin(); |
|
323 |
✓✗✓✓ ✓✓ |
343 |
while(it != fileName.end() && findNbPoint < nbPoint){ |
324 |
✓✓ | 322 |
if(*it == '.'){ |
325 |
22 |
findNbPoint++; |
|
326 |
✓✓ | 22 |
if(findNbPoint < nbPoint){ |
327 |
✓ | 1 |
buffer += *it; |
328 |
} |
||
329 |
}else{ |
||
330 |
✓ | 300 |
buffer += *it; |
331 |
} |
||
332 |
322 |
it++; |
|
333 |
} |
||
334 |
21 |
return buffer; |
|
335 |
} |
||
336 |
|||
337 |
///Create the directory if not exists |
||
338 |
/** @param dirName : name of the directory |
||
339 |
* @return true on success, false otherwise |
||
340 |
*/ |
||
341 |
9 |
bool createDirectoriesIfNotExist(const std::string & dirName){ |
|
342 |
✓ | 18 |
std::list<std::string> listDir(cutStringList(dirName, '/')); |
343 |
✓ | 18 |
std::string tmpDirName(""); |
344 |
9 |
bool isNotFirst(false); |
|
345 |
✓✓ | 27 |
for(std::list<std::string>::iterator it(listDir.begin()); it != listDir.end(); ++it){ |
346 |
✓✓ | 18 |
if(isNotFirst){ |
347 |
✓ | 9 |
tmpDirName += "/"; |
348 |
} |
||
349 |
✓ | 18 |
tmpDirName += *it; |
350 |
✓✓ | 18 |
if(tmpDirName != ""){ |
351 |
✓✓✓ | 17 |
if(!isDirectoryExist(tmpDirName)){ |
352 |
✗✓ | 4 |
if(mkdir(tmpDirName.c_str(), 0755) != 0){ |
353 |
cerr << "createDirectoryIfNotExist : can't create directory '" << tmpDirName << "'" << endl; |
||
354 |
return false; |
||
355 |
} |
||
356 |
} |
||
357 |
} |
||
358 |
18 |
isNotFirst = true; |
|
359 |
} |
||
360 |
9 |
return true; |
|
361 |
} |
||
362 |
|||
363 |
Generated by: GCOVR (Version 4.2) |