OpenRTM-aist  1.2.1
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 #ifndef COIL_OS_QNX
222  int files = scandir(dir.c_str(), &namelist, NULL, NULL);
223 #else
224  int files = scandir(const_cast<char*>(dir.c_str()), &namelist, NULL, NULL);
225 #endif
226  for (int i=0; i<files; i++) {
227  std::string dname = namelist[i]->d_name;
228  if( dname != "." && dname != ".." ){
229 
230  std::string fullpath = dir + "/" + dname;
231 
232  struct stat stat_buf;
233  if(stat(fullpath.c_str(), &stat_buf) == 0){
234  if ((stat_buf.st_mode & S_IFMT) == S_IFDIR){
235  findFile(fullpath, filename, filelist);
236  }
237  else
238  {
239  if(dname == filename)
240  {
241  filelist.push_back(fullpath);
242  }
243  }
244  }
245 
246 
247  }
248  }
249  }
250 
251 
272  inline void getFileList(std::string dir, std::string ext, coil::vstring &filelist)
273  {
274  struct dirent **namelist=NULL;
275 #ifndef COIL_OS_QNX
276  int files = scandir(dir.c_str(), &namelist, NULL, NULL);
277 #else
278  int files = scandir(const_cast<char*>(dir.c_str()), &namelist, NULL, NULL);
279 #endif
280 
281  for (int i=0; i<files; i++) {
282  std::string dname = namelist[i]->d_name;
283  if( dname != "." && dname != ".." ){
284 
285  std::string fullpath = dir + "/" + dname;
286 
287  struct stat stat_buf;
288  if(stat(fullpath.c_str(), &stat_buf) == 0){
289  if ((stat_buf.st_mode & S_IFMT) == S_IFDIR){
290  getFileList(fullpath, ext, filelist);
291  }
292  else
293  {
294  coil::vstring filesp = coil::split(dname, ".");
295  if(filesp.back() == ext)
296  {
297  filelist.push_back(fullpath);
298  }
299  }
300  }
301 
302 
303  }
304  }
305 
306  }
307 };
308 
309 #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:272
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