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