]> git.lyx.org Git - lyx.git/blob - src/Author.cpp
InsetCommand ctor: Pass 'Buffer *'
[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 Author const & AuthorList::get(int id) const
94 {
95         LASSERT(id < (int)authors_.size() , /**/);
96         return authors_[id];
97 }
98
99
100 AuthorList::Authors::const_iterator AuthorList::begin() const
101 {
102         return authors_.begin();
103 }
104
105
106 AuthorList::Authors::const_iterator AuthorList::end() const
107 {
108         return authors_.end();
109 }
110
111
112 void AuthorList::sort() {
113         std::sort(authors_.begin(), authors_.end(), author_smaller);
114 }
115
116
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();
121
122         AuthorList::Authors::const_iterator a_it = sorted.begin();
123         AuthorList::Authors::const_iterator a_end = sorted.end();
124
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;
134                         }
135                 }
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();
141         }
142         
143         for (a_it = sorted.begin(); a_it != a_end; ++a_it) {
144                 if (a_it->used())
145                         os << "\\author " << *a_it << "\n";     
146         }
147         return os;
148 }
149
150
151 } // namespace lyx