]> git.lyx.org Git - lyx.git/blob - src/Author.cpp
Improve how add_to_preamble and insert_to_preamble work, and audit the
[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/lstrings.h"
16
17 #include "support/lassert.h"
18
19 #include <algorithm>
20 #include <istream>
21
22 using namespace std;
23 using namespace lyx::support;
24
25 namespace lyx {
26
27
28 bool operator==(Author const & l, Author const & r)
29 {
30         return l.name() == r.name() && l.email() == r.email();
31 }
32
33
34 ostream & operator<<(ostream & os, Author const & a)
35 {
36         // FIXME UNICODE
37         os << a.buffer_id() << " \"" << to_utf8(a.name())
38                         << "\" " << to_utf8(a.email());
39                 
40         return os;
41 }
42
43 istream & operator>>(istream & is, Author & a)
44 {
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;
52 }
53
54
55 bool author_smaller(Author const & lhs, Author const & rhs) {
56         return lhs.buffer_id() < rhs.buffer_id();
57 }
58
59
60 AuthorList::AuthorList()
61         : last_id_(0)
62 {
63 }
64
65
66 int AuthorList::record(Author const & a)
67 {
68         Authors::const_iterator it(authors_.begin());
69         Authors::const_iterator itend(authors_.end());
70
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;
78                 }
79         }
80         authors_.push_back(a);
81         return last_id_++;
82 }
83
84
85 void AuthorList::record(int id, Author const & a)
86 {
87         LASSERT(unsigned(id) < authors_.size(), /**/);
88
89         authors_[id] = a;
90 }
91
92
93 void AuthorList::recordCurrentAuthor(Author const & a)
94 {
95         // current author has id 0
96         record(0, a);
97 }
98
99
100 Author const & AuthorList::get(int id) const
101 {
102         LASSERT(id < (int)authors_.size() , /**/);
103         return authors_[id];
104 }
105
106
107 AuthorList::Authors::const_iterator AuthorList::begin() const
108 {
109         return authors_.begin();
110 }
111
112
113 AuthorList::Authors::const_iterator AuthorList::end() const
114 {
115         return authors_.end();
116 }
117
118
119 void AuthorList::sort() {
120         std::sort(authors_.begin(), authors_.end(), author_smaller);
121 }
122
123
124 ostream & operator<<(ostream & os, AuthorList const & a) {
125         // Copy the authorlist, because we don't want to sort the original
126         AuthorList sorted = a;
127         sorted.sort();
128
129         AuthorList::Authors::const_iterator a_it = sorted.begin();
130         AuthorList::Authors::const_iterator a_end = sorted.end();
131
132         // Find the buffer id for the current author (internal id 0),
133         // if he doesn't have a buffer_id yet.
134         if (sorted.get(0).buffer_id() == 0) {
135                 unsigned int cur_id = 1;
136                 for (; a_it != a_end; ++a_it) {
137                         if (a_it->buffer_id() == cur_id)
138                                 ++cur_id;
139                         else if (a_it->buffer_id() > cur_id) {
140                                 break;
141                         }
142                 }
143                 // Set the id in both the original authorlist, 
144                 // as in the copy.
145                 a.get(0).setBufferId(cur_id);
146                 sorted.get(0).setBufferId(cur_id);
147                 sorted.sort();
148         }
149         
150         for (a_it = sorted.begin(); a_it != a_end; ++a_it) {
151                 if (a_it->used())
152                         os << "\\author " << *a_it << "\n";     
153         }
154         return os;
155 }
156
157
158 } // namespace lyx