]> git.lyx.org Git - lyx.git/blob - src/support/filefilterlist.C
Give FileFilterList its own .[Ch] files.
[lyx.git] / src / support / filefilterlist.C
1 /**
2  * \file filefilterlist.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 "support/filefilterlist.h"
14 #include "support/globbing.h"
15 #include "support/lstrings.h"
16
17 // FIXME Interface violation
18 #include "gettext.h"
19
20 #include <boost/regex.hpp>
21
22 #include </usr/include/glob.h>
23
24 #include <sstream>
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 FileFilterList::FileFilterList(string const & qt_style_filter)
36 {
37         string const filter = qt_style_filter
38                 + (qt_style_filter.empty() ? string() : ";;")
39                 + _("All files (*)");
40
41         // Split data such as "TeX documents (*.tex);;LyX Documents (*.lyx)"
42         // into individual filters.
43         static boost::regex const separator_re(";;");
44
45         string::const_iterator it = filter.begin();
46         string::const_iterator const end = filter.end();
47         while (true) {
48                 boost::match_results<string::const_iterator> what;
49
50                 if (!boost::regex_search(it, end, what, separator_re)) {
51                         parse_filter(string(it, end));
52                         break;
53                 }
54
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));
58
59                 // Increment the iterator to the end of the match.
60                 it += distance(it, what[0].second);
61         }
62 }
63
64
65 void FileFilterList::parse_filter(string const & filter)
66 {
67         // Matches "TeX documents (*.tex)",
68         // storing "TeX documents " as group 1 and "*.tex" as group 2.
69         static boost::regex const filter_re("([^(]*)\\(([^)]+)\\) *$");
70
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)));
75         } else {
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)));
79         }
80 }
81
82
83 string const FileFilterList::str(bool expand) const
84 {
85         ostringstream ss;
86
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();
93                 if (it != begin)
94                         ss << ";;";
95                 bool const has_description = !it->description().empty();
96                 if (has_description)
97                         ss << it->description() << " (";
98                 ss << globs;
99                 if (has_description)
100                         ss << ')';
101         }
102
103         return ss.str();
104 }
105
106 } // namespace support
107 } // namespace lyx