]> git.tdb.fi Git - poefilter.git/blob - source/filter.cpp
Associate categories and appearances at filter level
[poefilter.git] / source / filter.cpp
1 #include <msp/core/algorithm.h>
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
48 {
49         for(list<Block>::const_iterator i=blocks.begin(); i!=blocks.end(); ++i)
50         {
51                 list<FilterStatement> st;
52                 st.push_back(FilterStatement());
53                 i->appearance.add_lines(st.back());
54                 i->category->create_statements(st);
55
56                 IO::print(out, "# %s\n", i->category->get_name());
57                 for(list<FilterStatement>::const_iterator j=st.begin(); j!=st.end(); ++j)
58                         j->write(out);
59         }
60
61         out.write("# unmatched\nHide\n");
62 }
63
64
65 Filter::Loader::Loader(Filter &f, const PoeFilter &p):
66         DataFile::ObjectLoader<Filter>(f),
67         poe(p)
68 {
69         add("abstract", &Filter::abstract);
70         add("hide", &Loader::hide);
71         add("include", &Loader::include);
72         add("show", &Loader::show);
73 }
74
75 string Filter::Loader::glob_to_re(const string &glob)
76 {
77         string result = "^";
78         for(string::const_iterator i=glob.begin(); i!=glob.end(); ++i)
79         {
80                 if(*i=='*')
81                         result += '.';
82                 else if(!isalnum(*i))
83                         result += '\\';
84                 result += *i;
85         }
86         result += '$';
87
88         return result;
89 }
90
91 bool Filter::Loader::category_order(const Category *c1, const Category *c2)
92 {
93         return c1->get_order()<c2->get_order();
94 }
95
96 void Filter::Loader::hide(const string &name)
97 {
98         list<const Category *> categs;
99         if(name.find('*')!=string::npos)
100         {
101                 poe.find_categories(glob_to_re(name), categs);
102
103                 if(categs.empty())
104                         throw key_error(name);
105         }
106         else
107                 categs.push_back(&poe.get_category(name));
108
109         for(list<const Category *>::const_iterator i=categs.begin(); i!=categs.end(); ++i)
110                 for(list<Block>::iterator j=obj.blocks.begin(); j!=obj.blocks.end(); ++j)
111                         if(j->category==*i)
112                         {
113                                 obj.blocks.erase(j);
114                                 break;
115                         }
116 }
117
118 void Filter::Loader::include(const string &name)
119 {
120         const Filter &base = poe.get_filter(name);
121         obj.blocks.insert(obj.blocks.end(), base.blocks.begin(), base.blocks.end());
122 }
123
124 void Filter::Loader::show(const string &name)
125 {
126         list<const Category *> categs;
127         if(name.find('*')!=string::npos)
128         {
129                 poe.find_categories(glob_to_re(name), categs);
130
131                 if(categs.empty())
132                         throw key_error(name);
133
134                 categs.sort(&category_order);
135         }
136         else
137                 categs.push_back(&poe.get_category(name));
138
139         for(list<const Category *>::const_iterator i=categs.begin(); i!=categs.end(); ++i)
140         {
141                 bool found = false;
142                 for(list<Block>::const_iterator j=obj.blocks.begin(); (!found && j!=obj.blocks.end()); ++j)
143                         found = (j->category==*i);
144
145                 if(!found)
146                 {
147                         Block blk;
148                         blk.category = *i;
149                         blk.appearance = (*i)->get_appearance();
150                         if(const Appearance *app = poe.get_theme().find_appearance((*i)->get_name()))
151                                 blk.appearance.merge_from(*app);
152                         obj.blocks.push_back(blk);
153                 }
154         }
155 }