]> git.lyx.org Git - lyx.git/blob - src/BranchList.C
Point fix, earlier forgotten
[lyx.git] / src / BranchList.C
1 /**
2  * \file BranchList.C 
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Martin Vermeer
7  * 
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "BranchList.h"
14 #include "support/LAssert.h"
15
16 #include <functional>
17
18 using std::bind2nd;
19 using std::remove_if;
20 using std::binary_function;
21 using namespace lyx::support;
22
23
24 string const Branch::getBranch() const
25 {
26         return branch_;
27 }
28
29
30 void Branch::setBranch(string const & s)
31 {
32         branch_ = s;
33 }
34
35
36 bool Branch::getSelected() const
37 {
38         return selected_;
39 }
40
41
42 void Branch::setSelected(bool b)
43 {
44         selected_ = b;
45 }
46
47
48 string const Branch::getColor() const
49 {
50         return color_;
51 }
52
53
54 void Branch::setColor(string const & c)
55 {
56         color_ = c;
57 }
58
59
60 void BranchList::clear()
61 {
62         list.clear();
63 }
64
65
66 string BranchList::getColor(string const & s) const
67 {
68         List::const_iterator it = list.begin();
69         List::const_iterator end = list.end();
70         for (; it != end; ++it) {
71                 if (s == it->getBranch()) {
72                         return it->getColor();
73                 }
74         }
75         Assert(false); // Always
76         return string(); // never gets here
77 }
78
79
80
81 void BranchList::setColor(string const & s, string const & val)
82 {
83         List::iterator it = list.begin();
84         List::iterator end = list.end();
85         for (; it != end; ++it) {
86                 if (s == it->getBranch()) {
87                         it->setColor(val);
88                         return;
89                 }
90         }
91         Assert(false);
92 }
93
94
95 void BranchList::setSelected(string const & s, bool val)
96 {
97         List::iterator it = list.begin();
98         List::iterator end = list.end();
99         for (; it != end; ++it) {
100                 if (s.find(it->getBranch(), 0) != string::npos)
101                         it->setSelected(val);
102         }
103 }
104
105
106 void BranchList::add(string const & s)
107 {
108         string::size_type i = 0;
109         while (true) {
110                 string::size_type const j = s.find_first_of(separator(), i);
111                 string name;
112                 if (j == string::npos)
113                         name = s.substr(i);
114                 else
115                         name = s.substr(i, j - i);
116                 // Is this name already in the list?
117                 List::const_iterator it = list.begin();
118                 List::const_iterator end = list.end();
119                 bool already = false;
120                 for (; it != end; ++it) {
121                         if (it->getBranch() == name) {
122                                 already = true;
123                                 break;
124                         }
125                 }
126                 if (!already) {
127                         Branch br;
128                         br.setBranch(name);
129                         br.setSelected(false);
130                         br.setColor("none");
131                         list.push_back(br);
132                 }
133                 if (j == string::npos)
134                         break;
135                 i = j + 1; 
136         }
137 }
138
139
140 namespace {
141
142 struct match : public binary_function<Branch, string, bool> {
143         bool operator()(Branch const & br, string const & s) const {
144         return (br.getBranch() == s);
145         }                                                                       
146 };
147
148 } // namespace anon.
149
150
151 void BranchList::remove(string const & s)
152 {
153         list.remove_if(bind2nd(match(), s));
154 }
155
156
157 bool BranchList::selected(string const & s) const
158 {
159         List::const_iterator it = list.begin();
160         List::const_iterator end = list.end();
161         for (; it != end; ++it) {
162                 if (s == it->getBranch()) 
163                         return it->getSelected();
164         }
165         return false;
166 }
167
168
169 string BranchList::allBranches() const
170 {
171         List::const_iterator it = list.begin();
172         List::const_iterator end = list.end();
173         string ret;
174         for (; it != end; ++it) {
175                 ret += it->getBranch() + separator();
176         }
177         // remove final '|':
178         string::size_type i = ret.find_last_of(separator());
179         if (i != string::npos)
180                 ret.erase(i);
181         return ret;
182 }
183
184
185 string BranchList::allSelected() const
186 {       
187         List::const_iterator it = list.begin();
188         List::const_iterator end = list.end();
189         string ret;
190         for (; it != end; ++it) {
191                 if (it->getSelected()) 
192                         ret += it->getBranch() + separator();
193         }
194         // remove final '|':
195         string::size_type i = ret.find_last_of(separator());
196         if (i != string::npos)
197                 ret.erase(i);
198         return ret;
199 }
200
201
202 string const BranchList::separator() const
203 {
204         return separator_;
205 }