PhoenixInkscape  2.0.0
Generate multiple png files with svg inkscape files
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 "path_completion.h"
10 
12 
15 PPath completePathDir(const PPath & pathDir){
16  if(pathDir == ""){return PString(".");}
17  else if(pathDir[0] == '/'){return pathDir;}
18  else if(pathDir.isSameBegining("./")){return pathDir;}
19  else{return PPath(PString("./") + pathDir);}
20 }
21 
23 
26 PPath prefixPathDir(const PPath & pathDir){
27  if(pathDir == ""){return pathDir;}
28  else if(pathDir[pathDir.size() - 1lu] == '/'){return pathDir;}
29  else if(pathDir[pathDir.size() - 1lu] != '/'){return PPath(pathDir + PString("/"));}
30  else{return pathDir;}
31 }
32 
34 
37 PPath path_completion_all(const PPath & basePath){
38 // std::cerr << "path_completion_all : basePath = '"<<basePath<<"'" << std::endl;
39  if(basePath == ""){return PString("./");}
40  if(basePath.isFileExist()){return PString("");}
41 
42  PPath listMatchingPath("");
43  if(basePath.isDirectoryExist()){
44  PPath completedPathDir(completePathDir(basePath));
45  PPath prefixPath(prefixPathDir(basePath));
46  DIR * dp = opendir(completedPathDir.c_str());
47  dirent * dptr = readdir(dp);
48  while(NULL != dptr){
49  PPath pathName(dptr->d_name);
50  if(pathName != "." && pathName != ".."){
51  listMatchingPath += prefixPath + pathName + PString("\n");
52  }
53  dptr = readdir(dp);
54  }
55  closedir(dp);
56  }else{
57  PPath baseDir(basePath.getParentDirectory()), startNewPath(basePath.getFileName());
58  PPath completedPathDir(completePathDir(baseDir));
59  PPath prefixPath(prefixPathDir(baseDir));
60  DIR * dp = opendir(completedPathDir.c_str());
61 
62  dirent * dptr = readdir(dp);
63  while(NULL != dptr){
64  PPath pathName(dptr->d_name);
65  if(pathName != "." && pathName != ".."){
66  if(pathName.isSameBegining(startNewPath)){
67  listMatchingPath += prefixPath + pathName + PString("\n");
68  }
69  }
70  dptr = readdir(dp);
71  }
72 
73  closedir(dp);
74  }
75 // std::cerr << "path_completion_all : listMatchingPath = '"<<listMatchingPath<<"'" << std::endl;
76  return listMatchingPath;
77 }
78 
80 
84  if(basePath == ""){return PString("./");}
85  PPath listMatchingPath("");
86  if(basePath.isDirectoryExist()){
87  DIR * dp = opendir(basePath.c_str());
88  dirent * dptr = readdir(dp);
89  while(NULL != dptr){
90  if(dptr->d_type == DT_DIR){ //We search for directory only
91  PPath pathName(dptr->d_name);
92  if(pathName != "." && pathName != ".."){
93  listMatchingPath += basePath / pathName + PString("\n");
94  }
95  }
96  dptr = readdir(dp);
97  }
98  }else{
99  PPath baseDir(basePath.getParentDirectory()), startNewPath(basePath.getFileName());
100  DIR * dp = opendir(baseDir.c_str());
101 
102  dirent * dptr = readdir(dp);
103  while(NULL != dptr){
104  if(dptr->d_type == DT_DIR){ //We search for directory only
105  PPath pathName(dptr->d_name);
106  if(pathName != "." && pathName != ".."){
107  if(pathName.isSameBegining(startNewPath)){
108  listMatchingPath += baseDir / pathName + PString("\n");
109  }
110  }
111  }
112  dptr = readdir(dp);
113  }
114 
115  closedir(dp);
116  }
117  return listMatchingPath;
118 }
119 
120 
Path of a directory or a file.
Definition: PPath.h:17
PPath getParentDirectory() const
Get path of parent directory of current path.
Definition: PPath.cpp:207
bool isDirectoryExist() const
Say if the current directory path does exist.
Definition: PPath.cpp:151
bool isFileExist() const
Say if the current file path does exist.
Definition: PPath.cpp:139
PPath getFileName() const
Get the name of the file, from last char to /.
Definition: PPath.cpp:172
Extends the std::string.
Definition: PString.h:16
bool isSameBegining(const PString &beginStr) const
Say if the current PString has the same begining of beginStr.
Definition: PString.cpp:306
PPath prefixPathDir(const PPath &pathDir)
Complete the path directory with a prefix.
PPath path_completion_dirOnly(const PPath &basePath)
Return all directories only which match the basePath.
PPath path_completion_all(const PPath &basePath)
Return all path/files which match the basePath.
PPath completePathDir(const PPath &pathDir)
Complete the path directory.