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