]> git.lyx.org Git - lyx.git/blob - src/BranchList.C
dc09595841052b5a563f02b0df1b9842747e960b
[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
15 using std::string;
16
17 namespace {
18
19 class BranchNamesEqual : public std::unary_function<Branch, bool> {
20 public:
21         BranchNamesEqual(string const & name)
22                 : name_(name) {}
23         bool operator()(Branch const & branch) const
24         {
25                 return branch.getBranch() == name_;
26         }
27 private:
28         string name_;
29 };
30
31 } // namespace anon
32
33
34 string const & Branch::getBranch() const
35 {
36         return branch_;
37 }
38
39
40 void Branch::setBranch(string const & s)
41 {
42         branch_ = s;
43 }
44
45
46 bool Branch::getSelected() const
47 {
48         return selected_;
49 }
50
51
52 bool Branch::setSelected(bool b)
53 {
54         if (b == selected_)
55                 return false;
56         selected_ = b;
57         return true;
58 }
59
60
61 string const & Branch::getColor() const
62 {
63         return color_;
64 }
65
66
67 void Branch::setColor(string const & c)
68 {
69         color_ = c;
70 }
71
72
73 Branch * BranchList::find(std::string const & name)
74 {
75         List::iterator it =
76                 std::find_if(list.begin(), list.end(), BranchNamesEqual(name));
77         return it == list.end() ? 0 : &*it;
78 }
79
80
81 Branch const * BranchList::find(std::string const & name) const
82 {
83         List::const_iterator it =
84                 std::find_if(list.begin(), list.end(), BranchNamesEqual(name));
85         return it == list.end() ? 0 : &*it;
86 }
87
88
89 bool BranchList::add(string const & s)
90 {
91         bool added = false;
92         string::size_type i = 0;
93         while (true) {
94                 string::size_type const j = s.find_first_of(separator_, i);
95                 string name;
96                 if (j == string::npos)
97                         name = s.substr(i);
98                 else
99                         name = s.substr(i, j - i);
100                 // Is this name already in the list?
101                 bool const already =
102                         std::find_if(list.begin(), list.end(),
103                                      BranchNamesEqual(name)) != list.end();
104                 if (!already) {
105                         added = true;
106                         Branch br;
107                         br.setBranch(name);
108                         br.setSelected(false);
109                         br.setColor("none");
110                         list.push_back(br);
111                 }
112                 if (j == string::npos)
113                         break;
114                 i = j + 1;
115         }
116         return added;
117 }
118
119
120 bool BranchList::remove(string const & s)
121 {
122         List::size_type const size = list.size();
123         list.remove_if(BranchNamesEqual(s));
124         return size != list.size();
125 }