Structural deformable models
FFind.h
Go to the documentation of this file.
1 #ifndef _FFIND_H_
2 #define _FFIND_H_
3 
4 #include <iostream>
5 #include <string>
6 #include <list>
7 
8 #ifdef LINUX
9 #include <libgen.h>
10 #include <dirent.h>
11 #endif
12 
13 #include "utils.h"
14 
15 class FFind
16 {
17 public:
18  FFind() {}
19  FFind(const std::string& pattern) { setPattern(pattern); }
20  void setPattern(const std::string& pattern) {
21  char *dname=NULL,*bname=NULL;
22 #ifdef LINUX
23  char *p1 = strdup(pattern.c_str());
24  char *p2 = strdup(pattern.c_str());
25  dname = dirname(p1);
26  bname = basename(p2);
27  m_Pattern = bname;
28  m_Dir = dname;
29  if(p1) free(p1);
30  if(p2) free(p2);
31 #endif
32  }
33 
34  template<class T>
35  int find(const std::string& fmask, T iter) {
36  setPattern(fmask);
37  int n,nn;
38 #ifdef LINUX
39  struct dirent **namelist;
40  // THIS IS NOT THREADSAFE :-(
42  nn = n = scandir(m_Dir.c_str(), &namelist, __fixed_fnmatch, alphasort);
43  if (n < 0)
44  return 0;
45  else {
46  while(n--) {
47  *iter++ = namelist[n]->d_name;
48  free(namelist[n]);
49  }
50  free(namelist);
51  }
52 #else
53  nn = n = ::findFiles(fmask, iter);
54 #endif
55  return nn;
56  }
57 
58  template<class T>
59  static int findFiles(const std::string& fmask, T iter) {
60  FFind ff;
61  return ff.find(fmask, iter);
62  }
63 
64 protected:
65  std::string m_Pattern;
66  std::string m_Dir;
67 };
68 
69 #endif
#define NULL
Definition: simpletypes.h:9
void setPattern(const std::string &pattern)
Definition: FFind.h:20
int find(const std::string &fmask, T iter)
Definition: FFind.h:35
int findFiles(const std::string &fmask, T iter)
Definition: ffind.cpp:33
const string & __set_fnmatchpattern(const string &pattern)
Definition: utils.cpp:15
string m_Pattern
Definition: ffind.cpp:53
Definition: ffind.cpp:16
FFind()
Definition: FFind.h:18
static int findFiles(const std::string &fmask, T iter)
Definition: FFind.h:59
std::string m_Pattern
Definition: FFind.h:65
string m_Dir
Definition: ffind.cpp:54
FFind(const std::string &pattern)
Definition: FFind.h:19
void setPattern(const string &pattern)
Definition: ffind.cpp:20
int __fixed_fnmatch(const struct dirent *dent)
Definition: ffind.cpp:12
std::string m_Dir
Definition: FFind.h:66