win32/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 <windows.h>
23 #include <coil/config_coil.h>
24 #include <coil/stringutil.h>
25 
26 namespace coil
27 {
28  const unsigned int MaxPathLength(1024);
29 
56  inline std::string dirname(char* path)
57  {
58  char return_dirname[MaxPathLength + 1];
59 
60  size_t len = strlen(path);
61  if (len > (sizeof(return_dirname) / sizeof(char)))
62  {
63  len = sizeof(return_dirname) / sizeof(char);
64  }
65  std::strncpy(return_dirname, path, len);
66  return_dirname[len] = '\0';
67 
68  const char delimiter('/');
69  char *p = std::strrchr(return_dirname, delimiter);
70 
71  std::string dir_name;
72  if (p)
73  {
74  if(p != return_dirname)
75  {
76  if(*(p+1) == '\0')
77  {
78  *p = '\0';
79  dir_name = dirname(return_dirname);
80  }
81  else
82  {
83  *p = '\0';
84  dir_name = return_dirname;
85  }
86  }
87  else
88  {
89  *(p+1) = '\0';
90  dir_name = return_dirname;
91  }
92  }
93  else
94  {
95  dir_name = ".";
96  }
97  return dir_name;
98  }
99 
123  inline std::string basename(const char* path)
124  {
125  char p[MaxPathLength + 1];
126 
127  size_t len = strlen(path);
128  if (len > (sizeof(p) / sizeof(char)))
129  {
130  len = sizeof(p) / sizeof(char);
131  }
132  std::strncpy(p, path, len);
133  p[len] = '\0';
134 
135  const char delimiter('/');
136  char *pdelimiter = std::strrchr(p, delimiter);
137 
138  std::string base_name(p);
139  if (pdelimiter)
140  {
141  if(pdelimiter != p)
142  {
143  if(*(pdelimiter+1) == '\0')
144  {
145  *pdelimiter = '\0';
146  base_name = basename(p);
147  }
148  else
149  {
150  pdelimiter++;
151  base_name = pdelimiter;
152  }
153  }
154  else
155  {
156  if(*(pdelimiter+1) != '\0')
157  {
158  pdelimiter++;
159  base_name = pdelimiter;
160  }
161  else
162  {
163  base_name = pdelimiter;
164  }
165 
166  }
167  }
168  return base_name;
169  }
170 
171 
172 
173  typedef unsigned int ino_t;
174 
186  struct dirent
187  {
188  ino_t d_ino;
189  char d_name[_MAX_PATH];
190  };
191 
203  typedef struct
204  {
205  HANDLE h;
206  WIN32_FIND_DATAA *fd;
207  BOOL has_next;
208  struct dirent entry;
209  } DIR;
210 
211 
235  DIR* opendir(const char *name)
236  {
237  if (name == 0) { return 0; }
238  std::string path(name);
239  if (path.empty()) { return 0; }
240 
241  // path has at least one or more path characters
242  if (*(path.end() - 1) != '\\' && *(path.end() - 1) != '/')
243  {
244  std::string::size_type pos(path.find("/"));
245  if (pos == std::string::npos) { path.push_back('\\'); } // delim = '\'
246  else { path.push_back('/'); } // delim = '/'
247  }
248  path.push_back('*'); // now path is "/dir/dir/../*"
249 
250  // fd will be held by DIR structure
251  HANDLE dhandle;
252  WIN32_FIND_DATAA* fd;
253  try
254  {
255  fd = new WIN32_FIND_DATAA();
256  dhandle = FindFirstFileA(path.c_str(), fd);
257  if (dhandle == INVALID_HANDLE_VALUE) { delete fd; return 0; }
258 
259  }
260  catch (...)
261  {
262  FindClose(dhandle);
263  return 0;
264  }
265 
266  DIR* dir;
267  try
268  {
269  dir = new DIR();
270  dir->h = dhandle;
271  dir->fd = fd;
272  dir->has_next = TRUE;
273  }
274  catch (...)
275  {
276  delete fd;
277  return 0;
278  }
279  return dir;
280  }
281 
282 
306  dirent* readdir(DIR *dir)
307  {
308  if (dir == 0) { return 0; }
309  if (dir->fd == 0) { return 0;}
310  if (!dir->has_next) { return 0; }
311 
312  strcpy_s(dir->entry.d_name, _MAX_PATH, dir->fd->cFileName);
313  dir->has_next = FindNextFileA(dir->h, dir->fd);
314 
315  return &dir->entry;
316  }
317 
341  int closedir(DIR *dir)
342  {
343  if (dir == 0) { return -1; }
344  if (dir->h != 0 && dir->h != INVALID_HANDLE_VALUE)
345  {
346  FindClose(dir->h);
347  }
348  if (dir->fd != 0) { delete dir->fd; }
349  delete dir;
350 
351  return 0;
352  }
353 
354 
355 
381  inline coil::vstring filelist(const char* path, const char* glob_str = "")
382  {
383  struct dirent* ent;
385  bool has_glob(false);
386  std::string pattern;
387 
388  if (path == 0) { return flist; }
389  if (glob_str[0] != '\0') { has_glob = true; }
390 
391  DIR* dir_ptr(coil::opendir(path));
392  if (dir_ptr == 0) { return flist; }
393 
394  while ((ent = coil::readdir(dir_ptr)) != 0)
395  {
396  bool match(true);
397  if (has_glob)
398  {
399  const char* globc(glob_str);
400  std::string fname(ent->d_name);
401  for (size_t i(0); i < fname.size() && globc != '\0'; ++i, ++globc)
402  {
403  if (*globc == '*')
404  {
405  // the last '*' matches every thing
406  if (globc[1] == '\0') { break; }
407  // consecutive * or + are skiped, but fname keeps pointer
408  if (globc[1] == '*' || globc[1] == '+') { --i; continue; }
409 
410  // advance pointer and find normal characters
411  ++globc;
412  size_t pos(fname.find(*globc, i));
413  if (pos == std::string::npos) { match = false; break; }
414  // matched, and advance i to pos
415  i = pos;
416  }
417  else if (*globc == '+')
418  {
419  // the last '+' matches last one or more characters
420  if (globc[1] == '\0' && !(i + 1 < fname.size())) { break; }
421  // consecutive * or + are skiped, but fname keeps pointer
422  if (globc[1] == '*' || globc[1] == '+') { --i; continue; }
423 
424  // advance pointer and find normal characters
425  ++globc;
426  size_t pos(fname.find(*globc, i + 1));
427  if (pos == std::string::npos) { match = false; break; }
428  // matched, and advance i to pos
429  i = pos;
430  }
431  else
432  {
433  if (fname[i] != *globc) { match = false; }
434  }
435 
436  // in the last fname character, if glob is not end,
437  // or *, fname is not matched.
438  if (i + 1 == fname.size() &&
439  globc[1] != '\0' && globc[1] != '*') { match = false; }
440  }
441  }
442  if (match) { flist.push_back(ent->d_name); }
443  }
444  coil::closedir(dir_ptr);
445 
446  return flist;
447  }
448 
449 
450 };
451 
452 #endif // COIL_FILE_H
unsigned int ino_t
struct dirent entry
list flist
Definition: omniwxs.py:84
const unsigned int MaxPathLength(1024)
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.
WIN32_FIND_DATAA * fd
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