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