tufte layout files:
[lyx.git] / src / Author.cpp
blob0ad9b898ead7c64fe6112bebd2048a0612bf37f5
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.
6 * \author John Levon
8 * Full author contact details are available in file CREDITS.
9 */
11 #include <config.h>
13 #include "Author.h"
15 #include "support/lstrings.h"
17 #include "support/lassert.h"
19 #include <algorithm>
20 #include <istream>
22 using namespace std;
23 using namespace lyx::support;
25 namespace lyx {
28 bool operator==(Author const & l, Author const & r)
30 return l.name() == r.name() && l.email() == r.email();
34 ostream & operator<<(ostream & os, Author const & a)
36 // FIXME UNICODE
37 os << a.buffer_id() << " \"" << to_utf8(a.name())
38 << "\" " << to_utf8(a.email());
40 return os;
43 istream & operator>>(istream & is, Author & a)
45 string s;
46 is >> a.buffer_id_;
47 getline(is, s);
48 // FIXME UNICODE
49 a.name_ = from_utf8(trim(token(s, '\"', 1)));
50 a.email_ = from_utf8(trim(token(s, '\"', 2)));
51 return is;
55 bool author_smaller(Author const & lhs, Author const & rhs) {
56 return lhs.buffer_id() < rhs.buffer_id();
60 AuthorList::AuthorList()
61 : last_id_(0)
66 int AuthorList::record(Author const & a)
68 Authors::const_iterator it(authors_.begin());
69 Authors::const_iterator itend(authors_.end());
71 for (int i = 0; it != itend; ++it, ++i) {
72 if (*it == a) {
73 if (it->buffer_id() == 0)
74 // The current author is internally represented as
75 // author 0, but it appears he has already an id.
76 it->setBufferId(a.buffer_id());
77 return i;
80 authors_.push_back(a);
81 return last_id_++;
85 void AuthorList::record(int id, Author const & a)
87 LASSERT(unsigned(id) < authors_.size(), /**/);
89 authors_[id] = a;
93 Author const & AuthorList::get(int id) const
95 LASSERT(id < (int)authors_.size() , /**/);
96 return authors_[id];
100 AuthorList::Authors::const_iterator AuthorList::begin() const
102 return authors_.begin();
106 AuthorList::Authors::const_iterator AuthorList::end() const
108 return authors_.end();
112 void AuthorList::sort() {
113 std::sort(authors_.begin(), authors_.end(), author_smaller);
117 ostream & operator<<(ostream & os, AuthorList const & a) {
118 // Copy the authorlist, because we don't want to sort the original
119 AuthorList sorted = a;
120 sorted.sort();
122 AuthorList::Authors::const_iterator a_it = sorted.begin();
123 AuthorList::Authors::const_iterator a_end = sorted.end();
125 // Find the buffer id for the current author (internal id 0),
126 // if he doesn't have a buffer_id yet.
127 if (sorted.get(0).buffer_id() == 0) {
128 unsigned int cur_id = 1;
129 for (; a_it != a_end; ++a_it) {
130 if (a_it->buffer_id() == cur_id)
131 ++cur_id;
132 else if (a_it->buffer_id() > cur_id) {
133 break;
136 // Set the id in both the original authorlist,
137 // as in the copy.
138 a.get(0).setBufferId(cur_id);
139 sorted.get(0).setBufferId(cur_id);
140 sorted.sort();
143 for (a_it = sorted.begin(); a_it != a_end; ++a_it) {
144 if (a_it->used())
145 os << "\\author " << *a_it << "\n";
147 return os;
151 } // namespace lyx