3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
8 * Full author contact details are available in file CREDITS.
19 #include "std_sstream.h"
21 #include <boost/regex.hpp>
22 #include <boost/tokenizer.hpp>
27 using std::ostringstream;
35 string const convert_brace_glob(string const & glob)
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("([^,}]+),?");
45 string::const_iterator it = glob.begin();
46 string::const_iterator const end = glob.end();
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);
55 // Everything from the start of the input to
56 // the start of the match.
57 pattern += string(what[-1].first, what[-1].second);
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);
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);
68 // Increment the iterator to the end of the match.
69 it += distance(it, what[0].second);
76 vector<string> const glob(string const & pattern, int flags)
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);
89 vector<string> const expand_globs(string const & mask,
90 string const & directory)
92 typedef boost::tokenizer<boost::char_separator<char> > Tokenizer;
93 boost::char_separator<char> const separator(" ");
95 // Given "<glob> <glob> ... *.{abc,def} <glob>", expand to
96 // "<glob> <glob> ... *.abc *.def <glob>"
97 string const converted_glob = convert_brace_glob(mask);
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());
114 FileFilterList::FileFilterList(string const & qt_style_filter)
116 string const filter = qt_style_filter.empty() ?
117 _("All files (*)") : qt_style_filter;
119 // Split data such as "TeX documents (*.tex);;LyX Documents (*.lyx)"
120 // into individual filters.
121 static boost::regex const separator_re(";;");
123 string::const_iterator it = filter.begin();
124 string::const_iterator const end = filter.end();
126 boost::match_results<string::const_iterator> what;
128 if (!boost::regex_search(it, end, what, separator_re)) {
129 parse_filter(string(it, end));
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));
137 // Increment the iterator to the end of the match.
138 it += distance(it, what[0].second);
143 void FileFilterList::parse_filter(string const & filter)
145 // Matches "TeX documents (*.tex)",
146 // storing "TeX documents " as group 1 and "*.tex" as group 2.
147 static boost::regex const filter_re("([^(]*)\\(([^)]+)\\) *$");
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)));
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)));
161 string const FileFilterList::str(bool expand) const
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();
173 bool const has_description = !it->description().empty();
175 ss << it->description() << " (";
184 } // namespace support