]> git.lyx.org Git - lyx.git/blob - src/support/globbing.C
support fully the LANGUAGE variable
[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 <config.h>
12
13 #include "globbing.h"
14
15 #include "gettext.h"
16
17 #include "lstrings.h"
18 #include "path.h"
19 #include "std_sstream.h"
20
21 #include <boost/regex.hpp>
22 #include <boost/tokenizer.hpp>
23
24 #include "glob.h"
25
26 using std::distance;
27 using std::ostringstream;
28 using std::string;
29 using std::vector;
30
31
32 namespace lyx {
33 namespace support {
34
35 string const convert_brace_glob(string const & glob)
36 {
37         // Matches " *.{abc,def,ghi}", storing "*." as group 1 and
38         // "abc,def,ghi" as group 2.
39         static boost::regex const glob_re(" *([^ {]*)\\{([^ }]+)\\}");
40         // Matches "abc" and "abc,", storing "abc" as group 1.
41         static boost::regex const block_re("([^,}]+),?");
42
43         string pattern;
44
45         string::const_iterator it = glob.begin();
46         string::const_iterator const end = glob.end();
47         while (true) {
48                 boost::match_results<string::const_iterator> what;
49                 if (!boost::regex_search(it, end, what, glob_re)) {
50                         // Ensure that no information is lost.
51                         pattern += string(it, end);
52                         break;
53                 }
54
55                 // Everything from the start of the input to
56                 // the start of the match.
57                 pattern += string(what[-1].first, what[-1].second);
58
59                 // Given " *.{abc,def}", head == "*." and tail == "abc,def".
60                 string const head = string(what[1].first, what[1].second);
61                 string const tail = string(what[2].first, what[2].second);
62
63                 // Split the ','-separated chunks of tail so that
64                 // $head{$chunk1,$chunk2} becomes "$head$chunk1 $head$chunk2".
65                 string const fmt = " " + head + "$1";
66                 pattern += boost::regex_merge(tail, block_re, fmt);
67
68                 // Increment the iterator to the end of the match.
69                 it += distance(it, what[0].second);
70         }
71
72         return pattern;
73 }
74
75
76 vector<string> const glob(string const & pattern, int flags)
77 {
78         glob_t glob_buffer;
79         glob_buffer.gl_offs = 0;
80         glob(pattern.c_str(), flags, 0, &glob_buffer);
81         vector<string> const matches(glob_buffer.gl_pathv,
82                                      glob_buffer.gl_pathv +
83                                      glob_buffer.gl_pathc);
84         globfree(&glob_buffer);
85         return matches;
86 }
87
88
89 vector<string> const expand_globs(string const & mask,
90                                   string const & directory)
91 {
92         typedef boost::tokenizer<boost::char_separator<char> > Tokenizer;
93         boost::char_separator<char> const separator(" ");
94
95         // Given "<glob> <glob> ... *.{abc,def} <glob>", expand to
96         //       "<glob> <glob> ... *.abc *.def <glob>"
97         string const converted_glob = convert_brace_glob(mask);
98
99         Path p(directory);
100
101         // Split into individual globs and then call 'glob' on each one.
102         vector<string> matches;
103         Tokenizer const tokens(converted_glob, separator);
104         Tokenizer::const_iterator it = tokens.begin();
105         Tokenizer::const_iterator const end = tokens.end();
106         for (; it != end; ++it) {
107                 vector<string> const tmp = glob(*it);
108                 matches.insert(matches.end(), tmp.begin(), tmp.end());
109         }
110         return matches;
111 }
112
113
114 FileFilterList::FileFilterList(string const & qt_style_filter)
115 {
116         string const filter = qt_style_filter.empty() ?
117                 _("All files (*)") : qt_style_filter;
118
119         // Split data such as "TeX documents (*.tex);;LyX Documents (*.lyx)"
120         // into individual filters.
121         static boost::regex const separator_re(";;");
122
123         string::const_iterator it = filter.begin();
124         string::const_iterator const end = filter.end();
125         while (true) {
126                 boost::match_results<string::const_iterator> what;
127
128                 if (!boost::regex_search(it, end, what, separator_re)) {
129                         parse_filter(string(it, end));
130                         break;
131                 }
132
133                 // Everything from the start of the input to
134                 // the start of the match.
135                 parse_filter(string(what[-1].first, what[-1].second));
136
137                 // Increment the iterator to the end of the match.
138                 it += distance(it, what[0].second);
139         }
140 }
141
142
143 void FileFilterList::parse_filter(string const & filter)
144 {
145         // Matches "TeX documents (*.tex)",
146         // storing "TeX documents " as group 1 and "*.tex" as group 2.
147         static boost::regex const filter_re("([^(]*)\\(([^)]+)\\) *$");
148
149         boost::match_results<string::const_iterator> what;
150         if (!boost::regex_search(filter, what, filter_re)) {
151                 // Just a glob, no description.
152                 filters_.push_back(Filter(string(), trim(filter)));
153         } else {
154                 string const desc = string(what[1].first, what[1].second);
155                 string const globs = string(what[2].first, what[2].second);
156                 filters_.push_back(Filter(trim(desc), trim(globs)));
157         }
158 }
159
160
161 string const FileFilterList::str(bool expand) const
162 {
163         ostringstream ss;
164
165         vector<Filter>::const_iterator const begin = filters_.begin();
166         vector<Filter>::const_iterator const end = filters_.end();
167         vector<Filter>::const_iterator it = begin;
168         for (; it != end; ++it) {
169                 string const globs = expand ?
170                         convert_brace_glob(it->globs()) : it->globs();
171                 if (it != begin)
172                         ss << ";;";
173                 bool const has_description = !it->description().empty();
174                 if (has_description)
175                         ss << it->description() << " (";
176                 ss << globs;
177                 if (has_description)
178                         ss << ')';
179         }
180
181         return ss.str();
182 }
183
184 } // namespace support
185 } // namespace lyx