OpenRTM-aist  1.2.0
File.h
Go to the documentation of this file.
1 // -*- C++ -*-
19 #ifndef COIL_FILE_H
20 #define COIL_FILE_H
21 
22 #include <cstring>
23 #include <sys/types.h>
24 #include <dirent.h>
25 #include <libgen.h>
26 
27 #include <string.h>
28 #include <sys/stat.h>
29 
30 
31 #include <coil/config_coil.h>
32 #include <coil/stringutil.h>
33 
34 #ifdef __QNX__
35 using std::strlen;
36 using std::strcpy;
37 #endif
38 
39 namespace coil
40 {
41 
65  inline std::string dirname(char* path)
66  {
67  char path_name[strlen(path)+1];
68  strcpy(path_name, path);
69  std::string dir_name = ::dirname(path_name);
70  return dir_name;
71  }
72 
96  inline std::string basename(const char* path)
97  {
98  char path_name[strlen(path)+1];
99  strcpy(path_name, path);
100  std::string base_name = ::basename(path_name);
101  return base_name;
102  }
103 
129  inline coil::vstring filelist(const char* path, const char* glob_str = "")
130  {
131  struct dirent* ent;
132  coil::vstring flist;
133  bool has_glob(false);
134  std::string pattern;
135 
136  if (path == 0) { return flist; }
137  if (glob_str[0] != '\0') { has_glob = true; }
138 
139  DIR* dir_ptr(::opendir(path));
140  if (dir_ptr == 0) { return flist; }
141 
142  while ((ent = ::readdir(dir_ptr)) != 0)
143  {
144  bool match(true);
145  if (has_glob)
146  {
147  const char* globc(glob_str);
148  std::string fname(ent->d_name);
149  for (size_t i(0); i < fname.size() && *globc != '\0'; ++i, ++globc)
150  {
151  if (*globc == '*')
152  {
153  // the last '*' matches every thing
154  if (globc[1] == '\0') { break; }
155  // consecutive * or + are skiped, but fname keeps pointer
156  if (globc[1] == '*' || globc[1] == '+') { --i; continue; }
157 
158  // advance pointer and find normal characters
159  ++globc;
160  size_t pos(fname.find(*globc, i));
161  if (pos == std::string::npos) { match = false; break; }
162  // matched, and advance i to pos
163  i = pos;
164  }
165  else if (*globc == '+')
166  {
167  // the last '+' matches last one or more characters
168  if (globc[1] == '\0' && !(i + 1 < fname.size())) { break; }
169  // consecutive * or + are skiped, but fname keeps pointer
170  if (globc[1] == '*' || globc[1] == '+') { --i; continue; }
171 
172  // advance pointer and find normal characters
173  ++globc;
174  size_t pos(fname.find(*globc, i + 1));
175  if (pos == std::string::npos) { match = false; break; }
176  // matched, and advance i to pos
177  i = pos;
178  }
179  else
180  {
181  if (fname[i] != *globc) { match = false; }
182  }
183 
184  // in the last fname character, if glob is not end,
185  // or *, fname is not matched.
186  if (i + 1 == fname.size() &&
187  globc[1] != '\0' && globc[1] != '*') { match = false; }
188  }
189  }
190  if (match) { flist.push_back(ent->d_name); }
191  }
192  ::closedir(dir_ptr);
193 
194  return flist;
195  }
196 
197 
218  inline void findFile(std::string dir, std::string filename, coil::vstring &filelist)
219  {
220  struct dirent **namelist=NULL;
221  int files = scandir(dir.c_str(), &namelist, NULL, NULL);
222 
223  for (int i=0; i<files; i++) {
224  std::string dname = namelist[i]->d_name;
225  if( dname != "." && dname != ".." ){
226 
227  std::string fullpath = dir + "/" + dname;
228 
229  struct stat stat_buf;
230  if(stat(fullpath.c_str(), &stat_buf) == 0){
231  if ((stat_buf.st_mode & S_IFMT) == S_IFDIR){
232  findFile(fullpath, filename, filelist);
233  }
234  else
235  {
236  if(dname == filename)
237  {
238  filelist.push_back(fullpath);
239  }
240  }
241  }
242 
243 
244  }
245  }
246  }
247 
248 
269  inline void getFileList(std::string dir, std::string ext, coil::vstring &filelist)
270  {
271  struct dirent **namelist=NULL;
272  int files = scandir(dir.c_str(), &namelist, NULL, NULL);
273 
274  for (int i=0; i<files; i++) {
275  std::string dname = namelist[i]->d_name;
276  if( dname != "." && dname != ".." ){
277 
278  std::string fullpath = dir + "/" + dname;
279 
280  struct stat stat_buf;
281  if(stat(fullpath.c_str(), &stat_buf) == 0){
282  if ((stat_buf.st_mode & S_IFMT) == S_IFDIR){
283  getFileList(fullpath, ext, filelist);
284  }
285  else
286  {
287  coil::vstring filesp = coil::split(dname, ".");
288  if(filesp.back() == ext)
289  {
290  filelist.push_back(fullpath);
291  }
292  }
293  }
294 
295 
296  }
297  }
298 
299  }
300 };
301 
302 #endif // COIL_FILE_H
void findFile(std::string dir, std::string filename, coil::vstring &filelist)
Definition: File.h:218
std::vector< std::string > vstring
Definition: stringutil.h:45
std::string dirname(char *path)
Get a directory part than a file pass.
Definition: File.h:65
coil::vstring filelist(const char *path, const char *glob_str="")
Get file list.
Definition: File.h:129
std::string basename(const char *path)
Get a file name part than a file pass.
Definition: File.h:96
void getFileList(std::string dir, std::string ext, coil::vstring &filelist)
Definition: File.h:269
vstring split(const std::string &input, const std::string &delimiter, bool ignore_empty=false)
Split string by delimiter.
Common Object Interface Layer.
Definition: Affinity.h:28