3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
15 #include "support/lassert.h"
16 #include "support/lstrings.h"
22 using namespace lyx::support;
26 static int computeHash(docstring const & name,
27 docstring const & email)
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]);
38 Author::Author(docstring const & name, docstring const & email)
39 : name_(name), email_(email), used_(true)
41 buffer_id_ = computeHash(name_, email_);
45 bool operator==(Author const & l, Author const & r)
47 return l.name() == r.name() && l.email() == r.email();
51 ostream & operator<<(ostream & os, Author const & a)
54 os << a.buffer_id_ << " \"" << to_utf8(a.name_)
55 << "\" " << to_utf8(a.email_);
60 istream & operator>>(istream & is, Author & a)
66 a.name_ = from_utf8(trim(token(s, '\"', 1)));
67 a.email_ = from_utf8(trim(token(s, '\"', 2)));
72 bool author_smaller(Author const & lhs, Author const & rhs) {
73 return lhs.bufferId() < rhs.bufferId();
77 AuthorList::AuthorList()
83 int AuthorList::record(Author const & a)
85 // If we record an author which equals the current
86 // author, we copy the buffer_id, so that it will
87 // keep the same id in the file.
88 if (authors_.size() > 0 && a == authors_[0])
89 authors_[0].setBufferId(a.bufferId());
91 Authors::const_iterator it(authors_.begin());
92 Authors::const_iterator itend(authors_.end());
93 for (int i = 0; it != itend; ++it, ++i) {
97 authors_.push_back(a);
102 void AuthorList::record(int id, Author const & a)
104 LASSERT(unsigned(id) < authors_.size(), /**/);
110 void AuthorList::recordCurrentAuthor(Author const & a)
112 // current author has id 0
117 Author const & AuthorList::get(int id) const
119 LASSERT(id < (int)authors_.size() , /**/);
124 AuthorList::Authors::const_iterator AuthorList::begin() const
126 return authors_.begin();
130 AuthorList::Authors::const_iterator AuthorList::end() const
132 return authors_.end();
136 void AuthorList::sort() {
137 std::sort(authors_.begin(), authors_.end(), author_smaller);
141 ostream & operator<<(ostream & os, AuthorList const & a) {
142 // Copy the authorlist, because we don't want to sort the original
143 AuthorList sorted = a;
146 AuthorList::Authors::const_iterator a_it = sorted.begin();
147 AuthorList::Authors::const_iterator a_end = sorted.end();
149 for (a_it = sorted.begin(); a_it != a_end; ++a_it) {
151 os << "\\author " << *a_it << "\n";