]> git.lyx.org Git - lyx.git/blob - src/support/globbing.C
Globbing.
[lyx.git] / src / support / globbing.C
1 /**
2  * \file globbing.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include "globbing.h"
12
13 #include "path.h"
14
15 #include <boost/regex.hpp>
16 #include <boost/tokenizer.hpp>
17
18 #include "glob.h"
19
20 using std::string;
21 using std::vector;
22
23
24 namespace lyx {
25 namespace support {
26
27 string const convert_brace_glob(string const & glob)
28 {
29         // Matches " *.{abc,def,ghi}", storing "*." as group 1 and
30         // "abc,def,ghi" as group 2.
31         static boost::regex const glob_re(" *([^ {]*)\\{([^ }]+)\\}");
32         // Matches "abc" and "abc,", storing "abc" as group 1.
33         static boost::regex const block_re("([^,}]+),?");
34
35         string pattern;
36
37         string::const_iterator it = glob.begin();
38         string::const_iterator const end = glob.end();
39         while (true) {
40                 boost::match_results<string::const_iterator> what;
41                 if (!boost::regex_search(it, end, what, glob_re)) {
42                         // Ensure that no information is lost.
43                         pattern += string(it, end);
44                         break;
45                 }
46
47                 // Everything from the start of the input to
48                 // the start of the match.
49                 pattern += string(what[-1].first, what[-1].second);
50
51                 // Given " *.{abc,def}", head == "*." and tail == "abc,def".
52                 string const head = string(what[1].first, what[1].second);
53                 string const tail = string(what[2].first, what[2].second);
54
55                 // Split the ','-separated chunks of tail so that
56                 // $head{$chunk1,$chunk2} becomes "$head$chunk1 $head$chunk2".
57                 string const fmt = " " + head + "$1";
58                 pattern += boost::regex_merge(tail, block_re, fmt);
59
60                 // Increment the iterator to the end of the match.
61                 it += std::distance(it, what[0].second);
62         }
63
64         return pattern;
65 }
66
67
68 vector<string> const glob(string const & pattern, int flags)
69 {
70         glob_t glob_buffer = {0, 0, 0, 0, 0, 0, 0, 0, 0};
71         glob(pattern.c_str(), flags, 0, &glob_buffer);
72         vector<string> const matches(glob_buffer.gl_pathv,
73                                      glob_buffer.gl_pathv +
74                                      glob_buffer.gl_pathc);
75         globfree(&glob_buffer);
76         return matches;
77 }
78
79
80 vector<string> const expand_globs(string const & mask,
81                                   std::string const & directory)
82 {
83         typedef boost::tokenizer<boost::char_separator<char> > Tokenizer;
84         boost::char_separator<char> const separator(" ");
85
86         // Given "<glob> <glob> ... *.{abc,def} <glob>", expand to
87         //       "<glob> <glob> ... *.abc *.def <glob>"
88         string const expanded_glob = convert_brace_glob(mask);
89
90         Path p(directory);
91
92         // Split into individual globs and then call 'glob' on each one.
93         vector<string> matches;
94         Tokenizer const tokens(expanded_glob, separator);
95         Tokenizer::const_iterator it = tokens.begin();
96         Tokenizer::const_iterator const end = tokens.end();
97         for (; it != end; ++it) {
98                 vector<string> const tmp = glob(*it);
99                 matches.insert(matches.end(), tmp.begin(), tmp.end());
100         }
101         return matches;
102 }
103
104 } // namespace support
105 } // namespace lyx