ISIS Logo
UTILITIES
EPICS Utilities
FileList.cpp
Go to the documentation of this file.
1 #include <string>
2 #include <list>
3 #include <iostream>
4 #include <pcre.h>
5 
6 #include <epicsExport.h>
7 
8 #include <utilities.h>
9 
11 epicsShareFunc int getFileList(const std::string& dirBase, std::list<std::string>& files)
12 {
13  struct dirent *pDirent;
14  DIR *pDir;
15  files.clear();
16 
17  pDir = opendir(dirBase.c_str());
18 
19  if (pDir == NULL) {
20  std::cerr << "Cannot open directory: " << dirBase << std::endl;
21  return -1;
22  }
23 
24  //First two files may be '.' and '..' so skip
25  std::string read = readdir(pDir)->d_name;
26  if (read != ".")
27  {
28  files.push_back(read);
29  }else{
30  readdir(pDir);
31  }
32 
33  while ( (pDirent = readdir(pDir)) != NULL )
34  {
35  files.push_back(pDirent->d_name);
36  }
37  closedir (pDir);
38 
39  return files.size();
40 
41 }
42 
43 
44 epicsShareFunc int filterList(std::list<std::string>& items, const std::string& regex)
45 {
46  pcre *re;
47  const char *error;
48  int erroffset;
49 
50  re = pcre_compile(
51  regex.c_str(), /* the pattern */
52  0, /* default options */
53  &error, /* for error message */
54  &erroffset, /* for error offset */
55  NULL); /* use default character tables */
56 
57  if (re == NULL)
58  {
59  std::cerr << "PCRE compilation failed" << std::endl;
60  return -1;
61  }
62 
63  for (std::list<std::string>::iterator it = items.begin(); it != items.end(); )
64  {
65  std::string& item = *it;
66  if(pcre_exec(re, NULL, item.c_str(), static_cast<int>(item.length()), 0, 0, NULL, NULL) < 0)
67  it = items.erase(it);
68  else
69  ++it;
70  }
71 
72  return 0;
73 }
epicsShareFunc int filterList(std::list< std::string > &items, const std::string &regex)
Definition: FileList.cpp:44
epicsShareFunc struct dirent * readdir(DIR *dir)
Definition: win32_dirent.c:101
char * d_name
Definition: win32_dirent.h:26
epicsShareFunc int getFileList(const std::string &dirBase, std::list< std::string > &files)
returns -1 if unable to open directory, else number of items in directory
Definition: FileList.cpp:11
epicsShareFunc DIR * opendir(const char *name)
Definition: win32_dirent.c:36
epicsShareFunc int closedir(DIR *dir)
Definition: win32_dirent.c:78
Copyright © 2013 Science and Technology Facilities Council | Generated by   doxygen 1.8.5