PhoenixMock  1.8.7
Tools to split/merge/print mock used in Phoenix
path_completion.cpp
Go to the documentation of this file.
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 
15 
18 std::string completePathDir(const std::string & pathDir){
19 // return pathDir;
20  if(pathDir == ""){return ".";}
21  else if(pathDir[0] == '/'){return pathDir;}
22  else if(isSameBegining(pathDir, "./")){return pathDir;}
23  else{return "./" + pathDir;}
24 }
25 
27 
30 std::string prefixPathDir(const std::string & pathDir){
31  if(pathDir == ""){return "";}
32  else if(pathDir[pathDir.size() - 1lu] == '/'){return pathDir;}
33  else if(pathDir[pathDir.size() - 1lu] != '/'){return pathDir + "/";}
34  else{return pathDir;}
35 }
36 
38 
41 std::string path_completion_all(const std::string & basePath){
42 // std::cerr << "path_completion_all : basePath = '"<<basePath<<"'" << std::endl;
43  if(basePath == ""){return "./";}
44  if(isFileExist(basePath)){return "";}
45 
46  std::string listMatchingPath("");
47  if(isDirectoryExist(basePath)){
48  std::string completedPathDir(completePathDir(basePath));
49  std::string prefixPath(prefixPathDir(basePath));
50  DIR * dp = opendir(completedPathDir.c_str());
51  dirent * dptr = readdir(dp);
52  while(NULL != dptr){
53  std::string pathName(dptr->d_name);
54  if(pathName != "." && pathName != ".."){
55  listMatchingPath += prefixPath + pathName + "\n";
56  }
57  dptr = readdir(dp);
58  }
59  closedir(dp);
60  }else{
61  std::string baseDir(getDirectory(basePath)), startNewPath(getFileName(basePath));
62  std::string completedPathDir(completePathDir(baseDir));
63  std::string prefixPath(prefixPathDir(baseDir));
64  DIR * dp = opendir(completedPathDir.c_str());
65 
66  dirent * dptr = readdir(dp);
67  while(NULL != dptr){
68  std::string pathName(dptr->d_name);
69  if(pathName != "." && pathName != ".."){
70  if(isSameBegining(pathName, startNewPath)){
71  listMatchingPath += prefixPath + pathName + "\n";
72  }
73  }
74  dptr = readdir(dp);
75  }
76 
77  closedir(dp);
78  }
79 // std::cerr << "path_completion_all : listMatchingPath = '"<<listMatchingPath<<"'" << std::endl;
80  return listMatchingPath;
81 }
82 
84 
87 std::string path_completion_dirOnly(const std::string & basePath){
88  if(basePath == ""){return "./";}
89  std::string listMatchingPath("");
90  if(isDirectoryExist(basePath)){
91  DIR * dp = opendir(basePath.c_str());
92 
93  dirent * dptr = readdir(dp);
94  while(NULL != dptr){
95  if(dptr->d_type == DT_DIR){ //We search for directory only
96  std::string pathName(dptr->d_name);
97  if(pathName != "." && pathName != ".."){
98  listMatchingPath += basePath + "/" + pathName + "\n";
99  }
100  }
101  dptr = readdir(dp);
102  }
103  }else{
104  std::string baseDir(getDirectory(basePath)), startNewPath(getFileName(basePath));
105  DIR * dp = opendir(baseDir.c_str());
106 
107  dirent * dptr = readdir(dp);
108  while(NULL != dptr){
109  if(dptr->d_type == DT_DIR){ //We search for directory only
110  std::string pathName(dptr->d_name);
111  if(pathName != "." && pathName != ".."){
112  if(isSameBegining(pathName, startNewPath)){
113  listMatchingPath += baseDir + "/" + pathName + "\n";
114  }
115  }
116  }
117  dptr = readdir(dp);
118  }
119 
120  closedir(dp);
121  }
122  return listMatchingPath;
123 }
124 
125 
string_utils.h
path_completion_all
std::string path_completion_all(const std::string &basePath)
Return all path/files which match the basePath.
Definition: path_completion.cpp:41
isDirectoryExist
bool isDirectoryExist(const std::string &dirName)
Says if the given direcotry exists.
Definition: string_filename.cpp:57
completePathDir
std::string completePathDir(const std::string &pathDir)
Complete the path directory.
Definition: path_completion.cpp:18
string_filename.h
isSameBegining
bool isSameBegining(const std::string &str, const std::string &beginig)
Check if two string start the same way.
Definition: string_function.cpp:531
path_completion_dirOnly
std::string path_completion_dirOnly(const std::string &basePath)
Return all directories only which match the basePath.
Definition: path_completion.cpp:87
getFileName
std::string getFileName(const std::string &fileName)
fonction qui renvoie le nom du fichier du nom complet de fichier passé en paramètre
Definition: string_filename.cpp:231
prefixPathDir
std::string prefixPathDir(const std::string &pathDir)
Complete the path directory with a prefix.
Definition: path_completion.cpp:30
isFileExist
bool isFileExist(const std::string &fileName)
Say if a file exsits or not.
Definition: string_filename.cpp:27
getDirectory
std::string getDirectory(const std::string &fileName)
fonction qui renvoie le dossier parent du fichier
Definition: string_filename.cpp:156
path_completion.h