Directory: | ./ |
---|---|
File: | tmp_project/PhoenixCore/src/PPath.cpp |
Date: | 2025-03-14 11:56:07 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 308 | 335 | 91.9% |
Branches: | 311 | 388 | 80.2% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /*************************************** | ||
2 | Auteur : Pierre Aubert | ||
3 | Mail : pierre.aubert@lapp.in2p3.fr | ||
4 | Licence : CeCILL-C | ||
5 | ****************************************/ | ||
6 | |||
7 | #include <errno.h> | ||
8 | #include <stdio.h> | ||
9 | #include <regex.h> | ||
10 | #include <fcntl.h> //Definition of AT_* constants | ||
11 | |||
12 | #include <unistd.h> //for get_current_dir_name | ||
13 | #include <dirent.h> //for opendir | ||
14 | |||
15 | #include <sys/stat.h> | ||
16 | #include <sys/types.h> | ||
17 | #include <stdlib.h> | ||
18 | #include <unistd.h> | ||
19 | |||
20 | #include "phoenix_env.h" | ||
21 | #include "phoenix_system.h" | ||
22 | #include "PPath.h" | ||
23 | |||
24 | ///Get the content of a file | ||
25 | /** @param fp : file pointer to be used | ||
26 | * @return content of file fp | ||
27 | */ | ||
28 | 48 | PString phoenix_getFileContent(FILE* fp){ | |
29 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
|
48 | if(fp == NULL){return "";} |
30 | //Let's get the size of the file | ||
31 | 48 | fseek(fp, 0, SEEK_END); | |
32 | 48 | long fileOffset(ftell(fp)); | |
33 | 48 | fseek(fp, 0, SEEK_SET); | |
34 | //Is the file is empty we quit | ||
35 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
|
48 | if(fileOffset == 0l){ |
36 | ✗ | return ""; | |
37 | } | ||
38 | //If the size of the file is -1lu this should be a stream which has no defined end yet, such as the popen output | ||
39 |
2/2✓ Branch 0 taken 37 times.
✓ Branch 1 taken 11 times.
|
48 | if(fileOffset == -1l){ |
40 | //So we have to use the old method which consist to read the stream character per character until EOF | ||
41 | //We have to use a std::string instead of PString to have smarter resize strategy | ||
42 |
1/1✓ Branch 2 taken 37 times.
|
37 | std::string strBuffer(""); |
43 | int buffer; | ||
44 |
1/2✓ Branch 1 taken 1736 times.
✗ Branch 2 not taken.
|
1736 | while(!feof(fp)){ |
45 |
1/1✓ Branch 1 taken 1736 times.
|
1736 | buffer = fgetc(fp); |
46 |
3/3✓ Branch 0 taken 1699 times.
✓ Branch 1 taken 37 times.
✓ Branch 3 taken 1699 times.
|
1736 | if(buffer != EOF) strBuffer += (char)buffer; |
47 | 37 | else break; | |
48 | } | ||
49 |
1/1✓ Branch 1 taken 37 times.
|
37 | return strBuffer; |
50 | 37 | }else{ | |
51 | 11 | size_t fileSize(fileOffset); | |
52 | //If we know the size of the file, we can resize our PString once for all and load the content with fread | ||
53 |
1/1✓ Branch 1 taken 11 times.
|
11 | PString bufferAllFile; |
54 |
1/1✓ Branch 1 taken 11 times.
|
11 | bufferAllFile.resize(fileSize); |
55 |
1/1✓ Branch 2 taken 11 times.
|
11 | if(fread(bufferAllFile.data(), sizeof(char), fileSize, fp) == fileSize){ |
56 | |||
57 | } | ||
58 |
1/1✓ Branch 1 taken 11 times.
|
11 | return bufferAllFile; |
59 | 11 | } | |
60 | } | ||
61 | |||
62 | |||
63 | ///Default constructor of PPath | ||
64 | 28 | PPath::PPath() | |
65 | 28 | :PString() | |
66 | { | ||
67 |
1/1✓ Branch 1 taken 28 times.
|
28 | initialisationPPath(); |
68 | 28 | } | |
69 | |||
70 | ///Constructor of PPath | ||
71 | /** @param other : PString | ||
72 | */ | ||
73 | 274 | PPath::PPath(const PString & other) | |
74 | 274 | :PString(other) | |
75 | { | ||
76 |
1/1✓ Branch 1 taken 274 times.
|
274 | initialisationPPath(); |
77 | 274 | } | |
78 | |||
79 | ///Copy constructor of PPath | ||
80 | /** @param other : class to copy | ||
81 | */ | ||
82 | 101 | PPath::PPath(const PPath & other) | |
83 | 101 | :PString(other) | |
84 | { | ||
85 |
1/1✓ Branch 1 taken 101 times.
|
101 | copyPPath(other); |
86 | 101 | } | |
87 | |||
88 | ///Destructor of PPath | ||
89 | 806 | PPath::~PPath(){ | |
90 | |||
91 | } | ||
92 | |||
93 | ///Definition of equal operator of PPath | ||
94 | /** @param other : class to copy | ||
95 | * @return copied class | ||
96 | */ | ||
97 | 23 | PPath & PPath::operator = (const PPath & other){ | |
98 | 23 | copyPString(other); | |
99 | 23 | copyPPath(other); | |
100 | 23 | return *this; | |
101 | } | ||
102 | |||
103 | ///Operator + for PPath | ||
104 | /** @param other1 : PPath | ||
105 | * @param other2 : PPath | ||
106 | * @return other1 + other2 | ||
107 | */ | ||
108 | ✗ | PPath operator + (const PPath & other1, const PPath & other2){ | |
109 | ✗ | PPath out(other1); | |
110 | ✗ | out += other2; | |
111 | ✗ | return out; | |
112 | } | ||
113 | |||
114 | ///Operator / for PPath to concatenate PPath | ||
115 | /** @param other1 : PPath | ||
116 | * @param other2 : PPath | ||
117 | * @return other1 / other2 | ||
118 | */ | ||
119 | 3 | PPath operator / (const PPath & other1, const PPath & other2){ | |
120 | 3 | PPath out(other1); | |
121 |
1/2✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
|
3 | if(other1.size() != 0lu){ |
122 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
|
3 | out += PString("/"); |
123 | } | ||
124 |
1/1✓ Branch 1 taken 3 times.
|
3 | out += other2; |
125 | 3 | return out; | |
126 | } | ||
127 | |||
128 | ///Say if the current path does exist | ||
129 | /** @return if the current path does exist | ||
130 | */ | ||
131 | 2 | bool PPath::isExist() const{ | |
132 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
|
2 | if(size() == 0lu) return false; |
133 | 1 | return access(c_str(), F_OK) != -1; | |
134 | } | ||
135 | |||
136 | ///Say if the current file path does exist | ||
137 | /** @return if the current file path does exist | ||
138 | */ | ||
139 | 17 | bool PPath::isFileExist() const{ | |
140 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
|
17 | if(size() == 0lu) return false; |
141 | struct stat path_stat; | ||
142 |
2/2✓ Branch 2 taken 3 times.
✓ Branch 3 taken 14 times.
|
17 | if(stat(c_str(), &path_stat) != 0){ |
143 | 3 | return false; | |
144 | } | ||
145 | 14 | return S_ISREG(path_stat.st_mode) != 0; | |
146 | } | ||
147 | |||
148 | ///Say if the current directory path does exist | ||
149 | /** @return if the current directory path does exist | ||
150 | */ | ||
151 | 17 | bool PPath::isDirectoryExist() const{ | |
152 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
|
17 | if(size() == 0lu) return false; |
153 | struct stat path_stat; | ||
154 |
2/2✓ Branch 2 taken 11 times.
✓ Branch 3 taken 6 times.
|
17 | if(stat(c_str(), &path_stat) != 0){ |
155 | 11 | return false; | |
156 | } | ||
157 | 6 | return S_ISDIR(path_stat.st_mode) != 0; | |
158 | } | ||
159 | |||
160 | ///Tel if a path is absolute or not | ||
161 | /** @param path : path to be checked | ||
162 | * @return true if the path is absolute, false otherwise | ||
163 | */ | ||
164 | 4 | bool PPath::isAbsolutePath() const{ | |
165 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
|
4 | if(size() == 0lu) return false; |
166 | 4 | return front() == '/'; | |
167 | } | ||
168 | |||
169 | ///Get the name of the file, from last char to / | ||
170 | /** @return name of the corresponding file | ||
171 | */ | ||
172 | 17 | PPath PPath::getFileName() const{ | |
173 |
1/1✓ Branch 2 taken 17 times.
|
17 | std::string buffer(""); |
174 | 17 | std::string::const_reverse_iterator rit = rbegin(); | |
175 |
3/3✓ Branch 2 taken 233 times.
✓ Branch 4 taken 230 times.
✓ Branch 5 taken 3 times.
|
233 | while(rit != rend()){ |
176 |
2/2✓ Branch 1 taken 14 times.
✓ Branch 2 taken 216 times.
|
230 | if(*rit == '/') break; |
177 |
1/1✓ Branch 2 taken 216 times.
|
216 | buffer = *rit + buffer; |
178 |
1/1✓ Branch 1 taken 216 times.
|
216 | rit++; |
179 | } | ||
180 |
2/2✓ Branch 1 taken 17 times.
✓ Branch 4 taken 17 times.
|
34 | return PPath(buffer); |
181 | 17 | } | |
182 | |||
183 | ///Get the name of the directory, from last char to / (if the last char is not a /, otherwise it takes the chars before last /) | ||
184 | /** @return name of the corresponding directory | ||
185 | */ | ||
186 | 3 | PPath PPath::getDirectoryName() const{ | |
187 |
1/1✓ Branch 2 taken 3 times.
|
3 | std::string buffer(""); |
188 | 3 | std::string::const_reverse_iterator rit = rbegin(); | |
189 |
3/3✓ Branch 2 taken 22 times.
✓ Branch 4 taken 21 times.
✓ Branch 5 taken 1 times.
|
22 | while(rit != rend()){ |
190 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 2 taken 18 times.
|
21 | if(*rit == '/'){ |
191 |
3/3✓ Branch 1 taken 3 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 1 times.
|
3 | if(buffer != ""){ |
192 | 2 | break; | |
193 | }else{ | ||
194 |
1/1✓ Branch 1 taken 1 times.
|
1 | rit++; |
195 | 1 | continue; | |
196 | } | ||
197 | } | ||
198 |
1/1✓ Branch 2 taken 18 times.
|
18 | buffer = *rit + buffer; |
199 |
1/1✓ Branch 1 taken 18 times.
|
18 | rit++; |
200 | } | ||
201 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
|
6 | return PPath(buffer); |
202 | 3 | } | |
203 | |||
204 | ///Get path of parent directory of current path | ||
205 | /** @return parent directory of current path | ||
206 | */ | ||
207 | 14 | PPath PPath::getParentDirectory() const{ | |
208 |
4/4✓ Branch 1 taken 1 times.
✓ Branch 2 taken 13 times.
✓ Branch 4 taken 1 times.
✓ Branch 7 taken 1 times.
|
15 | if(size() == 0lu){return PString("");} |
209 |
1/1✓ Branch 2 taken 13 times.
|
13 | std::string buffer(""); |
210 | 13 | bool find(false), isLastSlash(back() == '/'); | |
211 | 13 | std::string::const_reverse_iterator rit = rbegin(); | |
212 |
3/3✓ Branch 2 taken 1057 times.
✓ Branch 4 taken 1044 times.
✓ Branch 5 taken 13 times.
|
1057 | while(rit != rend()){ |
213 |
2/2✓ Branch 0 taken 911 times.
✓ Branch 1 taken 133 times.
|
1044 | if(find){ |
214 |
1/1✓ Branch 2 taken 911 times.
|
911 | buffer = *rit + buffer; |
215 | }else{ | ||
216 |
4/4✓ Branch 1 taken 18 times.
✓ Branch 2 taken 115 times.
✓ Branch 3 taken 13 times.
✓ Branch 4 taken 5 times.
|
133 | find = (*rit == '/' && !isLastSlash); |
217 | 133 | isLastSlash = *rit == '/'; | |
218 | } | ||
219 |
1/1✓ Branch 1 taken 1044 times.
|
1044 | rit++; |
220 | } | ||
221 |
2/2✓ Branch 1 taken 13 times.
✓ Branch 4 taken 13 times.
|
13 | return PPath(buffer); |
222 | 13 | } | |
223 | |||
224 | ///Get the parent directory which contains subDirName | ||
225 | /** @param subDirName : name of the searched sub-directory | ||
226 | * @return corresponding PPath (or empty if subDirName was not found) | ||
227 | */ | ||
228 | ✗ | PPath PPath::getParentDirectory(const PPath & subDirName) const{ | |
229 | ✗ | if(size() == 0lu || !isDirectoryExist()){return *this;} | |
230 | ✗ | PPath curDir(*this); | |
231 | ✗ | std::string tmp(curDir / subDirName); | |
232 | ✗ | DIR * dp = opendir(tmp.c_str()); | |
233 | ✗ | while(NULL == dp && curDir.size() > 1lu){ | |
234 | ✗ | curDir = curDir.getParentDirectory(); | |
235 | ✗ | tmp = curDir / subDirName; | |
236 | ✗ | dp = opendir(tmp.c_str()); | |
237 | } | ||
238 | ✗ | return curDir; | |
239 | |||
240 | |||
241 | // PPath curDir(*this); | ||
242 | // PPath tmpDir(curDir / subDirName); | ||
243 | // while(!curDir.isDirectoryExist() && curDir.size() > subDirName.size()){ | ||
244 | // curDir = curDir.getParentDirectory() / subDirName; | ||
245 | // } | ||
246 | // return curDir; | ||
247 | } | ||
248 | |||
249 | ///Get file extension | ||
250 | /** @return file extension | ||
251 | */ | ||
252 | 22 | PString PPath::getExtension() const{ | |
253 |
4/4✓ Branch 1 taken 22 times.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 18 times.
✓ Branch 6 taken 4 times.
|
22 | if(!find('.')) return ""; |
254 |
1/1✓ Branch 2 taken 18 times.
|
18 | std::string extension(""); |
255 | 18 | bool run(true); | |
256 | 18 | std::string::const_reverse_iterator rit(rbegin()); | |
257 |
6/7✓ Branch 0 taken 72 times.
✓ Branch 1 taken 18 times.
✓ Branch 4 taken 72 times.
✓ Branch 6 taken 72 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 72 times.
✓ Branch 9 taken 18 times.
|
90 | while(run && rit != rend()){ |
258 |
5/6✓ Branch 1 taken 54 times.
✓ Branch 2 taken 18 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 54 times.
✓ Branch 6 taken 18 times.
✓ Branch 7 taken 54 times.
|
72 | if(*rit == '.' || *rit == '/') run = false; |
259 |
1/1✓ Branch 2 taken 54 times.
|
54 | else extension = *rit + extension; |
260 |
1/1✓ Branch 1 taken 72 times.
|
72 | rit++; |
261 | } | ||
262 |
1/1✓ Branch 1 taken 18 times.
|
18 | return PString(extension); |
263 | 18 | } | |
264 | |||
265 | ///Get the longest file extension | ||
266 | /** @return longest file extension | ||
267 | */ | ||
268 | 7 | PString PPath::getLongestExtension() const{ | |
269 |
1/1✓ Branch 1 taken 7 times.
|
7 | size_t nbPoint(count('.')); |
270 |
3/3✓ Branch 0 taken 3 times.
✓ Branch 1 taken 4 times.
✓ Branch 3 taken 3 times.
|
7 | if(nbPoint == 0lu) return ""; |
271 |
3/3✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
✓ Branch 7 taken 4 times.
|
4 | PString extension(""), tmpExtension(""), middleDot(""); |
272 | 4 | bool run(true); | |
273 | 4 | PString::const_reverse_iterator rit(rbegin()); | |
274 |
6/7✓ Branch 0 taken 62 times.
✓ Branch 1 taken 4 times.
✓ Branch 4 taken 62 times.
✓ Branch 6 taken 62 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 62 times.
✓ Branch 9 taken 4 times.
|
66 | while(run && rit != rend()){ |
275 |
2/2✓ Branch 1 taken 4 times.
✓ Branch 2 taken 58 times.
|
62 | if(*rit == '/'){run = false;} |
276 |
2/2✓ Branch 1 taken 6 times.
✓ Branch 2 taken 52 times.
|
58 | else if(*rit == '.'){ |
277 |
3/3✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
✓ Branch 7 taken 6 times.
|
6 | extension = tmpExtension + middleDot + extension; |
278 |
1/1✓ Branch 1 taken 6 times.
|
6 | tmpExtension = ""; |
279 |
1/1✓ Branch 1 taken 6 times.
|
6 | middleDot = "."; |
280 | }else{ | ||
281 |
2/2✓ Branch 2 taken 52 times.
✓ Branch 5 taken 52 times.
|
52 | tmpExtension = *rit + tmpExtension; |
282 | } | ||
283 | |||
284 |
1/1✓ Branch 1 taken 62 times.
|
62 | rit++; |
285 | } | ||
286 |
1/1✓ Branch 1 taken 4 times.
|
4 | return extension; |
287 | 4 | } | |
288 | |||
289 | ///Erase the extension of the PPath | ||
290 | /** @return PPath without extension | ||
291 | */ | ||
292 | 5 | PPath & PPath::eraseExtension(){ | |
293 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 4 times.
|
5 | if(size() <= 1lu){return *this;} |
294 |
1/1✓ Branch 1 taken 4 times.
|
4 | PString extension(getExtension()); |
295 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 3 times.
|
4 | if(extension.size() == 0lu){return *this;} |
296 |
1/1✓ Branch 3 taken 3 times.
|
3 | resize(size() - extension.size() - 1lu); |
297 | 3 | return *this; | |
298 | 4 | } | |
299 | |||
300 | ///Erase the longest extension of the PPath | ||
301 | /** @return PPath without longest extension | ||
302 | */ | ||
303 | 4 | PPath & PPath::eraseLongestExtension(){ | |
304 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 3 times.
|
4 | if(size() <= 1lu){return *this;} |
305 |
1/1✓ Branch 1 taken 3 times.
|
3 | PString extension(getLongestExtension()); |
306 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
|
3 | if(extension.size() == 0lu){return *this;} |
307 |
1/1✓ Branch 3 taken 2 times.
|
2 | resize(size() - extension.size() - 1lu); |
308 | 2 | return *this; | |
309 | 3 | } | |
310 | |||
311 | ///Erase the extension of the PPath | ||
312 | /** @return PPath without extension | ||
313 | */ | ||
314 | ✗ | PPath PPath::eraseExtension() const{ | |
315 | ✗ | PPath out(*this); | |
316 | ✗ | return out.eraseExtension(); | |
317 | } | ||
318 | |||
319 | ///Erase the longest extension of the PPath | ||
320 | /** @return PPath without longest extension | ||
321 | */ | ||
322 | ✗ | PPath PPath::eraseLongestExtension() const{ | |
323 | ✗ | PPath out(*this); | |
324 | ✗ | return out.eraseLongestExtension(); | |
325 | } | ||
326 | |||
327 | ///Create the current directory | ||
328 | /** @param mode : access mode of the created directory | ||
329 | * @return true on success, false otherwise | ||
330 | */ | ||
331 | 4 | bool PPath::createDirectory(mode_t mode) const{ | |
332 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 3 times.
|
4 | if(size() == 0lu){return false;} |
333 |
3/3✓ Branch 1 taken 3 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 2 times.
|
3 | else if(isDirectoryExist()){return true;} |
334 | |||
335 |
1/1✓ Branch 1 taken 2 times.
|
2 | std::vector<PString> listDir(split('/')); |
336 |
1/1✓ Branch 1 taken 2 times.
|
2 | PPath tmpDirName; |
337 | 2 | bool isNotFirst(false); | |
338 |
2/2✓ Branch 3 taken 6 times.
✓ Branch 4 taken 2 times.
|
8 | for(std::vector<PString>::iterator it(listDir.begin()); it != listDir.end(); ++it){ |
339 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
|
6 | if(isNotFirst){ |
340 |
1/1✓ Branch 1 taken 4 times.
|
4 | tmpDirName += "/"; |
341 | } | ||
342 |
1/1✓ Branch 2 taken 6 times.
|
6 | tmpDirName += *it; |
343 |
2/3✓ Branch 1 taken 6 times.
✓ Branch 3 taken 6 times.
✗ Branch 4 not taken.
|
6 | if(tmpDirName != ""){ |
344 |
2/3✓ Branch 1 taken 6 times.
✓ Branch 3 taken 6 times.
✗ Branch 4 not taken.
|
6 | if(!tmpDirName.isDirectoryExist()){ |
345 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
|
6 | if(mkdir(tmpDirName.c_str(), mode) != 0){ |
346 | ✗ | return false; | |
347 | } | ||
348 | } | ||
349 | } | ||
350 | 6 | isNotFirst = true; | |
351 | } | ||
352 | 2 | return true; | |
353 | // return mkdir(c_str(), 0755) == 0; | ||
354 | 2 | } | |
355 | |||
356 | ///Get path which is under the given pathPart ('some/dir/path' with 'dir' will return 'path') | ||
357 | /** @param pathPart : directory in the fileName we are looking for | ||
358 | * @return rest of the path under the pathPart | ||
359 | */ | ||
360 | 3 | PPath PPath::getUnderPath(const PString & pathPart) const{ | |
361 |
3/3✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
✓ Branch 4 taken 1 times.
|
3 | if(pathPart.size() == 0lu){return *this;} |
362 |
1/1✓ Branch 1 taken 2 times.
|
2 | std::vector<PString> listDir(split('/')); |
363 | 2 | std::vector<PString>::iterator it(listDir.begin()); | |
364 | 2 | bool isSearch(true); | |
365 |
6/6✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 times.
✓ Branch 4 taken 7 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 7 times.
✓ Branch 7 taken 2 times.
|
9 | while(isSearch && it != listDir.end()){ |
366 | 7 | isSearch = *it != pathPart; | |
367 | 7 | ++it; | |
368 | } | ||
369 |
1/1✓ Branch 1 taken 2 times.
|
2 | PPath out; |
370 |
1/1✓ Branch 1 taken 2 times.
|
2 | PString separator(""); |
371 |
2/2✓ Branch 2 taken 1 times.
✓ Branch 3 taken 2 times.
|
3 | while(it != listDir.end()){ |
372 |
2/2✓ Branch 2 taken 1 times.
✓ Branch 5 taken 1 times.
|
1 | out += separator + (*it); |
373 |
1/1✓ Branch 1 taken 1 times.
|
1 | separator = "/"; |
374 | 1 | ++it; | |
375 | } | ||
376 |
1/1✓ Branch 1 taken 2 times.
|
2 | return out; |
377 | 2 | } | |
378 | |||
379 | ///Get the file content in a PString | ||
380 | /** @return PString content of the file | ||
381 | */ | ||
382 | 12 | PString PPath::loadFileContent() const{ | |
383 |
4/4✓ Branch 1 taken 12 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 11 times.
✓ Branch 6 taken 1 times.
|
12 | if(!isFileExist()){return "";} |
384 |
1/1✓ Branch 2 taken 11 times.
|
11 | FILE * fp = fopen(c_str(), "r"); |
385 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
11 | if(fp == NULL){return "";} |
386 |
1/1✓ Branch 1 taken 11 times.
|
11 | PString bufferAllFile(phoenix_getFileContent(fp)); |
387 |
1/1✓ Branch 1 taken 11 times.
|
11 | fclose(fp); |
388 |
1/1✓ Branch 1 taken 11 times.
|
11 | return bufferAllFile; |
389 | 11 | } | |
390 | |||
391 | ///Save a PString in a file | ||
392 | /** @param content : file content | ||
393 | * @return true on success, false otherwise | ||
394 | */ | ||
395 | 5 | bool PPath::saveFileContent(const PString & content) const{ | |
396 | 5 | FILE * fp = fopen(c_str(), "w"); | |
397 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 4 times.
|
5 | if(fp == NULL){return false;} |
398 | 4 | bool result(true); | |
399 | 4 | fprintf(fp, "%s", content.c_str()); | |
400 | 4 | fclose(fp); | |
401 | 4 | return result; | |
402 | } | ||
403 | |||
404 | ///Get the size of the current file | ||
405 | /** @return size of the current file in bytes | ||
406 | */ | ||
407 | 2 | size_t PPath::getFileSize() const{ | |
408 | 2 | FILE* fs = fopen(c_str(), "r"); | |
409 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if(fs == NULL){return 0lu;} |
410 | 1 | fseek(fs, 0, SEEK_END); | |
411 | 1 | size_t fileSize(ftell(fs)); | |
412 | 1 | fseek(fs, 0, SEEK_SET); | |
413 | 1 | fclose(fs); | |
414 | 1 | return fileSize; | |
415 | } | ||
416 | |||
417 | ///Check if the given file starts with the given begning | ||
418 | /** @param expectedBegining : expected begening of the file | ||
419 | * @return true if the file starts with the expected begning, false otherwise | ||
420 | */ | ||
421 | 5 | bool PPath::checkFileBegning(const PString & expectedBegining) const{ | |
422 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 4 times.
|
5 | if(size() == 0lu){return false;} |
423 | 4 | FILE* fp = fopen(c_str(), "r"); | |
424 |
6/6✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 2 times.
|
4 | if(fp == NULL || expectedBegining.size() == 0lu){return false;} |
425 | 2 | int val(0); | |
426 | 2 | size_t i(0lu); | |
427 | 2 | bool isMatch(true); | |
428 |
7/8✓ Branch 0 taken 11 times.
✓ Branch 1 taken 1 times.
✓ Branch 3 taken 11 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 10 times.
✓ Branch 7 taken 1 times.
✓ Branch 8 taken 10 times.
✓ Branch 9 taken 2 times.
|
12 | while(isMatch && !feof(fp) && i < expectedBegining.size()){ |
429 | 10 | val = fgetc(fp); | |
430 | 10 | isMatch = (char)val == expectedBegining[i]; | |
431 | 10 | ++i; | |
432 | } | ||
433 | 2 | fclose(fp); | |
434 | 2 | return isMatch; | |
435 | } | ||
436 | |||
437 | ///Get the last modification time of the given file | ||
438 | /** @param fileName : name of the file we want the last modification time | ||
439 | * @return last modification time of the given file | ||
440 | */ | ||
441 | 3 | time_t PPath::getFileModificationTime() const{ | |
442 | struct stat attr; | ||
443 |
2/2✓ Branch 2 taken 1 times.
✓ Branch 3 taken 2 times.
|
3 | if(stat(c_str(), &attr) == 0){ |
444 | #ifdef __APPLE__ | ||
445 | return attr.st_mtimespec.tv_sec; | ||
446 | #else | ||
447 | 1 | return attr.st_mtim.tv_sec; | |
448 | #endif | ||
449 | }else{ | ||
450 | 2 | return -1l; | |
451 | } | ||
452 | } | ||
453 | |||
454 | ///Get the list of files in a directory | ||
455 | /** @return list of files in the current directory | ||
456 | */ | ||
457 | 2 | std::vector<PPath> PPath::getAllFileInDir() const{ | |
458 | 2 | std::vector<PPath> listFile; | |
459 | // Open the current directory | ||
460 |
1/1✓ Branch 2 taken 2 times.
|
2 | DIR * dp = opendir(c_str()); |
461 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if(NULL == dp){ |
462 | ✗ | return listFile; | |
463 | } | ||
464 |
1/1✓ Branch 1 taken 2 times.
|
2 | dirent * dptr = readdir(dp); |
465 |
2/2✓ Branch 0 taken 17 times.
✓ Branch 1 taken 2 times.
|
19 | while(NULL != dptr){ |
466 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 10 times.
|
17 | if(dptr->d_type == DT_REG){ |
467 |
2/2✓ Branch 1 taken 7 times.
✓ Branch 4 taken 7 times.
|
7 | PPath fileName(dptr->d_name); |
468 |
1/1✓ Branch 1 taken 7 times.
|
7 | listFile.push_back(fileName); |
469 | 7 | } | |
470 |
1/1✓ Branch 1 taken 17 times.
|
17 | dptr = readdir(dp); |
471 | } | ||
472 |
1/1✓ Branch 1 taken 2 times.
|
2 | closedir(dp); |
473 | 2 | return listFile; | |
474 | } | ||
475 | |||
476 | ///Get the list of files in a directory | ||
477 | /** @return list of files in the current directory | ||
478 | */ | ||
479 | 1 | std::vector<PPath> PPath::getAllDirectoryInDir() const{ | |
480 | 1 | std::vector<PPath> listFile; | |
481 | // Open the current directory | ||
482 |
1/1✓ Branch 2 taken 1 times.
|
1 | DIR * dp = opendir(c_str()); |
483 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if(NULL == dp){ |
484 | ✗ | return listFile; | |
485 | } | ||
486 |
1/1✓ Branch 1 taken 1 times.
|
1 | dirent * dptr = readdir(dp); |
487 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1 times.
|
7 | while(NULL != dptr){ |
488 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
|
6 | if(dptr->d_type == DT_DIR){ |
489 |
2/2✓ Branch 1 taken 4 times.
✓ Branch 4 taken 4 times.
|
4 | PPath dirName(dptr->d_name); |
490 |
8/8✓ Branch 1 taken 4 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 1 times.
✓ Branch 6 taken 3 times.
✓ Branch 8 taken 2 times.
✓ Branch 9 taken 1 times.
✓ Branch 10 taken 2 times.
✓ Branch 11 taken 2 times.
|
4 | if(dirName != ".." && dirName != "."){ |
491 |
1/1✓ Branch 1 taken 2 times.
|
2 | listFile.push_back(dirName); |
492 | } | ||
493 | 4 | } | |
494 |
1/1✓ Branch 1 taken 6 times.
|
6 | dptr = readdir(dp); |
495 | } | ||
496 |
1/1✓ Branch 1 taken 1 times.
|
1 | closedir(dp); |
497 | 1 | return listFile; | |
498 | } | ||
499 | |||
500 | ///Get the list of all elements in a directory | ||
501 | /** @return list of files in the current directory | ||
502 | */ | ||
503 | 2 | std::vector<PPath> PPath::getAllElementInDir() const{ | |
504 | 2 | std::vector<PPath> listFile; | |
505 | // Open the current directory | ||
506 |
1/1✓ Branch 2 taken 2 times.
|
2 | DIR * dp = opendir(c_str()); |
507 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if(NULL == dp){ |
508 | ✗ | return listFile; | |
509 | } | ||
510 |
1/1✓ Branch 1 taken 2 times.
|
2 | dirent * dptr = readdir(dp); |
511 |
2/2✓ Branch 0 taken 17 times.
✓ Branch 1 taken 2 times.
|
19 | while(NULL != dptr){ |
512 |
2/2✓ Branch 1 taken 17 times.
✓ Branch 4 taken 17 times.
|
17 | PPath fileName(dptr->d_name); |
513 |
8/8✓ Branch 1 taken 17 times.
✓ Branch 3 taken 15 times.
✓ Branch 4 taken 2 times.
✓ Branch 6 taken 15 times.
✓ Branch 8 taken 13 times.
✓ Branch 9 taken 2 times.
✓ Branch 10 taken 13 times.
✓ Branch 11 taken 4 times.
|
17 | if(fileName != ".." && fileName != "."){ |
514 |
1/1✓ Branch 1 taken 13 times.
|
13 | listFile.push_back(fileName); |
515 | } | ||
516 |
1/1✓ Branch 1 taken 17 times.
|
17 | dptr = readdir(dp); |
517 | 17 | } | |
518 |
1/1✓ Branch 1 taken 2 times.
|
2 | closedir(dp); |
519 | 2 | return listFile; | |
520 | } | ||
521 | |||
522 | ///Change the mode of a file or directory | ||
523 | /** @param __mode : mode to be applied to the given file (Default value makes files executable S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) | ||
524 | * @return true on success, false otherwise | ||
525 | */ | ||
526 | 3 | bool PPath::changeMode(mode_t __mode) const{ | |
527 | 3 | return chmod(c_str(), __mode) >= 0; | |
528 | } | ||
529 | |||
530 | ///Remove extra dots from the path | ||
531 | /** @return path wthout dots | ||
532 | */ | ||
533 | 14 | PPath PPath::simplify() const{ | |
534 |
3/3✓ Branch 1 taken 1 times.
✓ Branch 2 taken 13 times.
✓ Branch 4 taken 1 times.
|
14 | if(size() == 0lu){return *this;} |
535 |
3/3✓ Branch 1 taken 1 times.
✓ Branch 2 taken 12 times.
✓ Branch 4 taken 1 times.
|
13 | if(front() == '.'){return *this;} |
536 |
1/1✓ Branch 1 taken 12 times.
|
12 | std::vector<PString> listDir(split('/')); |
537 | 12 | std::vector<PString>::reverse_iterator rit = listDir.rbegin(); | |
538 |
3/3✓ Branch 2 taken 60 times.
✓ Branch 4 taken 48 times.
✓ Branch 5 taken 12 times.
|
60 | while(rit != listDir.rend()){ |
539 |
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 == ""){ |
540 | // rit = listDir.erase(rit); //No erase in reverse_iterator, so anoying | ||
541 |
1/1✓ Branch 2 taken 12 times.
|
12 | *rit = ""; |
542 |
2/2✓ Branch 2 taken 10 times.
✓ Branch 3 taken 26 times.
|
36 | }else if(*rit == ".."){ |
543 | // rit = listDir.erase(rit); //No erase in reverse_iterator, so anoying | ||
544 |
1/1✓ Branch 2 taken 10 times.
|
10 | *rit = ""; |
545 | 10 | ++rit; | |
546 |
2/3✓ Branch 2 taken 10 times.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
|
10 | if(rit != listDir.rend()){ |
547 | // rit = listDir.erase(rit); //No erase in reverse_iterator, so anoying | ||
548 |
1/1✓ Branch 2 taken 10 times.
|
10 | *rit = ""; |
549 | } | ||
550 | } | ||
551 | // else{ | ||
552 | // ++rit; //No erase in reverse_iterator, so anoying | ||
553 | // } | ||
554 | 48 | ++rit; | |
555 | } | ||
556 |
1/1✓ Branch 1 taken 12 times.
|
12 | PPath out; |
557 |
1/1✓ Branch 1 taken 12 times.
|
12 | PString separator(""); |
558 |
2/2✓ Branch 1 taken 6 times.
✓ Branch 2 taken 6 times.
|
12 | if(front() == '/'){ |
559 |
3/3✓ Branch 1 taken 6 times.
✓ Branch 4 taken 6 times.
✓ Branch 7 taken 6 times.
|
6 | out = PString("/"); |
560 | } | ||
561 |
2/2✓ Branch 4 taken 58 times.
✓ Branch 5 taken 12 times.
|
70 | for(std::vector<PString>::iterator it(listDir.begin()); it != listDir.end(); ++it){ |
562 |
2/2✓ Branch 2 taken 32 times.
✓ Branch 3 taken 26 times.
|
58 | if(*it == ""){continue;} |
563 |
2/2✓ Branch 2 taken 26 times.
✓ Branch 5 taken 26 times.
|
26 | out += separator + (*it); |
564 |
1/1✓ Branch 1 taken 26 times.
|
26 | separator = "/"; |
565 | } | ||
566 |
1/1✓ Branch 1 taken 12 times.
|
12 | return out; |
567 | 12 | } | |
568 | |||
569 | ///Make an absolute path of the given path | ||
570 | /** @return corresponding absolute path | ||
571 | */ | ||
572 | 3 | PPath PPath::makeAbsolute() const{ | |
573 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
|
3 | if(size() == 0lu){ |
574 |
2/2✓ Branch 2 taken 1 times.
✓ Branch 5 taken 1 times.
|
2 | return PPath::getCurrentDirectory().add("/"); |
575 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
|
2 | }else if(isAbsolutePath()){ |
576 | 1 | return *this; | |
577 | }else{ | ||
578 |
3/3✓ Branch 2 taken 1 times.
✓ Branch 5 taken 1 times.
✓ Branch 8 taken 1 times.
|
2 | return PPath::getCurrentDirectory().add("/").add(*this); |
579 | } | ||
580 | } | ||
581 | |||
582 | ///Get the program location | ||
583 | /** @return location of the current program | ||
584 | */ | ||
585 | 6 | PPath PPath::getProgramLocation(){ | |
586 | char buffer[4096]; | ||
587 | 6 | ssize_t nbChar = readlink("/proc/self/exe", buffer, 2048); | |
588 | // readlink("/proc/self/exe", buf, bufsize) (Linux) | ||
589 | // readlink("/proc/curproc/file", buf, bufsize) (FreeBSD) | ||
590 | // readlink("/proc/self/path/a.out", buf, bufsize) (Solaris) | ||
591 | |||
592 |
1/2✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 | if(nbChar > 0l){ |
593 |
1/1✓ Branch 1 taken 6 times.
|
6 | PString outputBuf(""); |
594 |
2/2✓ Branch 0 taken 654 times.
✓ Branch 1 taken 6 times.
|
660 | for(ssize_t i(0l); i < nbChar; ++i){ |
595 |
1/1✓ Branch 1 taken 654 times.
|
654 | outputBuf += buffer[i]; |
596 | } | ||
597 |
1/1✓ Branch 1 taken 6 times.
|
6 | return outputBuf; |
598 | 6 | }else{ | |
599 | ✗ | return PString(""); | |
600 | } | ||
601 | } | ||
602 | |||
603 | ///Get the program directory | ||
604 | /** @return directory of the program | ||
605 | */ | ||
606 | 4 | PPath PPath::getProgramDirectory(){ | |
607 |
1/1✓ Branch 1 taken 4 times.
|
4 | PPath progLoc(getProgramLocation()); |
608 |
2/3✓ Branch 1 taken 4 times.
✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
|
4 | if(progLoc != ""){ |
609 |
1/1✓ Branch 1 taken 4 times.
|
4 | return progLoc.getParentDirectory(); |
610 | }else{ | ||
611 | #ifdef CMAKE_INSTALL_PREFIX | ||
612 | return PString(CMAKE_INSTALL_PREFIX "/bin/"); | ||
613 | #else | ||
614 | ✗ | return PString("/usr/bin/"); | |
615 | #endif | ||
616 | } | ||
617 | 4 | } | |
618 | |||
619 | ///Get the program prefix (installation directory without /bin) | ||
620 | /** @return prefix of the program (installation directory without /bin) | ||
621 | */ | ||
622 | 2 | PPath PPath::getProgramPrefix(){ | |
623 |
1/1✓ Branch 2 taken 2 times.
|
4 | return getProgramDirectory().getParentDirectory(); |
624 | } | ||
625 | |||
626 | ///Gets the $HOME directory | ||
627 | /** @return $HOME directory | ||
628 | */ | ||
629 | 2 | PPath PPath::getHomeDir(){ | |
630 |
2/2✓ Branch 2 taken 2 times.
✓ Branch 5 taken 2 times.
|
4 | return phoenix_getenv("HOME"); |
631 | } | ||
632 | |||
633 | ///Returns the current directory | ||
634 | /** @return current directory | ||
635 | */ | ||
636 | 4 | PPath PPath::getCurrentDirectory(){ | |
637 | #ifndef __APPLE__ | ||
638 | 4 | char* ptr = get_current_dir_name(); | |
639 |
1/1✓ Branch 1 taken 4 times.
|
4 | PString str(phoenix_charToString(ptr)); |
640 | 4 | free(ptr); | |
641 |
1/1✓ Branch 1 taken 4 times.
|
8 | return str; |
642 | #else | ||
643 | return phoenix_getenv("PWD"); | ||
644 | #endif | ||
645 | 4 | } | |
646 | |||
647 | ///Get the name of the current node on which the program is running | ||
648 | /** @return name of the current node on which the program is running | ||
649 | */ | ||
650 | 2 | PString PPath::getCurrentNodeName(){ | |
651 |
3/3✓ Branch 2 taken 2 times.
✓ Branch 5 taken 2 times.
✓ Branch 8 taken 2 times.
|
2 | return phoenix_popen("uname -n").eraseChar(" \n\t"); |
652 | } | ||
653 | |||
654 | ///Copy function of PPath | ||
655 | /** @param other : class to copy | ||
656 | */ | ||
657 | 124 | void PPath::copyPPath(const PPath & other){ | |
658 | |||
659 | 124 | } | |
660 | |||
661 | ///Initialisation function of the class PPath | ||
662 | 302 | void PPath::initialisationPPath(){ | |
663 | |||
664 | 302 | } | |
665 | |||
666 | |||
667 | |||
668 | |||
669 | |||
670 |