Jpp 21.0.0-rc.1-88-g0130508c4
the software that should make you happy
Loading...
Searching...
No Matches
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#include <filesystem>
9
10
11/**
12 * \file
13 * File list.
14 * \author mdejong
15 */
16namespace JSYSTEM {}
17namespace JPP { using namespace JSYSTEM; }
18
19namespace JSYSTEM {
20
21 /**
22 * Auxiliary class to list files.
23 */
24 struct JGlob :
25 public std::vector<std::string>
26 {
27 /**
28 * Default constructor.
29 */
31 {}
32
33
34 /**
35 * Constructor.
36 *
37 * \param pattern pattern
38 */
39 JGlob(const std::string& pattern)
40 {
41 (*this)(pattern);
42 }
43
44
45 /**
46 * Constructor.
47 *
48 * \param buffer list of patterns
49 */
51 {
52 (*this)(buffer);
53 }
54
55
56 /**
57 * Get list of files.
58 *
59 * \param pattern pattern
60 * \return list of files
61 */
62 const JGlob& operator()(const std::string& pattern)
63 {
64 this->clear();
65
66 evaluate(pattern);
67
68 return *this;
69 }
70
71
72 /**
73 * Get list of files.
74 *
75 *
76 * \param buffer list of patterns
77 * \return list of files
78 */
80 {
81 this->clear();
82
83 for (const std::string& pattern : buffer) {
84 evaluate(pattern);
85 }
86
87 return *this;
88 }
89
90
91 /**
92 * Push back file.
93 * This method constitutes a light weight wrapper to resolve symbolic links on the fly.
94 *
95 * \param file_name file name
96 */
97 void push_back(const std::string& file_name)
98 {
99 using namespace std::filesystem;
100
101 path ps(file_name);
102
103 if (is_symlink(ps)) {
104 ps = read_symlink(ps);
105 }
106
107 static_cast<std::vector<std::string>&>(*this).push_back(ps.string());
108 }
109
110 private:
111 /**
112 * Process pattern.
113 *
114 * \param pattern pattern
115 */
116 void evaluate(const std::string& pattern)
117 {
118 memset(&buffer, 0, sizeof(buffer));
119
120 const int value = glob(pattern.c_str(), GLOB_TILDE | GLOB_BRACE, NULL, &buffer);
121
122 if (value == 0) {
123
124 for(size_t i = 0; i < buffer.gl_pathc; ++i) {
125 this->push_back(buffer.gl_pathv[i]);
126 }
127
128 } else {
129
130 this->push_back(pattern);
131 }
132
133 globfree(&buffer);
134 }
135
136 glob_t buffer;
137 };
138
139
140 /**
141 * Function object to get list of files for given pattern.
142 */
144}
145
146#endif
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary classes and methods for operating system calls.
static JGlob getFilenames
Function object to get list of files for given pattern.
Definition JGlob.hh:143
Auxiliary class to list files.
Definition JGlob.hh:26
glob_t buffer
Definition JGlob.hh:136
const JGlob & operator()(const std::string &pattern)
Get list of files.
Definition JGlob.hh:62
JGlob(const std::string &pattern)
Constructor.
Definition JGlob.hh:39
const JGlob & operator()(const std::vector< std::string > &buffer)
Get list of files.
Definition JGlob.hh:79
void evaluate(const std::string &pattern)
Process pattern.
Definition JGlob.hh:116
JGlob()
Default constructor.
Definition JGlob.hh:30
JGlob(const std::vector< std::string > &buffer)
Constructor.
Definition JGlob.hh:50
void push_back(const std::string &file_name)
Push back file.
Definition JGlob.hh:97