Jpp  15.0.5
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JGlob.hh
Go to the documentation of this file.
1 #ifndef __JSYSTEM_JGLOB__
2 #define __JSYSTEM_JGLOB__
3 
4 #include <glob.h>
5 #include <string.h>
6 #include <string>
7 #include <vector>
8 
9 #include "JLang/JException.hh"
10 
11 
12 /**
13  * \file
14  * File list.
15  * \author mdejong
16  */
17 namespace JSYSTEM {}
18 namespace JPP { using namespace JSYSTEM; }
19 
20 namespace JSYSTEM {
21 
23 
24  /**
25  * Auxiliary class to list files.
26  */
27  struct JGlob :
28  public std::vector<std::string>
29  {
30  /**
31  * Default constructor.
32  */
34  {}
35 
36 
37  /**
38  * Constructor.
39  *
40  * \param pattern pattern
41  */
42  JGlob(const std::string& pattern)
43  {
44  (*this)(pattern);
45  }
46 
47 
48  /**
49  * Get list of files.
50  *
51  * \param pattern pattern
52  * \return list of files
53  */
54  const JGlob& operator()(const std::string& pattern)
55  {
56  this->clear();
57 
58  memset(&buffer, 0, sizeof(buffer));
59 
60  const int value = glob(pattern.c_str(), GLOB_TILDE, NULL, &buffer);
61 
62  if (value != 0) {
63 
64  globfree(&buffer);
65 
66  THROW(JRunTimeException, "glob() failed " << pattern << " error " << value);
67  }
68 
69  for(size_t i = 0; i < buffer.gl_pathc; ++i) {
70  this->push_back(buffer.gl_pathv[i]);
71  }
72 
73  globfree(&buffer);
74 
75  return *this;
76  }
77 
78  private:
79  glob_t buffer;
80  };
81 
82 
83  /**
84  * Function object to get list of files for given pattern.
85  */
87 }
88 
89 #endif
Exceptions.
glob_t buffer
Definition: JGlob.hh:79
JGlob(const std::string &pattern)
Constructor.
Definition: JGlob.hh:42
static JGlob getFilenames
Function object to get list of files for given pattern.
Definition: JGlob.hh:86
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:670
Auxiliary class to list files.
Definition: JGlob.hh:27
JGlob()
Default constructor.
Definition: JGlob.hh:33
const JGlob & operator()(const std::string &pattern)
Get list of files.
Definition: JGlob.hh:54
Run time exception.
Definition: JException.hh:594