GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: tmp_project/StringUtils/src/string_filename.cpp Lines: 206 208 99.0 %
Date: 2024-12-09 15:30:52 Branches: 226 244 92.6 %

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
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
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
15
bool isDirectoryExist(const std::string & dirName){
58
15
	if(dirName == ""){return false;}
59
	struct stat path_stat;
60
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
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
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
8
	if(path == ""){return getCurrentDirectory() + "/";}
95
4
	else if(path[0] == '/'){return path;}
96
	else{
97
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
4
	for(std::vector<std::string>::const_iterator it(vecPath.begin()); it != vecPath.end(); ++it){
108
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
14
	if(path == ""){return path;}
119
13
	if(path.front() == '.'){return path;}
120
24
	std::list<std::string> listDir(cutStringList(path, '/'));
121
12
	std::list<std::string>::reverse_iterator rit = listDir.rbegin();
122
60
	while(rit != listDir.rend()){
123

48
		if(*rit == "." || *rit == ""){
124
// 			rit = listDir.erase(rit);	//No erase in reverse_iterator, so anoying
125
12
			*rit = "";
126
36
		}else if(*rit == ".."){
127
// 			rit = listDir.erase(rit);	//No erase in reverse_iterator, so anoying
128
10
			*rit = "";
129
10
			++rit;
130
10
			if(rit != listDir.rend()){
131
// 				rit = listDir.erase(rit);	//No erase in reverse_iterator, so anoying
132
10
				*rit = "";
133
			}
134
		}
135
// 		else{
136
// 			++rit;	//No erase in reverse_iterator, so anoying
137
// 		}
138
48
		++rit;
139
	}
140
36
	std::string out(""), separator("");
141
12
	if(path[0] == '/'){
142
6
		out = "/";
143
	}
144
70
	for(std::list<std::string>::iterator it(listDir.begin()); it != listDir.end(); ++it){
145
58
		if(*it == ""){continue;}
146
26
		out += separator + (*it);
147
26
		separator = "/";
148
	}
149
12
	return out;
150
}
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
6
	std::string buffer("");
158
6
	bool find(false);
159
6
	std::string::const_reverse_iterator rit = fileName.rbegin();
160
604
	while(rit != fileName.rend()){
161
598
		if(find){
162
517
			buffer = *rit + buffer;
163
		}else{
164
81
			find = (*rit == '/');
165
		}
166
598
		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
5
	if(pathPart == ""){return fileName;}
178
8
	std::list<std::string> listDir(cutStringList(fileName, '/'));
179
4
	std::list<std::string>::iterator it(listDir.begin());
180
4
	bool isSearch(true);
181

15
	while(isSearch && it != listDir.end()){
182
11
		isSearch = *it != pathPart;
183
11
		++it;
184
	}
185
16
	std::string out(""), separator("");
186
9
	while(it != listDir.end()){
187
5
		out += separator + (*it);
188
5
		separator = "/";
189
5
		++it;
190
	}
191
4
	return out;
192
}
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
3
	for(std::vector<std::string>::const_iterator it(vecFileName.begin()); it != vecFileName.end(); ++it){
202
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

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


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
3
	std::string buffer("");
233
3
	std::string::const_reverse_iterator rit = fileName.rbegin();
234
31
	while(rit != fileName.rend()){
235
31
		if(*rit == '/') break;
236
28
		buffer = *rit + buffer;
237
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
2
	std::string buffer("");
248
2
	std::string::const_reverse_iterator rit = path.rbegin();
249
11
	while(rit != path.rend()){
250
11
		if(*rit == '/'){
251
3
			if(buffer != ""){
252
2
				break;
253
			}else{
254
1
				rit++;
255
1
				continue;
256
			}
257
		}
258
8
		buffer = *rit + buffer;
259
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
3
	FILE * fp = fopen(filename.c_str(), "r");
270
3
	if(fp == NULL){
271

2
		std::cerr << "getFileContent : cannot open file '" << filename << "'" << std::endl;
272
2
		return "";
273
	}
274
2
	std::string tmp(getFileContent(fp));
275
1
	fclose(fp);
276
1
	return tmp;
277
}
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
34
	if(fp == NULL) return "";
285
99
	std::string bufferAllFile("");
286
	int buffer;
287
11100
	while(!feof(fp)){
288
11100
		buffer = fgetc(fp);
289
11100
		if(buffer != EOF) bufferAllFile += (char)buffer;
290
33
		else break;
291
	}
292
33
	return bufferAllFile;
293
}
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
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
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
5
std::string getExtention(const std::string & fileName){
327

5
	if(!findInString(fileName, '.')) return "";
328
8
	std::string extention("");
329
4
	bool run(true);
330
4
	std::string::const_reverse_iterator rit(fileName.rbegin());
331

30
	while(run && rit != fileName.rend()){
332

26
		if(*rit == '.' || *rit == '/') run = false;
333
22
		else extention = *rit + extention;
334
26
		rit++;
335
	}
336
4
	return extention;
337
}
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
7
	size_t nbPoint(countNbChar(fileName, '.'));
345
7
	if(nbPoint == 0lu) return "";
346
20
	std::string extention(""), tmpExtention(""), middleDot("");
347
5
	bool run(true);
348
5
	std::string::const_reverse_iterator rit(fileName.rbegin());
349

74
	while(run && rit != fileName.rend()){
350
69
		if(*rit == '/'){run = false;}
351
66
		else if(*rit == '.'){
352
9
			extention = tmpExtention + middleDot + extention;
353
9
			tmpExtention = "";
354
9
			middleDot = ".";
355
		}else{
356
57
			tmpExtention = *rit + tmpExtention;
357
		}
358
359
69
		rit++;
360
	}
361
5
	return extention;
362
}
363
364
///Erase extention of the given file
365
/**	@param fileName : input file name
366
 * 	@return filen name without extention
367
*/
368
6
std::string eraseExtension(const std::string & fileName){
369
6
	int nbPoint(countNbChar(fileName, '.'));
370
6
	if(nbPoint == 0) return fileName;
371
10
	std::string buffer("");
372
5
	int findNbPoint(0);
373
5
	std::string::const_iterator it = fileName.begin();
374

73
	while(it != fileName.end() && findNbPoint < nbPoint){
375
68
		if(*it == '.'){
376
8
			findNbPoint++;
377
8
			if(findNbPoint < nbPoint){
378
3
				buffer += *it;
379
			}
380
		}else{
381
60
			buffer += *it;
382
		}
383
68
		it++;
384
	}
385
5
	return buffer;
386
}
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
6
	std::string longestExtention(getLongestExtention(fileName));
394
3
	if(longestExtention != ""){
395
2
		return copyStr(fileName, 0lu, fileName.size() - (longestExtention.size() + 1lu));
396
	}else{
397
1
		return fileName;
398
	}
399
}
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
6
	std::list<std::string> listDir(cutStringList(dirName, '/'));
407
6
	std::string tmpDirName("");
408
3
	bool isNotFirst(false);
409
9
	for(std::list<std::string>::iterator it(listDir.begin()); it != listDir.end(); ++it){
410
6
		if(isNotFirst){
411
3
			tmpDirName += "/";
412
		}
413
6
		tmpDirName += *it;
414
6
		if(tmpDirName != ""){
415
5
			if(!isDirectoryExist(tmpDirName)){
416
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
}
426
427