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_);
61 istream & operator>>(istream & is, Author & a)
67 a.name_ = from_utf8(trim(token(s, '\"', 1)));
68 a.email_ = from_utf8(trim(token(s, '\"', 2)));
73 bool author_smaller(Author const & lhs, Author const & rhs)
75 return lhs.bufferId() < rhs.bufferId();
79 AuthorList::AuthorList()
84 int AuthorList::record(Author const & a)
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());
92 Authors::const_iterator it(authors_.begin());
93 Authors::const_iterator itend(authors_.end());
94 for (int i = 0; it != itend; ++it, ++i) {
98 authors_.push_back(a);
103 void AuthorList::record(int id, Author const & a)
105 LBUFERR(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() , return authors_[0]);
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()
138 std::sort(authors_.begin(), authors_.end(), author_smaller);
142 ostream & operator<<(ostream & os, AuthorList const & a)
144 // Copy the authorlist, because we don't want to sort the original
145 AuthorList sorted = a;
148 AuthorList::Authors::const_iterator a_it = sorted.begin();
149 AuthorList::Authors::const_iterator a_end = sorted.end();
151 for (a_it = sorted.begin(); a_it != a_end; ++a_it) {
153 os << "\\author " << *a_it << "\n";