Qore Programming Language 1.19.1
Loading...
Searching...
No Matches
qore_glob.h
1/* -*- mode: c++; indent-tabs-mode: nil -*- */
2/*
3 glob.h
4
5 Qore Programming Language
6
7 Copyright (C) 2003 - 2023 Qore Technologies, s.r.o.
8
9 Permission is hereby granted, free of charge, to any person obtaining a
10 copy of this software and associated documentation files (the "Software"),
11 to deal in the Software without restriction, including without limitation
12 the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 and/or sell copies of the Software, and to permit persons to whom the
14 Software is furnished to do so, subject to the following conditions:
15
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 DEALINGS IN THE SOFTWARE.
26
27 Note that the Qore library is released under a choice of three open-source
28 licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
29 information.
30*/
31
32#ifndef _QORE_INTERN_GLOB_H
33#define _QORE_INTERN_GLOB_H
34
35#include <algorithm>
36#include <cassert>
37#include <string>
38#include <vector>
39
40#include "qore/intern/QoreRegex.h"
41
42typedef int (*glob_error_t)(const char *, int);
43
44#ifdef _WIN32
45
46// some glob options
47#define GLOB_NONE 0
48#define GLOB_NOSORT (1 << 0)
49
50class QoreGlobWin {
51protected:
52 typedef std::vector<std::string> names_t;
53 names_t names;
54
55public:
56 size_t gl_pathc;
57 const char** gl_pathv;
58
59 DLLLOCAL QoreGlobWin() : gl_pathc(0), gl_pathv(0) {
60 }
61
62 DLLLOCAL ~QoreGlobWin() {
63 reset();
64 }
65
66 DLLLOCAL int set(const char* pattern, int flags, glob_error_t errfunc) {
67 assert(!flags);
68 assert(!errfunc);
69 reset();
70
71 // normalize the path
72 QoreString path(pattern);
73
74 // save the original dir name
75 QoreString orig_dir((const char*)q_dirname(path.c_str()));
76 //printd(5, "glob() dir: '%s'\n", orig_dir.c_str());
77 if (orig_dir == ".")
78 orig_dir.clear();
79 else
80 orig_dir.concat('\\');
81
82 q_normalize_path(path);
83 char* dirp = q_dirname(path.c_str());
84 unsigned len = strlen(dirp);
85 QoreString dir(dirp, len, len + 1, QCS_DEFAULT);
86
87 // set the pattern to get all files in the directory, and then match according to glob() rules
88 dir.concat("\\*.*");
89 WIN32_FIND_DATA pfd;
90 HANDLE h = ::FindFirstFile(dir.getBuffer(), &pfd);
91 ON_BLOCK_EXIT(::FindClose, h);
92
93 // remove wildcard to reuse directory name for matches
94 if (len > 1)
95 dir.terminate(dir.size() - 3);
96 else
97 dir.clear();
98
99 // make regex pattern
100 QoreString str(q_basenameptr(path.c_str()));
101
102 // check if we should get files that start with a "."
103 bool get_dot = (str[0] == '.');
104 //printd(5, "QoreGlobWin::set() path: '%s' dir: '%s' str: '%s' get_dot: %d\n", path.c_str(), dir.c_str(), str.c_str());
105
106 str.replaceAll(".", "\\.");
107 str.replaceAll("?", ".");
108 str.replaceAll("*", ".*");
109 str.prepend("^");
110 str.concat("$");
111
112 ExceptionSink xsink;
113 QoreRegex qrn(&str, PCRE_CASELESS, &xsink);
114 if (xsink)
115 return -1;
116
117 while (FindNextFile(h, &pfd)) {
118 if (pfd.cFileName[0] == '.' && !get_dot)
119 continue;
120 if (qrn.exec(pfd.cFileName, strlen(pfd.cFileName))) {
121 QoreString str(orig_dir);
122 str.concat(pfd.cFileName);
123 names.push_back(str.c_str());
124 //printd(5, "QoreGlobWin::set(pattern='%s') dir='%s' regex='%s' %s MATCHED\n", pattern, dir.getBuffer(), str.getBuffer(), pfd.cFileName);
125 }
126 }
127
128 if (names.size()) {
129 if (!(flags & GLOB_NOSORT))
130 std::sort(names.begin(), names.end());
131
132 gl_pathc = names.size();
133
134 gl_pathv = (const char**)malloc(sizeof(char*) * names.size());
135 for (unsigned i = 0; i < names.size(); ++i) {
136 gl_pathv[i] = names[i].c_str();
137 }
138 }
139
140 return 0;
141 }
142
143 DLLLOCAL int reset() {
144 names.clear();
145 gl_pathc = 0;
146 if (gl_pathv) {
147 free(gl_pathv);
148 gl_pathv = 0;
149 }
150 return 0;
151 }
152};
153
154typedef QoreGlobWin glob_t;
155#else
156#error no glob implementation for this platform
157#endif
158
159// check prototypes
160DLLLOCAL int glob(const char *pattern, int flags, glob_error_t errfunc, glob_t *buf);
161DLLLOCAL int globfree(glob_t *buf);
162
163#endif
DLLEXPORT const QoreEncoding * QCS_DEFAULT
the default encoding for the Qore library
DLLEXPORT char * q_dirname(const char *path)
thread-safe dirname function (resulting pointer must be free()ed)
DLLEXPORT char * q_basenameptr(const char *path)
returns a pointer within the same string
DLLEXPORT void q_normalize_path(QoreString &path, const char *cwd=0)
normalizes the given path for the current platform in place (makes absolute, removes "....
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:50
Qore's string type supported by the QoreEncoding class.
Definition: QoreString.h:93