Jpp  18.3.1
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  * Constructor.
50  *
51  * \param buffer list of patterns
52  */
54  {
55  (*this)(buffer);
56  }
57 
58 
59  /**
60  * Get list of files.
61  *
62  * \param pattern pattern
63  * \return list of files
64  */
65  const JGlob& operator()(const std::string& pattern)
66  {
67  this->clear();
68 
69  evaluate(pattern);
70 
71  return *this;
72  }
73 
74 
75  /**
76  * Get list of files.
77  *
78  *
79  * \param buffer list of patterns
80  * \return list of files
81  */
83  {
84  this->clear();
85 
86  for (const std::string& pattern : buffer) {
87  evaluate(pattern);
88  }
89 
90  return *this;
91  }
92 
93 
94  private:
95  /**
96  * Process pattern.
97  *
98  * \param pattern pattern
99  */
100  void evaluate(const std::string& pattern)
101  {
102  memset(&buffer, 0, sizeof(buffer));
103 
104  const int value = glob(pattern.c_str(), GLOB_TILDE, NULL, &buffer);
105 
106  if (value != 0) {
107 
108  globfree(&buffer);
109 
110  THROW(JRunTimeException, "glob() failed " << pattern << " error " << value);
111  }
112 
113  for(size_t i = 0; i < buffer.gl_pathc; ++i) {
114  this->push_back(buffer.gl_pathv[i]);
115  }
116 
117  globfree(&buffer);
118  }
119 
120  glob_t buffer;
121  };
122 
123 
124  /**
125  * Function object to get list of files for given pattern.
126  */
128 }
129 
130 #endif
Exceptions.
glob_t buffer
Definition: JGlob.hh:120
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:127
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:712
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:65
Run time exception.
Definition: JException.hh:628
then awk string
const JGlob & operator()(const std::vector< std::string > &buffer)
Get list of files.
Definition: JGlob.hh:82
JGlob(const std::vector< std::string > &buffer)
Constructor.
Definition: JGlob.hh:53
void evaluate(const std::string &pattern)
Process pattern.
Definition: JGlob.hh:100