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