]> git.lyx.org Git - lyx.git/blob - src/Author.cpp
GuiBox.cpp: fix logic
[lyx.git] / src / Author.cpp
1 /**
2  * \file Author.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "Author.h"
14
15 #include "support/lassert.h"
16 #include "support/lstrings.h"
17
18 #include <algorithm>
19 #include <istream>
20
21 using namespace std;
22 using namespace lyx::support;
23
24 namespace lyx {
25
26 static int computeHash(docstring const & name,
27         docstring const & email)
28 {
29         string const full_author_string = to_utf8(name + email);
30         // Bernstein's hash function
31         unsigned int hash = 5381;
32         for (unsigned int i = 0; i < full_author_string.length(); ++i)
33                 hash = ((hash << 5) + hash) + (unsigned int)(full_author_string[i]);
34         return int(hash);
35 }
36
37
38 Author::Author(docstring const & name, docstring const & email)
39         : name_(name), email_(email), used_(true)
40 {
41         buffer_id_ = computeHash(name_, email_);
42 }
43
44
45 bool operator==(Author const & l, Author const & r)
46 {
47         return l.name() == r.name() && l.email() == r.email();
48 }
49
50
51 ostream & operator<<(ostream & os, Author const & a)
52 {
53         // FIXME UNICODE
54         os << a.buffer_id_ << " \"" << to_utf8(a.name_)
55                         << "\" " << to_utf8(a.email_);
56
57         return os;
58 }
59
60
61 istream & operator>>(istream & is, Author & a)
62 {
63         string s;
64         is >> a.buffer_id_;
65         getline(is, s);
66         // FIXME UNICODE
67         a.name_ = from_utf8(trim(token(s, '\"', 1)));
68         a.email_ = from_utf8(trim(token(s, '\"', 2)));
69         return is;
70 }
71
72
73 bool author_smaller(Author const & lhs, Author const & rhs)
74 {
75         return lhs.bufferId() < rhs.bufferId();
76 }
77
78
79 AuthorList::AuthorList()
80         : last_id_(0)
81 {}
82
83
84 int AuthorList::record(Author const & a)
85 {
86         // If we record an author which equals the current
87         // author, we copy the buffer_id, so that it will
88         // keep the same id in the file.
89         if (!authors_.empty() && a == authors_[0])
90                 authors_[0].setBufferId(a.bufferId());
91
92         Authors::const_iterator it(authors_.begin());
93         Authors::const_iterator itend(authors_.end());
94         for (int i = 0;  it != itend; ++it, ++i) {
95                 if (*it == a)
96                         return i;
97         }
98         authors_.push_back(a);
99         return last_id_++;
100 }
101
102
103 void AuthorList::record(int id, Author const & a)
104 {
105         LBUFERR(unsigned(id) < authors_.size());
106         authors_[id] = a;
107 }
108
109
110 void AuthorList::recordCurrentAuthor(Author const & a)
111 {
112         // current author has id 0
113         record(0, a);
114 }
115
116
117 Author const & AuthorList::get(int id) const
118 {
119         LASSERT(id < (int)authors_.size() , return authors_[0]);
120         return authors_[id];
121 }
122
123
124 AuthorList::Authors::const_iterator AuthorList::begin() const
125 {
126         return authors_.begin();
127 }
128
129
130 AuthorList::Authors::const_iterator AuthorList::end() const
131 {
132         return authors_.end();
133 }
134
135
136 void AuthorList::sort()
137 {
138         std::sort(authors_.begin(), authors_.end(), author_smaller);
139 }
140
141
142 ostream & operator<<(ostream & os, AuthorList const & a)
143 {
144         // Copy the authorlist, because we don't want to sort the original
145         AuthorList sorted = a;
146         sorted.sort();
147
148         AuthorList::Authors::const_iterator a_it = sorted.begin();
149         AuthorList::Authors::const_iterator a_end = sorted.end();
150         
151         for (a_it = sorted.begin(); a_it != a_end; ++a_it) {
152                 if (a_it->used())
153                         os << "\\author " << *a_it << "\n";     
154         }
155         return os;
156 }
157
158
159 } // namespace lyx