]> git.lyx.org Git - lyx.git/blob - src/Author.cpp
Update my email and status.
[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         LASSERT(unsigned(id) < authors_.size(), /**/);
106
107         authors_[id] = a;
108 }
109
110
111 void AuthorList::recordCurrentAuthor(Author const & a)
112 {
113         // current author has id 0
114         record(0, a);
115 }
116
117
118 Author const & AuthorList::get(int id) const
119 {
120         LASSERT(id < (int)authors_.size() , /**/);
121         return authors_[id];
122 }
123
124
125 AuthorList::Authors::const_iterator AuthorList::begin() const
126 {
127         return authors_.begin();
128 }
129
130
131 AuthorList::Authors::const_iterator AuthorList::end() const
132 {
133         return authors_.end();
134 }
135
136
137 void AuthorList::sort()
138 {
139         std::sort(authors_.begin(), authors_.end(), author_smaller);
140 }
141
142
143 ostream & operator<<(ostream & os, AuthorList const & a)
144 {
145         // Copy the authorlist, because we don't want to sort the original
146         AuthorList sorted = a;
147         sorted.sort();
148
149         AuthorList::Authors::const_iterator a_it = sorted.begin();
150         AuthorList::Authors::const_iterator a_end = sorted.end();
151         
152         for (a_it = sorted.begin(); a_it != a_end; ++a_it) {
153                 if (a_it->used())
154                         os << "\\author " << *a_it << "\n";     
155         }
156         return os;
157 }
158
159
160 } // namespace lyx