posix/coil/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 <coil/config_coil.h>
28 #include <coil/stringutil.h>
29 
30 namespace coil
31 {
32 
56  inline std::string dirname(char* path)
57  {
58  char path_name[strlen(path)+1];
59  strcpy(path_name, path);
60  std::string dir_name = ::dirname(path);
61  return dir_name;
62  }
63 
87  inline std::string basename(const char* path)
88  {
89  char path_name[strlen(path)+1];
90  strcpy(path_name, path);
91  std::string base_name = ::basename(path_name);
92  return base_name;
93  }
94 
120  inline coil::vstring filelist(const char* path, const char* glob_str = "")
121  {
122  struct dirent* ent;
124  bool has_glob(false);
125  std::string pattern;
126 
127  if (path == 0) { return flist; }
128  if (glob_str[0] != '\0') { has_glob = true; }
129 
130  DIR* dir_ptr(::opendir(path));
131  if (dir_ptr == 0) { return flist; }
132 
133  while ((ent = ::readdir(dir_ptr)) != 0)
134  {
135  bool match(true);
136  if (has_glob)
137  {
138  const char* globc(glob_str);
139  std::string fname(ent->d_name);
140  for (size_t i(0); i < fname.size() && globc != '\0'; ++i, ++globc)
141  {
142  if (*globc == '*')
143  {
144  // the last '*' matches every thing
145  if (globc[1] == '\0') { break; }
146  // consecutive * or + are skiped, but fname keeps pointer
147  if (globc[1] == '*' || globc[1] == '+') { --i; continue; }
148 
149  // advance pointer and find normal characters
150  ++globc;
151  size_t pos(fname.find(*globc, i));
152  if (pos == std::string::npos) { match = false; break; }
153  // matched, and advance i to pos
154  i = pos;
155  }
156  else if (*globc == '+')
157  {
158  // the last '+' matches last one or more characters
159  if (globc[1] == '\0' && !(i + 1 < fname.size())) { break; }
160  // consecutive * or + are skiped, but fname keeps pointer
161  if (globc[1] == '*' || globc[1] == '+') { --i; continue; }
162 
163  // advance pointer and find normal characters
164  ++globc;
165  size_t pos(fname.find(*globc, i + 1));
166  if (pos == std::string::npos) { match = false; break; }
167  // matched, and advance i to pos
168  i = pos;
169  }
170  else
171  {
172  if (fname[i] != *globc) { match = false; }
173  }
174 
175  // in the last fname character, if glob is not end,
176  // or *, fname is not matched.
177  if (i + 1 == fname.size() &&
178  globc[1] != '\0' && globc[1] != '*') { match = false; }
179  }
180  }
181  if (match) { flist.push_back(ent->d_name); }
182  }
183  ::closedir(dir_ptr);
184 
185  return flist;
186  }
187 };
188 
189 #endif // COIL_FILE_H
list flist
Definition: omniwxs.py:84
DIR * opendir(const char *name)
Open a directory stream.
Structure for directory entry.
std::vector< std::string > vstring
Definition: stringutil.h:37
dir_name
Definition: omniwxs.py:76
char d_name[_MAX_PATH]
coil::vstring filelist(const char *path, const char *glob_str="")
Get file list.
dirent * readdir(DIR *dir)
Get a directory entry pointer.
const char * basename(const char *path)
Get a file name part than a file pass.
Definition: ace/coil/File.h:33
int closedir(DIR *dir)
Close a directory stream.
Structure for directory stream.
Common Object Interface Layer.
const char * dirname(const char *path)
Definition: ace/coil/File.h:28


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Mon Jun 10 2019 14:07:52