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