2 * \file filefilterlist.C
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/globbing.h"
15 #include "support/lstrings.h"
17 // FIXME Interface violation
20 #include <boost/regex.hpp>
22 #include </usr/include/glob.h>
27 using std::ostringstream;
35 FileFilterList::FileFilterList(string const & qt_style_filter)
37 string const filter = qt_style_filter
38 + (qt_style_filter.empty() ? string() : ";;")
41 // Split data such as "TeX documents (*.tex);;LyX Documents (*.lyx)"
42 // into individual filters.
43 static boost::regex const separator_re(";;");
45 string::const_iterator it = filter.begin();
46 string::const_iterator const end = filter.end();
48 boost::match_results<string::const_iterator> what;
50 if (!boost::regex_search(it, end, what, separator_re)) {
51 parse_filter(string(it, end));
55 // Everything from the start of the input to
56 // the start of the match.
57 parse_filter(string(what[-1].first, what[-1].second));
59 // Increment the iterator to the end of the match.
60 it += distance(it, what[0].second);
65 void FileFilterList::parse_filter(string const & filter)
67 // Matches "TeX documents (*.tex)",
68 // storing "TeX documents " as group 1 and "*.tex" as group 2.
69 static boost::regex const filter_re("([^(]*)\\(([^)]+)\\) *$");
71 boost::match_results<string::const_iterator> what;
72 if (!boost::regex_search(filter, what, filter_re)) {
73 // Just a glob, no description.
74 filters_.push_back(Filter(string(), trim(filter)));
76 string const desc = string(what[1].first, what[1].second);
77 string const globs = string(what[2].first, what[2].second);
78 filters_.push_back(Filter(trim(desc), trim(globs)));
83 string const FileFilterList::str(bool expand) const
87 vector<Filter>::const_iterator const begin = filters_.begin();
88 vector<Filter>::const_iterator const end = filters_.end();
89 vector<Filter>::const_iterator it = begin;
90 for (; it != end; ++it) {
91 string const globs = expand ?
92 convert_brace_glob(it->globs()) : it->globs();
95 bool const has_description = !it->description().empty();
97 ss << it->description() << " (";
106 } // namespace support