]> git.tdb.fi Git - poefilter.git/blob - source/filter.cpp
Initial version from when I last played
[poefilter.git] / source / filter.cpp
1 #include <algorithm>
2 #include <msp/core/maputils.h>
3 #include <msp/io/print.h>
4 #include "category.h"
5 #include "filter.h"
6 #include "poefilter.h"
7
8 using namespace std;
9 using namespace Msp;
10
11 FilterStatement::FilterStatement():
12         show(true)
13 { }
14
15 void FilterStatement::set_show(bool s)
16 {
17         show = s;
18 }
19
20 void FilterStatement::add_line(const string &l)
21 {
22         lines.push_back(l);
23 }
24
25 void FilterStatement::add_line(list<FilterStatement> &st, const string &line)
26 {
27         for(list<FilterStatement>::iterator i=st.begin(); i!=st.end(); ++i)
28                 i->add_line(line);
29 }
30
31 void FilterStatement::write(IO::Base &out) const
32 {
33         if(show)
34                 out.write("Show\n");
35         else
36                 out.write("Hide\n");
37
38         for(list<string>::const_iterator i=lines.begin(); i!=lines.end(); ++i)
39                 IO::print(out, "\t%s\n", *i);
40 }
41
42
43 Filter::Filter():
44         abstract(false)
45 { }
46
47 void Filter::write(IO::Base &out, const Theme &theme) const
48 {
49         for(list<const Category *>::const_iterator i=categories.begin(); i!=categories.end(); ++i)
50         {
51                 list<FilterStatement> st;
52                 //IO::print(out, "# %s\n", i->first);
53                 (*i)->create_statements(st, theme);
54                 for(list<FilterStatement>::const_iterator j=st.begin(); j!=st.end(); ++j)
55                         j->write(out);
56         }
57
58         out.write("# unmatched\nHide\n");
59 }
60
61
62 Filter::Loader::Loader(Filter &f, const PoeFilter &p):
63         DataFile::ObjectLoader<Filter>(f),
64         poe(p)
65 {
66         add("abstract", &Filter::abstract);
67         add("hide", &Loader::hide);
68         add("include", &Loader::include);
69         add("show", &Loader::show);
70 }
71
72 string Filter::Loader::glob_to_re(const string &glob)
73 {
74         string result = "^";
75         for(string::const_iterator i=glob.begin(); i!=glob.end(); ++i)
76         {
77                 if(*i=='*')
78                         result += '.';
79                 else if(!isalnum(*i))
80                         result += '\\';
81                 result += *i;
82         }
83         result += '$';
84
85         return result;
86 }
87
88 bool Filter::Loader::category_order(const Category *c1, const Category *c2)
89 {
90         return c1->get_order()<c2->get_order();
91 }
92
93 void Filter::Loader::hide(const string &name)
94 {
95         list<const Category *> categs;
96         if(name.find('*')!=string::npos)
97         {
98                 poe.find_categories(glob_to_re(name), categs);
99
100                 if(categs.empty())
101                         throw key_error(name);
102         }
103         else
104                 categs.push_back(&poe.get_category(name));
105
106         for(list<const Category *>::const_iterator i=categs.begin(); i!=categs.end(); ++i)
107         {
108                 list<const Category *>::iterator j = find(obj.categories.begin(), obj.categories.end(), *i);
109                 if(j!=obj.categories.end())
110                         obj.categories.erase(j);
111         }
112 }
113
114 void Filter::Loader::include(const string &name)
115 {
116         const Filter &base = poe.get_filter(name);
117         obj.categories.insert(obj.categories.end(), base.categories.begin(), base.categories.end());
118 }
119
120 void Filter::Loader::show(const string &name)
121 {
122         list<const Category *> categs;
123         if(name.find('*')!=string::npos)
124         {
125                 poe.find_categories(glob_to_re(name), categs);
126
127                 if(categs.empty())
128                         throw key_error(name);
129
130                 categs.sort(&category_order);
131         }
132         else
133                 categs.push_back(&poe.get_category(name));
134
135         for(list<const Category *>::const_iterator i=categs.begin(); i!=categs.end(); ++i)
136                 if(find(obj.categories.begin(), obj.categories.end(), *i)==obj.categories.end())
137                         obj.categories.push_back(*i);
138 }