]> git.lyx.org Git - lyx.git/blob - src/Author.cpp
compilation fix for Qt 4.5
[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 istream & operator>>(istream & is, Author & a)
61 {
62         string s;
63         is >> a.buffer_id_;
64         getline(is, s);
65         // FIXME UNICODE
66         a.name_ = from_utf8(trim(token(s, '\"', 1)));
67         a.email_ = from_utf8(trim(token(s, '\"', 2)));
68         return is;
69 }
70
71
72 bool author_smaller(Author const & lhs, Author const & rhs) {
73         return lhs.bufferId() < rhs.bufferId();
74 }
75
76
77 AuthorList::AuthorList()
78         : last_id_(0)
79 {
80 }
81
82
83 int AuthorList::record(Author const & a)
84 {
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());
90
91         Authors::const_iterator it(authors_.begin());
92         Authors::const_iterator itend(authors_.end());
93         for (int i = 0;  it != itend; ++it, ++i) {
94                 if (*it == a)
95                         return i;
96         }
97         authors_.push_back(a);
98         return last_id_++;
99 }
100
101
102 void AuthorList::record(int id, Author const & a)
103 {
104         LASSERT(unsigned(id) < authors_.size(), /**/);
105
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() , /**/);
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         std::sort(authors_.begin(), authors_.end(), author_smaller);
138 }
139
140
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;
144         sorted.sort();
145
146         AuthorList::Authors::const_iterator a_it = sorted.begin();
147         AuthorList::Authors::const_iterator a_end = sorted.end();
148         
149         for (a_it = sorted.begin(); a_it != a_end; ++a_it) {
150                 if (a_it->used())
151                         os << "\\author " << *a_it << "\n";     
152         }
153         return os;
154 }
155
156
157 } // namespace lyx