2 * \file FileFilterList.cpp
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.
13 #include "support/FileFilterList.h"
14 #include "support/lstrings.h"
16 // FIXME Interface violation
19 #include <boost/regex.hpp>
20 #include <boost/tokenizer.hpp>
25 using std::ostringstream;
32 /** Given a string such as
33 * "<glob> <glob> ... *.{abc,def} <glob>",
34 * convert the csh-style brace expresions:
35 * "<glob> <glob> ... *.abc *.def <glob>".
36 * Requires no system support, so should work equally on Unix, Mac, Win32.
38 string const convert_brace_glob(string const & glob)
40 // Matches " *.{abc,def,ghi}", storing "*." as group 1 and
41 // "abc,def,ghi" as group 2.
42 static boost::regex const glob_re(" *([^ {]*)\\{([^ }]+)\\}");
43 // Matches "abc" and "abc,", storing "abc" as group 1.
44 static boost::regex const block_re("([^,}]+),?");
48 string::const_iterator it = glob.begin();
49 string::const_iterator const end = glob.end();
51 boost::match_results<string::const_iterator> what;
52 if (!boost::regex_search(it, end, what, glob_re)) {
53 // Ensure that no information is lost.
54 pattern += string(it, end);
58 // Everything from the start of the input to
59 // the start of the match.
60 pattern += string(what[-1].first, what[-1].second);
62 // Given " *.{abc,def}", head == "*." and tail == "abc,def".
63 string const head = string(what[1].first, what[1].second);
64 string const tail = string(what[2].first, what[2].second);
66 // Split the ','-separated chunks of tail so that
67 // $head{$chunk1,$chunk2} becomes "$head$chunk1 $head$chunk2".
68 string const fmt = " " + head + "$1";
69 pattern += boost::regex_merge(tail, block_re, fmt);
71 // Increment the iterator to the end of the match.
72 it += distance(it, what[0].second);
84 FileFilterList::Filter::Filter(docstring const & description,
85 std::string const & globs)
88 typedef boost::tokenizer<boost::char_separator<char> > Tokenizer;
89 boost::char_separator<char> const separator(" ");
91 // Given "<glob> <glob> ... *.{abc,def} <glob>", expand to
92 // "<glob> <glob> ... *.abc *.def <glob>"
93 string const expanded_globs = convert_brace_glob(globs);
95 // Split into individual globs.
96 vector<string> matches;
97 Tokenizer const tokens(expanded_globs, separator);
98 globs_ = vector<string>(tokens.begin(), tokens.end());
102 FileFilterList::FileFilterList(docstring const & qt_style_filter)
105 string const filter = to_utf8(qt_style_filter)
106 + (qt_style_filter.empty() ? string() : ";;")
107 + to_utf8(_("All files (*)"));
109 // Split data such as "TeX documents (*.tex);;LyX Documents (*.lyx)"
110 // into individual filters.
111 static boost::regex const separator_re(";;");
113 string::const_iterator it = filter.begin();
114 string::const_iterator const end = filter.end();
116 boost::match_results<string::const_iterator> what;
118 if (!boost::regex_search(it, end, what, separator_re)) {
119 parse_filter(string(it, end));
123 // Everything from the start of the input to
124 // the start of the match.
125 parse_filter(string(what[-1].first, what[-1].second));
127 // Increment the iterator to the end of the match.
128 it += distance(it, what[0].second);
133 void FileFilterList::parse_filter(string const & filter)
135 // Matches "TeX documents (*.tex)",
136 // storing "TeX documents " as group 1 and "*.tex" as group 2.
137 static boost::regex const filter_re("([^(]*)\\(([^)]+)\\) *$");
139 boost::match_results<string::const_iterator> what;
140 if (!boost::regex_search(filter, what, filter_re)) {
141 // Just a glob, no description.
142 filters_.push_back(Filter(docstring(), trim(filter)));
145 docstring const desc = from_utf8(string(what[1].first, what[1].second));
146 string const globs = string(what[2].first, what[2].second);
147 filters_.push_back(Filter(trim(desc), trim(globs)));
152 docstring const FileFilterList::as_string() const
157 vector<Filter>::const_iterator fit = filters_.begin();
158 vector<Filter>::const_iterator const fend = filters_.end();
159 for (; fit != fend; ++fit) {
160 Filter::glob_iterator const gbegin = fit->begin();
161 Filter::glob_iterator const gend = fit->end();
168 bool const has_description = !fit->description().empty();
170 ss << to_utf8(fit->description()) << " (";
172 for (Filter::glob_iterator git = gbegin; git != gend; ++git) {
182 return from_utf8(ss.str());
185 } // namespace support