GCC Code Coverage Report


Directory: ./
File: src/path_completion.cpp
Date: 2024-07-27 10:53:27
Exec Total Coverage
Lines: 68 71 95.8%
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
1/4
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
3 if(pathDir == ""){return ".";}
21
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
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
1/4
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
3 if(pathDir == ""){return "";}
32
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
3 else if(pathDir[pathDir.size() - 1lu] == '/'){return pathDir;}
33
1/2
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
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
3/3
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 4 times.
✓ Branch 5 taken 1 times.
5 if(basePath == ""){return "./";}
44
4/4
✓ Branch 1 taken 4 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 1 times.
4 if(isFileExist(basePath)){return "";}
45
46
1/1
✓ Branch 2 taken 3 times.
3 std::string listMatchingPath("");
47
3/3
✓ Branch 1 taken 3 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 1 times.
3 if(isDirectoryExist(basePath)){
48
1/1
✓ Branch 1 taken 2 times.
2 std::string completedPathDir(completePathDir(basePath));
49
1/1
✓ Branch 1 taken 2 times.
2 std::string prefixPath(prefixPathDir(basePath));
50
1/1
✓ Branch 2 taken 2 times.
2 DIR * dp = opendir(completedPathDir.c_str());
51
1/1
✓ Branch 1 taken 2 times.
2 dirent * dptr = readdir(dp);
52
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 2 times.
9 while(NULL != dptr){
53
1/1
✓ Branch 2 taken 7 times.
7 std::string pathName(dptr->d_name);
54
6/6
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 2 times.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 3 times.
✓ Branch 7 taken 4 times.
7 if(pathName != "." && pathName != ".."){
55
3/3
✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
3 listMatchingPath += prefixPath + pathName + "\n";
56 }
57
1/1
✓ Branch 1 taken 7 times.
7 dptr = readdir(dp);
58 7 }
59
1/1
✓ Branch 1 taken 2 times.
2 closedir(dp);
60 2 }else{
61
2/2
✓ Branch 1 taken 1 times.
✓ Branch 4 taken 1 times.
1 std::string baseDir(getDirectory(basePath)), startNewPath(getFileName(basePath));
62
1/1
✓ Branch 1 taken 1 times.
1 std::string completedPathDir(completePathDir(baseDir));
63
1/1
✓ Branch 1 taken 1 times.
1 std::string prefixPath(prefixPathDir(baseDir));
64
1/1
✓ Branch 2 taken 1 times.
1 DIR * dp = opendir(completedPathDir.c_str());
65
66
1/1
✓ Branch 1 taken 1 times.
1 dirent * dptr = readdir(dp);
67
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 while(NULL != dptr){
68
1/1
✓ Branch 2 taken 3 times.
3 std::string pathName(dptr->d_name);
69
6/6
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 2 times.
3 if(pathName != "." && pathName != ".."){
70
2/3
✓ Branch 1 taken 1 times.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 if(isSameBegining(pathName, startNewPath)){
71
3/3
✓ Branch 1 taken 1 times.
✓ Branch 4 taken 1 times.
✓ Branch 7 taken 1 times.
1 listMatchingPath += prefixPath + pathName + "\n";
72 }
73 }
74
1/1
✓ Branch 1 taken 3 times.
3 dptr = readdir(dp);
75 3 }
76
77
1/1
✓ Branch 1 taken 1 times.
1 closedir(dp);
78 1 }
79 // std::cerr << "path_completion_all : listMatchingPath = '"<<listMatchingPath<<"'" << std::endl;
80 3 return listMatchingPath;
81 3 }
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
1/4
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
2 if(basePath == ""){return "./";}
89
1/1
✓ Branch 2 taken 2 times.
2 std::string listMatchingPath("");
90
3/3
✓ Branch 1 taken 2 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 1 times.
2 if(isDirectoryExist(basePath)){
91
1/1
✓ Branch 2 taken 1 times.
1 DIR * dp = opendir(basePath.c_str());
92
93
1/1
✓ Branch 1 taken 1 times.
1 dirent * dptr = readdir(dp);
94
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 while(NULL != dptr){
95
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 if(dptr->d_type == DT_DIR){ //We search for directory only
96
1/1
✓ Branch 2 taken 3 times.
3 std::string pathName(dptr->d_name);
97
6/6
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 2 times.
3 if(pathName != "." && pathName != ".."){
98
4/4
✓ Branch 1 taken 1 times.
✓ Branch 4 taken 1 times.
✓ Branch 7 taken 1 times.
✓ Branch 10 taken 1 times.
1 listMatchingPath += basePath + "/" + pathName + "\n";
99 }
100 3 }
101
1/1
✓ Branch 1 taken 4 times.
4 dptr = readdir(dp);
102 }
103 }else{
104
2/2
✓ Branch 1 taken 1 times.
✓ Branch 4 taken 1 times.
1 std::string baseDir(getDirectory(basePath)), startNewPath(getFileName(basePath));
105
1/1
✓ Branch 2 taken 1 times.
1 DIR * dp = opendir(baseDir.c_str());
106
107
1/1
✓ Branch 1 taken 1 times.
1 dirent * dptr = readdir(dp);
108
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 while(NULL != dptr){
109
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 if(dptr->d_type == DT_DIR){ //We search for directory only
110
1/1
✓ Branch 2 taken 3 times.
3 std::string pathName(dptr->d_name);
111
6/6
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 2 times.
3 if(pathName != "." && pathName != ".."){
112
2/3
✓ Branch 1 taken 1 times.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 if(isSameBegining(pathName, startNewPath)){
113
4/4
✓ Branch 1 taken 1 times.
✓ Branch 4 taken 1 times.
✓ Branch 7 taken 1 times.
✓ Branch 10 taken 1 times.
1 listMatchingPath += baseDir + "/" + pathName + "\n";
114 }
115 }
116 3 }
117
1/1
✓ Branch 1 taken 4 times.
4 dptr = readdir(dp);
118 }
119
120
1/1
✓ Branch 1 taken 1 times.
1 closedir(dp);
121 1 }
122 2 return listMatchingPath;
123 2 }
124
125
126