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 #ifdef __QNX__
31 using std::strlen;
32 using std::strcpy;
33 #endif
34 
35 namespace coil
36 {
37 
61  inline std::string dirname(char* path)
62  {
63  char path_name[strlen(path)+1];
64  strcpy(path_name, path);
65  std::string dir_name = ::dirname(path);
66  return dir_name;
67  }
68 
92  inline std::string basename(const char* path)
93  {
94  char path_name[strlen(path)+1];
95  strcpy(path_name, path);
96  std::string base_name = ::basename(path_name);
97  return base_name;
98  }
99 
125  inline coil::vstring filelist(const char* path, const char* glob_str = "")
126  {
127  struct dirent* ent;
129  bool has_glob(false);
130  std::string pattern;
131 
132  if (path == 0) { return flist; }
133  if (glob_str[0] != '\0') { has_glob = true; }
134 
135  DIR* dir_ptr(::opendir(path));
136  if (dir_ptr == 0) { return flist; }
137 
138  while ((ent = ::readdir(dir_ptr)) != 0)
139  {
140  bool match(true);
141  if (has_glob)
142  {
143  const char* globc(glob_str);
144  std::string fname(ent->d_name);
145  for (size_t i(0); i < fname.size() && *globc != '\0'; ++i, ++globc)
146  {
147  if (*globc == '*')
148  {
149  // the last '*' matches every thing
150  if (globc[1] == '\0') { break; }
151  // consecutive * or + are skiped, but fname keeps pointer
152  if (globc[1] == '*' || globc[1] == '+') { --i; continue; }
153 
154  // advance pointer and find normal characters
155  ++globc;
156  size_t pos(fname.find(*globc, i));
157  if (pos == std::string::npos) { match = false; break; }
158  // matched, and advance i to pos
159  i = pos;
160  }
161  else if (*globc == '+')
162  {
163  // the last '+' matches last one or more characters
164  if (globc[1] == '\0' && !(i + 1 < fname.size())) { break; }
165  // consecutive * or + are skiped, but fname keeps pointer
166  if (globc[1] == '*' || globc[1] == '+') { --i; continue; }
167 
168  // advance pointer and find normal characters
169  ++globc;
170  size_t pos(fname.find(*globc, i + 1));
171  if (pos == std::string::npos) { match = false; break; }
172  // matched, and advance i to pos
173  i = pos;
174  }
175  else
176  {
177  if (fname[i] != *globc) { match = false; }
178  }
179 
180  // in the last fname character, if glob is not end,
181  // or *, fname is not matched.
182  if (i + 1 == fname.size() &&
183  globc[1] != '\0' && globc[1] != '*') { match = false; }
184  }
185  }
186  if (match) { flist.push_back(ent->d_name); }
187  }
188  ::closedir(dir_ptr);
189 
190  return flist;
191  }
192 };
193 
194 #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
def 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 Feb 28 2022 23:00:43