GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: tmp_project/OptionParser/src/path_completion.cpp Lines: 59 62 95.2 %
Date: 2024-12-09 15:30:52 Branches: 102 122 83.6 %

Line Branch Exec Source
1
/***************************************
2
	Auteur : Pierre Aubert
3
	Mail : pierre.aubert@lapp.in2p3.fr
4
	Licence : CeCILL-C
5
****************************************/
6
7
#include <dirent.h>
8
9
#include "string_utils.h"
10
#include "string_filename.h"
11
12
#include "path_completion.h"
13
14
///Complete the path directory
15
/**	@param pathDir : path of the directory
16
 * 	@return completed directory
17
*/
18
3
std::string completePathDir(const std::string & pathDir){
19
// 	return pathDir;
20

3
	if(pathDir == ""){return ".";}
21
3
	else if(pathDir[0] == '/'){return pathDir;}
22
	else if(isSameBegining(pathDir, "./")){return pathDir;}
23
	else{return "./" + pathDir;}
24
}
25
26
///Complete the path directory with a prefix
27
/**	@param pathDir : path of the directory
28
 * 	@return completed directory
29
*/
30
3
std::string prefixPathDir(const std::string & pathDir){
31

3
	if(pathDir == ""){return "";}
32
3
	else if(pathDir[pathDir.size() - 1lu] == '/'){return pathDir;}
33
3
	else if(pathDir[pathDir.size() - 1lu] != '/'){return pathDir + "/";}
34
	else{return pathDir;}
35
}
36
37
///Return all path/files which match the basePath
38
/**	@param basePath : incomming path
39
 * 	@return all path/files which match the basePath
40
*/
41
5
std::string path_completion_all(const std::string & basePath){
42
// 	std::cerr << "path_completion_all : basePath = '"<<basePath<<"'" << std::endl;
43
5
	if(basePath == ""){return "./";}
44

4
	if(isFileExist(basePath)){return "";}
45
46
6
	std::string listMatchingPath("");
47
3
	if(isDirectoryExist(basePath)){
48
4
		std::string completedPathDir(completePathDir(basePath));
49
4
		std::string prefixPath(prefixPathDir(basePath));
50
2
		DIR * dp = opendir(completedPathDir.c_str());
51
2
		dirent * dptr = readdir(dp);
52
9
		while(NULL != dptr){
53
7
			std::string pathName(dptr->d_name);
54

7
			if(pathName != "." && pathName != ".."){
55
3
				listMatchingPath += prefixPath + pathName + "\n";
56
			}
57
7
			dptr = readdir(dp);
58
		}
59
2
		closedir(dp);
60
	}else{
61
2
		std::string baseDir(getDirectory(basePath)), startNewPath(getFileName(basePath));
62
2
		std::string completedPathDir(completePathDir(baseDir));
63
2
		std::string prefixPath(prefixPathDir(baseDir));
64
1
		DIR * dp = opendir(completedPathDir.c_str());
65
66
1
		dirent * dptr = readdir(dp);
67
4
		while(NULL != dptr){
68
3
			std::string pathName(dptr->d_name);
69

3
			if(pathName != "." && pathName != ".."){
70
1
				if(isSameBegining(pathName, startNewPath)){
71
1
					listMatchingPath += prefixPath + pathName + "\n";
72
				}
73
			}
74
3
			dptr = readdir(dp);
75
		}
76
77
1
		closedir(dp);
78
	}
79
// 	std::cerr << "path_completion_all : listMatchingPath = '"<<listMatchingPath<<"'" << std::endl;
80
3
	return listMatchingPath;
81
}
82
83
///Return all directories only which match the basePath
84
/**	@param basePath : incomming path
85
 * 	@return all directories which match the basePath
86
*/
87
2
std::string path_completion_dirOnly(const std::string & basePath){
88

2
	if(basePath == ""){return "./";}
89
4
	std::string listMatchingPath("");
90
2
	if(isDirectoryExist(basePath)){
91
1
		DIR * dp = opendir(basePath.c_str());
92
93
1
		dirent * dptr = readdir(dp);
94
5
		while(NULL != dptr){
95
4
			if(dptr->d_type == DT_DIR){	//We search for directory only
96
6
				std::string pathName(dptr->d_name);
97

3
				if(pathName != "." && pathName != ".."){
98

1
					listMatchingPath += basePath + "/" + pathName + "\n";
99
				}
100
			}
101
4
			dptr = readdir(dp);
102
		}
103
	}else{
104
2
		std::string baseDir(getDirectory(basePath)), startNewPath(getFileName(basePath));
105
1
		DIR * dp = opendir(baseDir.c_str());
106
107
1
		dirent * dptr = readdir(dp);
108
5
		while(NULL != dptr){
109
4
			if(dptr->d_type == DT_DIR){	//We search for directory only
110
6
				std::string pathName(dptr->d_name);
111

3
				if(pathName != "." && pathName != ".."){
112
1
					if(isSameBegining(pathName, startNewPath)){
113

1
						listMatchingPath += baseDir + "/" + pathName + "\n";
114
					}
115
				}
116
			}
117
4
			dptr = readdir(dp);
118
		}
119
120
1
		closedir(dp);
121
	}
122
2
	return listMatchingPath;
123
}
124
125