Directory: | ./ |
---|---|
File: | tmp_project/PhoenixString/src/string_filename.cpp |
Date: | 2024-07-27 10:53:27 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 216 | 218 | 99.1% |
Branches: | 234 | 252 | 92.9% |
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 | 3 | bool isFileOrDirExist(const std::string & fileName){ | |
19 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
|
3 | if(fileName == "") return false; |
20 | 2 | 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 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 10 times.
|
11 | if(fileName == ""){return false;} |
29 | struct stat path_stat; | ||
30 |
2/2✓ Branch 2 taken 4 times.
✓ Branch 3 taken 6 times.
|
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 |
7/7✓ Branch 1 taken 3 times.
✓ Branch 2 taken 2 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 3 times.
✓ Branch 7 taken 2 times.
✓ Branch 10 taken 3 times.
|
5 | if(vecDirectory.size() == 0lu || fileName == ""){return "";} |
44 | 2 | std::vector<std::string>::const_iterator it(vecDirectory.begin()); | |
45 |
2/2✓ Branch 2 taken 3 times.
✓ Branch 3 taken 1 times.
|
4 | while(it != vecDirectory.end()){ |
46 |
2/2✓ Branch 2 taken 3 times.
✓ Branch 5 taken 3 times.
|
3 | std::string tmpFile(*it + "/" + fileName); |
47 |
3/3✓ Branch 1 taken 3 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 2 times.
|
3 | if(isFileExist(tmpFile)){return tmpFile;} |
48 | 2 | ++it; | |
49 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
|
3 | } |
50 |
1/1✓ Branch 2 taken 1 times.
|
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 | 15 | bool isDirectoryExist(const std::string & dirName){ | |
58 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 14 times.
|
15 | if(dirName == ""){return false;} |
59 | struct stat path_stat; | ||
60 |
2/2✓ Branch 2 taken 6 times.
✓ Branch 3 taken 8 times.
|
14 | if(stat(dirName.c_str(), &path_stat) != 0){ |
61 | 6 | return false; | |
62 | } | ||
63 | 8 | return S_ISDIR(path_stat.st_mode) != 0; | |
64 | } | ||
65 | |||
66 | ///Returns the current directory | ||
67 | /** @return current directory | ||
68 | */ | ||
69 | 5 | std::string getCurrentDirectory(){ | |
70 | #ifndef __APPLE__ | ||
71 | 5 | char* ptr = get_current_dir_name(); | |
72 |
1/1✓ Branch 2 taken 5 times.
|
5 | std::string str(ptr); |
73 | 5 | free(ptr); | |
74 | 5 | 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 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
|
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 | 6 | std::string makeAbsolutePath(const std::string & path){ | |
94 |
3/3✓ Branch 1 taken 2 times.
✓ Branch 2 taken 4 times.
✓ Branch 5 taken 2 times.
|
8 | if(path == ""){return getCurrentDirectory() + "/";} |
95 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
|
4 | else if(path[0] == '/'){return path;} |
96 | else{ | ||
97 |
2/2✓ Branch 2 taken 2 times.
✓ Branch 5 taken 2 times.
|
4 | return getCurrentDirectory() + "/" + path; |
98 | } | ||
99 | } | ||
100 | |||
101 | ///Make an absolute path of the vector of given paths | ||
102 | /** @param vecPath : vector of relative or absolute path (file or dir) | ||
103 | * @return vector of corresponding absolute path | ||
104 | */ | ||
105 | 1 | std::vector<std::string> makeAbsolutePath(const std::vector<std::string> & vecPath){ | |
106 | 1 | std::vector<std::string> vecOut; | |
107 |
2/2✓ Branch 3 taken 3 times.
✓ Branch 4 taken 1 times.
|
4 | for(std::vector<std::string>::const_iterator it(vecPath.begin()); it != vecPath.end(); ++it){ |
108 |
2/2✓ Branch 2 taken 3 times.
✓ Branch 5 taken 3 times.
|
3 | vecOut.push_back(makeAbsolutePath(*it)); |
109 | } | ||
110 | 1 | return vecOut; | |
111 | } | ||
112 | |||
113 | ///Remove dots from the path | ||
114 | /** @param path : path to be cleaned | ||
115 | * @return path wthout dots | ||
116 | */ | ||
117 | 14 | std::string removePathDots(const std::string path){ | |
118 |
3/3✓ Branch 1 taken 1 times.
✓ Branch 2 taken 13 times.
✓ Branch 4 taken 1 times.
|
14 | if(path == ""){return path;} |
119 |
3/3✓ Branch 1 taken 1 times.
✓ Branch 2 taken 12 times.
✓ Branch 4 taken 1 times.
|
13 | if(path.front() == '.'){return path;} |
120 |
1/1✓ Branch 1 taken 12 times.
|
12 | std::list<std::string> listDir(cutStringList(path, '/')); |
121 | 12 | std::list<std::string>::reverse_iterator rit = listDir.rbegin(); | |
122 |
3/3✓ Branch 2 taken 60 times.
✓ Branch 4 taken 48 times.
✓ Branch 5 taken 12 times.
|
60 | while(rit != listDir.rend()){ |
123 |
6/6✓ Branch 2 taken 44 times.
✓ Branch 3 taken 4 times.
✓ Branch 6 taken 8 times.
✓ Branch 7 taken 36 times.
✓ Branch 8 taken 12 times.
✓ Branch 9 taken 36 times.
|
48 | if(*rit == "." || *rit == ""){ |
124 | // rit = listDir.erase(rit); //No erase in reverse_iterator, so anoying | ||
125 |
1/1✓ Branch 2 taken 12 times.
|
12 | *rit = ""; |
126 |
2/2✓ Branch 2 taken 10 times.
✓ Branch 3 taken 26 times.
|
36 | }else if(*rit == ".."){ |
127 | // rit = listDir.erase(rit); //No erase in reverse_iterator, so anoying | ||
128 |
1/1✓ Branch 2 taken 10 times.
|
10 | *rit = ""; |
129 | 10 | ++rit; | |
130 |
2/3✓ Branch 2 taken 10 times.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
|
10 | if(rit != listDir.rend()){ |
131 | // rit = listDir.erase(rit); //No erase in reverse_iterator, so anoying | ||
132 |
1/1✓ Branch 2 taken 10 times.
|
10 | *rit = ""; |
133 | } | ||
134 | } | ||
135 | // else{ | ||
136 | // ++rit; //No erase in reverse_iterator, so anoying | ||
137 | // } | ||
138 | 48 | ++rit; | |
139 | } | ||
140 |
2/2✓ Branch 2 taken 12 times.
✓ Branch 6 taken 12 times.
|
24 | std::string out(""), separator(""); |
141 |
2/2✓ Branch 1 taken 6 times.
✓ Branch 2 taken 6 times.
|
12 | if(path[0] == '/'){ |
142 |
1/1✓ Branch 1 taken 6 times.
|
6 | out = "/"; |
143 | } | ||
144 |
2/2✓ Branch 4 taken 58 times.
✓ Branch 5 taken 12 times.
|
70 | for(std::list<std::string>::iterator it(listDir.begin()); it != listDir.end(); ++it){ |
145 |
2/2✓ Branch 2 taken 32 times.
✓ Branch 3 taken 26 times.
|
58 | if(*it == ""){continue;} |
146 |
2/2✓ Branch 2 taken 26 times.
✓ Branch 5 taken 26 times.
|
26 | out += separator + (*it); |
147 |
1/1✓ Branch 1 taken 26 times.
|
26 | separator = "/"; |
148 | } | ||
149 | 12 | return out; | |
150 | 12 | } | |
151 | |||
152 | ///fonction qui renvoie le dossier parent du fichier | ||
153 | /** @param fileName : nom du fichier dont on veut le dossier | ||
154 | * @return nom du dossier parent du fichier passé en paramètre | ||
155 | */ | ||
156 | 6 | std::string getDirectory(const std::string & fileName){ | |
157 |
1/1✓ Branch 2 taken 6 times.
|
6 | std::string buffer(""); |
158 | 6 | bool find(false); | |
159 | 6 | std::string::const_reverse_iterator rit = fileName.rbegin(); | |
160 |
3/3✓ Branch 2 taken 600 times.
✓ Branch 4 taken 594 times.
✓ Branch 5 taken 6 times.
|
600 | while(rit != fileName.rend()){ |
161 |
2/2✓ Branch 0 taken 513 times.
✓ Branch 1 taken 81 times.
|
594 | if(find){ |
162 |
1/1✓ Branch 2 taken 513 times.
|
513 | buffer = *rit + buffer; |
163 | }else{ | ||
164 | 81 | find = (*rit == '/'); | |
165 | } | ||
166 |
1/1✓ Branch 1 taken 594 times.
|
594 | rit++; |
167 | } | ||
168 | 12 | return buffer; | |
169 | } | ||
170 | |||
171 | ///Get path which is under the given pathPart ('some/dir/path' with 'dir' will return 'path') | ||
172 | /** @param fileName : path to be used | ||
173 | * @param pathPart : directory in the fileName we are looking for | ||
174 | * @return rest of the path under the pathPart | ||
175 | */ | ||
176 | 5 | std::string getUnderPath(const std::string & fileName, const std::string & pathPart){ | |
177 |
3/3✓ Branch 1 taken 1 times.
✓ Branch 2 taken 4 times.
✓ Branch 4 taken 1 times.
|
5 | if(pathPart == ""){return fileName;} |
178 |
1/1✓ Branch 1 taken 4 times.
|
4 | std::list<std::string> listDir(cutStringList(fileName, '/')); |
179 | 4 | std::list<std::string>::iterator it(listDir.begin()); | |
180 | 4 | bool isSearch(true); | |
181 |
6/6✓ Branch 0 taken 12 times.
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 11 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 11 times.
✓ Branch 7 taken 4 times.
|
15 | while(isSearch && it != listDir.end()){ |
182 | 11 | isSearch = *it != pathPart; | |
183 | 11 | ++it; | |
184 | } | ||
185 |
2/2✓ Branch 2 taken 4 times.
✓ Branch 6 taken 4 times.
|
8 | std::string out(""), separator(""); |
186 |
2/2✓ Branch 2 taken 5 times.
✓ Branch 3 taken 4 times.
|
9 | while(it != listDir.end()){ |
187 |
2/2✓ Branch 2 taken 5 times.
✓ Branch 5 taken 5 times.
|
5 | out += separator + (*it); |
188 |
1/1✓ Branch 1 taken 5 times.
|
5 | separator = "/"; |
189 | 5 | ++it; | |
190 | } | ||
191 | 4 | return out; | |
192 | 4 | } | |
193 | |||
194 | ///Get path which is under the given pathPart ('some/dir/path' with 'dir' will return 'path') | ||
195 | /** @param vecFileName : vector of paths to be used | ||
196 | * @param pathPart : directory in the fileName we are looking for | ||
197 | * @return vector of the rest of the path under the pathPart | ||
198 | */ | ||
199 | 1 | std::vector<std::string> getUnderPath(const std::vector<std::string> & vecFileName, const std::string & pathPart){ | |
200 | 1 | std::vector<std::string> vecOut; | |
201 |
2/2✓ Branch 3 taken 2 times.
✓ Branch 4 taken 1 times.
|
3 | for(std::vector<std::string>::const_iterator it(vecFileName.begin()); it != vecFileName.end(); ++it){ |
202 |
2/2✓ Branch 2 taken 2 times.
✓ Branch 5 taken 2 times.
|
2 | vecOut.push_back(getUnderPath(*it, pathPart)); |
203 | } | ||
204 | 1 | return vecOut; | |
205 | } | ||
206 | |||
207 | ///Check if the given file starts with the given begning | ||
208 | /** @param fileName : name of the file to be checked | ||
209 | * @param expectedBegining : expected begening of the file | ||
210 | * @return true if the file starts with the expected begning, false otherwise | ||
211 | */ | ||
212 | 4 | bool checkFileBegning(const std::string & fileName, const std::string & expectedBegining){ | |
213 | 4 | FILE* fp = fopen(fileName.c_str(), "r"); | |
214 |
5/6✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 2 times.
|
4 | if(fp == NULL || expectedBegining.size() == 0lu){return false;} |
215 | 2 | int val(0); | |
216 | 2 | size_t i(0lu); | |
217 | 2 | bool isMatch(true); | |
218 |
7/8✓ Branch 0 taken 40 times.
✓ Branch 1 taken 1 times.
✓ Branch 3 taken 40 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 39 times.
✓ Branch 7 taken 1 times.
✓ Branch 8 taken 39 times.
✓ Branch 9 taken 2 times.
|
41 | while(isMatch && !feof(fp) && i < expectedBegining.size()){ |
219 | 39 | val = fgetc(fp); | |
220 | 39 | isMatch = (char)val == expectedBegining[i]; | |
221 | 39 | ++i; | |
222 | } | ||
223 | 2 | fclose(fp); | |
224 | 2 | return isMatch; | |
225 | } | ||
226 | |||
227 | ///fonction qui renvoie le nom du fichier du nom complet de fichier passé en paramètre | ||
228 | /** @param fileName : nom complet de fichier dont on veut le nom (/nom/du/fichier -> fichier) | ||
229 | * @return nom du fichier passé en paramètre | ||
230 | */ | ||
231 | 3 | std::string getFileName(const std::string & fileName){ | |
232 |
1/1✓ Branch 2 taken 3 times.
|
3 | std::string buffer(""); |
233 | 3 | std::string::const_reverse_iterator rit = fileName.rbegin(); | |
234 |
2/3✓ Branch 2 taken 31 times.
✓ Branch 4 taken 31 times.
✗ Branch 5 not taken.
|
31 | while(rit != fileName.rend()){ |
235 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 2 taken 28 times.
|
31 | if(*rit == '/') break; |
236 |
1/1✓ Branch 2 taken 28 times.
|
28 | buffer = *rit + buffer; |
237 |
1/1✓ Branch 1 taken 28 times.
|
28 | rit++; |
238 | } | ||
239 | 6 | return buffer; | |
240 | } | ||
241 | |||
242 | ///Get the name of the deeper directory | ||
243 | /** @param path : input path | ||
244 | * @return name of the deeper directory | ||
245 | */ | ||
246 | 2 | std::string getDirName(const std::string & path){ | |
247 |
1/1✓ Branch 2 taken 2 times.
|
2 | std::string buffer(""); |
248 | 2 | std::string::const_reverse_iterator rit = path.rbegin(); | |
249 |
2/3✓ Branch 2 taken 11 times.
✓ Branch 4 taken 11 times.
✗ Branch 5 not taken.
|
11 | while(rit != path.rend()){ |
250 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 2 taken 8 times.
|
11 | if(*rit == '/'){ |
251 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
|
3 | if(buffer != ""){ |
252 | 2 | break; | |
253 | }else{ | ||
254 |
1/1✓ Branch 1 taken 1 times.
|
1 | rit++; |
255 | 1 | continue; | |
256 | } | ||
257 | } | ||
258 |
1/1✓ Branch 2 taken 8 times.
|
8 | buffer = *rit + buffer; |
259 |
1/1✓ Branch 1 taken 8 times.
|
8 | rit++; |
260 | } | ||
261 | 4 | return buffer; | |
262 | } | ||
263 | |||
264 | ///Get the file content in a string | ||
265 | /** @param filename : file name | ||
266 | * @return string content of the file | ||
267 | */ | ||
268 | 3 | std::string getFileContent(const std::string & filename){ | |
269 |
1/1✓ Branch 2 taken 3 times.
|
3 | FILE * fp = fopen(filename.c_str(), "r"); |
270 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
|
3 | if(fp == NULL){ |
271 |
4/4✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 2 times.
✓ Branch 10 taken 2 times.
|
2 | std::cerr << "getFileContent : cannot open file '" << filename << "'" << std::endl; |
272 |
1/1✓ Branch 2 taken 2 times.
|
2 | return ""; |
273 | } | ||
274 |
1/1✓ Branch 1 taken 1 times.
|
1 | std::string tmp(getFileContent(fp)); |
275 |
1/1✓ Branch 1 taken 1 times.
|
1 | fclose(fp); |
276 | 1 | return tmp; | |
277 | 1 | } | |
278 | |||
279 | ///Get the file content in a string | ||
280 | /** @param fp : pointer to the file | ||
281 | * @return string content of the file | ||
282 | */ | ||
283 | 34 | std::string getFileContent(FILE* fp){ | |
284 |
3/3✓ Branch 0 taken 1 times.
✓ Branch 1 taken 33 times.
✓ Branch 4 taken 1 times.
|
34 | if(fp == NULL) return ""; |
285 |
1/1✓ Branch 2 taken 33 times.
|
33 | std::string bufferAllFile(""); |
286 | int buffer; | ||
287 |
1/2✓ Branch 1 taken 11548 times.
✗ Branch 2 not taken.
|
11548 | while(!feof(fp)){ |
288 |
1/1✓ Branch 1 taken 11548 times.
|
11548 | buffer = fgetc(fp); |
289 |
3/3✓ Branch 0 taken 11515 times.
✓ Branch 1 taken 33 times.
✓ Branch 3 taken 11515 times.
|
11548 | if(buffer != EOF) bufferAllFile += (char)buffer; |
290 | 33 | else break; | |
291 | } | ||
292 | 33 | return bufferAllFile; | |
293 | 33 | } | |
294 | |||
295 | ///Save a string in a file | ||
296 | /** @param filename : name of the file to be written | ||
297 | * @param content : file content | ||
298 | * @return true on success, false otherwise | ||
299 | */ | ||
300 | 6 | bool saveFileContent(const std::string & filename, const std::string & content){ | |
301 | 6 | FILE * fp = fopen(filename.c_str(), "w"); | |
302 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 5 times.
|
6 | if(fp == NULL){ |
303 | 1 | std::cerr << "saveFileContent : cannot open file '" << filename << "'" << std::endl; | |
304 | 1 | return false; | |
305 | } | ||
306 | 5 | bool result(saveFileContent(fp, content)); | |
307 | 5 | fclose(fp); | |
308 | 5 | return result; | |
309 | } | ||
310 | |||
311 | ///Save a string in a file | ||
312 | /** @param fp : pointer to the file to be written | ||
313 | * @param content : file content | ||
314 | * @return true on success, false otherwise | ||
315 | */ | ||
316 | 7 | bool saveFileContent(FILE* fp, const std::string & content){ | |
317 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5 times.
|
7 | if(fp == NULL) return false; |
318 | 5 | fprintf(fp, "%s", content.c_str()); | |
319 | 5 | return true; | |
320 | } | ||
321 | |||
322 | ///Get file extention | ||
323 | /** @param fileName : input file name | ||
324 | * @return file extention | ||
325 | */ | ||
326 | 3 | std::string getExtention(const std::string & fileName){ | |
327 |
4/4✓ Branch 1 taken 3 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 2 times.
✓ Branch 7 taken 1 times.
|
3 | if(!findInString(fileName, '.')) return ""; |
328 |
1/1✓ Branch 2 taken 2 times.
|
2 | std::string extention(""); |
329 | 2 | bool run(true); | |
330 | 2 | std::string::const_reverse_iterator rit(fileName.rbegin()); | |
331 |
6/7✓ Branch 0 taken 14 times.
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 14 times.
✓ Branch 6 taken 14 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 14 times.
✓ Branch 9 taken 2 times.
|
16 | while(run && rit != fileName.rend()){ |
332 |
5/6✓ Branch 1 taken 12 times.
✓ Branch 2 taken 2 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 12 times.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 12 times.
|
14 | if(*rit == '.' || *rit == '/') run = false; |
333 |
1/1✓ Branch 2 taken 12 times.
|
12 | else extention = *rit + extention; |
334 |
1/1✓ Branch 1 taken 14 times.
|
14 | rit++; |
335 | } | ||
336 | 2 | return extention; | |
337 | 2 | } | |
338 | |||
339 | ///Get the longest file extention | ||
340 | /** @param fileName : input file name | ||
341 | * @return longest file extention | ||
342 | */ | ||
343 | 7 | std::string getLongestExtention(const std::string & fileName){ | |
344 |
1/1✓ Branch 1 taken 7 times.
|
7 | size_t nbPoint(countNbChar(fileName, '.')); |
345 |
3/3✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5 times.
✓ Branch 4 taken 2 times.
|
7 | if(nbPoint == 0lu) return ""; |
346 |
3/3✓ Branch 2 taken 5 times.
✓ Branch 6 taken 5 times.
✓ Branch 10 taken 5 times.
|
15 | std::string extention(""), tmpExtention(""), middleDot(""); |
347 | 5 | bool run(true); | |
348 | 5 | std::string::const_reverse_iterator rit(fileName.rbegin()); | |
349 |
7/7✓ Branch 0 taken 71 times.
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 71 times.
✓ Branch 6 taken 69 times.
✓ Branch 7 taken 2 times.
✓ Branch 8 taken 69 times.
✓ Branch 9 taken 5 times.
|
74 | while(run && rit != fileName.rend()){ |
350 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 2 taken 66 times.
|
69 | if(*rit == '/'){run = false;} |
351 |
2/2✓ Branch 1 taken 9 times.
✓ Branch 2 taken 57 times.
|
66 | else if(*rit == '.'){ |
352 |
2/2✓ Branch 1 taken 9 times.
✓ Branch 4 taken 9 times.
|
9 | extention = tmpExtention + middleDot + extention; |
353 |
1/1✓ Branch 1 taken 9 times.
|
9 | tmpExtention = ""; |
354 |
1/1✓ Branch 1 taken 9 times.
|
9 | middleDot = "."; |
355 | }else{ | ||
356 |
1/1✓ Branch 2 taken 57 times.
|
57 | tmpExtention = *rit + tmpExtention; |
357 | } | ||
358 | |||
359 |
1/1✓ Branch 1 taken 69 times.
|
69 | rit++; |
360 | } | ||
361 | 5 | return extention; | |
362 | 5 | } | |
363 | |||
364 | ///Erase extention of the given file | ||
365 | /** @param fileName : input file name | ||
366 | * @return filen name without extention | ||
367 | */ | ||
368 | 4 | std::string eraseExtension(const std::string & fileName){ | |
369 |
1/1✓ Branch 1 taken 4 times.
|
4 | int nbPoint(countNbChar(fileName, '.')); |
370 |
3/3✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
✓ Branch 3 taken 1 times.
|
4 | if(nbPoint == 0) return fileName; |
371 |
1/1✓ Branch 2 taken 3 times.
|
3 | std::string buffer(""); |
372 | 3 | int findNbPoint(0); | |
373 | 3 | std::string::const_iterator it = fileName.begin(); | |
374 |
5/6✓ Branch 2 taken 55 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 52 times.
✓ Branch 5 taken 3 times.
✓ Branch 6 taken 52 times.
✓ Branch 7 taken 3 times.
|
55 | while(it != fileName.end() && findNbPoint < nbPoint){ |
375 |
2/2✓ Branch 1 taken 4 times.
✓ Branch 2 taken 48 times.
|
52 | if(*it == '.'){ |
376 | 4 | findNbPoint++; | |
377 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
|
4 | if(findNbPoint < nbPoint){ |
378 |
1/1✓ Branch 2 taken 1 times.
|
1 | buffer += *it; |
379 | } | ||
380 | }else{ | ||
381 |
1/1✓ Branch 2 taken 48 times.
|
48 | buffer += *it; |
382 | } | ||
383 | 52 | it++; | |
384 | } | ||
385 | 3 | return buffer; | |
386 | 3 | } | |
387 | |||
388 | ///Erase longest extention of the given file | ||
389 | /** @param fileName : input file name | ||
390 | * @return filen name without longest extention | ||
391 | */ | ||
392 | 3 | std::string eraseLongestExtension(const std::string & fileName){ | |
393 |
1/1✓ Branch 1 taken 3 times.
|
3 | std::string longestExtention(getLongestExtention(fileName)); |
394 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
|
3 | if(longestExtention != ""){ |
395 |
1/1✓ Branch 3 taken 2 times.
|
2 | return copyStr(fileName, 0lu, fileName.size() - (longestExtention.size() + 1lu)); |
396 | }else{ | ||
397 |
1/1✓ Branch 1 taken 1 times.
|
1 | return fileName; |
398 | } | ||
399 | 3 | } | |
400 | |||
401 | ///Create the directory if not exists | ||
402 | /** @param dirName : name of the directory | ||
403 | * @return true on success, false otherwise | ||
404 | */ | ||
405 | 3 | bool createDirectoriesIfNotExist(const std::string & dirName){ | |
406 |
1/1✓ Branch 1 taken 3 times.
|
3 | std::list<std::string> listDir(cutStringList(dirName, '/')); |
407 |
1/1✓ Branch 2 taken 3 times.
|
3 | std::string tmpDirName(""); |
408 | 3 | bool isNotFirst(false); | |
409 |
2/2✓ Branch 3 taken 6 times.
✓ Branch 4 taken 3 times.
|
9 | for(std::list<std::string>::iterator it(listDir.begin()); it != listDir.end(); ++it){ |
410 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | if(isNotFirst){ |
411 |
1/1✓ Branch 1 taken 3 times.
|
3 | tmpDirName += "/"; |
412 | } | ||
413 |
1/1✓ Branch 2 taken 6 times.
|
6 | tmpDirName += *it; |
414 |
2/2✓ Branch 1 taken 5 times.
✓ Branch 2 taken 1 times.
|
6 | if(tmpDirName != ""){ |
415 |
3/3✓ Branch 1 taken 5 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 3 times.
|
5 | if(!isDirectoryExist(tmpDirName)){ |
416 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
|
2 | if(mkdir(tmpDirName.c_str(), 0755) != 0){ |
417 | ✗ | cerr << "createDirectoryIfNotExist : can't create directory '" << tmpDirName << "'" << endl; | |
418 | ✗ | return false; | |
419 | } | ||
420 | } | ||
421 | } | ||
422 | 6 | isNotFirst = true; | |
423 | } | ||
424 | 3 | return true; | |
425 | 3 | } | |
426 | |||
427 | |||
428 |