2003-02-08 19:18:01 +00:00
|
|
|
/**
|
2007-04-26 04:41:58 +00:00
|
|
|
* \file Author.cpp
|
2003-02-08 19:18:01 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
|
|
|
* \author John Levon
|
|
|
|
*
|
2003-08-23 00:17:00 +00:00
|
|
|
* Full author contact details are available in file CREDITS.
|
2003-02-08 19:18:01 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2007-04-26 04:41:58 +00:00
|
|
|
#include "Author.h"
|
2003-02-08 19:18:01 +00:00
|
|
|
|
|
|
|
#include "support/lstrings.h"
|
2003-03-02 12:16:00 +00:00
|
|
|
|
2008-04-30 08:26:40 +00:00
|
|
|
#include "support/lassert.h"
|
2003-09-09 17:25:35 +00:00
|
|
|
|
2009-07-23 20:08:05 +00:00
|
|
|
#include <algorithm>
|
2007-11-06 21:45:24 +00:00
|
|
|
#include <istream>
|
2003-09-09 22:13:45 +00:00
|
|
|
|
2007-12-12 10:16:00 +00:00
|
|
|
using namespace std;
|
2007-12-12 18:57:56 +00:00
|
|
|
using namespace lyx::support;
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
namespace lyx {
|
|
|
|
|
2003-06-30 23:56:22 +00:00
|
|
|
|
2003-02-08 19:18:01 +00:00
|
|
|
bool operator==(Author const & l, Author const & r)
|
|
|
|
{
|
|
|
|
return l.name() == r.name() && l.email() == r.email();
|
|
|
|
}
|
2003-03-02 12:16:00 +00:00
|
|
|
|
2003-02-08 19:18:01 +00:00
|
|
|
|
2007-12-12 19:28:07 +00:00
|
|
|
ostream & operator<<(ostream & os, Author const & a)
|
2003-02-08 19:18:01 +00:00
|
|
|
{
|
2006-12-21 13:58:28 +00:00
|
|
|
// FIXME UNICODE
|
2009-07-23 20:08:05 +00:00
|
|
|
os << a.buffer_id() << " \"" << to_utf8(a.name())
|
|
|
|
<< "\" " << to_utf8(a.email());
|
|
|
|
|
2003-02-08 19:18:01 +00:00
|
|
|
return os;
|
|
|
|
}
|
2003-03-02 12:16:00 +00:00
|
|
|
|
2007-12-12 19:28:07 +00:00
|
|
|
istream & operator>>(istream & is, Author & a)
|
2003-02-08 19:18:01 +00:00
|
|
|
{
|
|
|
|
string s;
|
2009-07-23 20:08:05 +00:00
|
|
|
is >> a.buffer_id_;
|
2003-02-08 19:18:01 +00:00
|
|
|
getline(is, s);
|
2006-12-21 13:58:28 +00:00
|
|
|
// FIXME UNICODE
|
|
|
|
a.name_ = from_utf8(trim(token(s, '\"', 1)));
|
|
|
|
a.email_ = from_utf8(trim(token(s, '\"', 2)));
|
2003-02-08 19:18:01 +00:00
|
|
|
return is;
|
|
|
|
}
|
2003-03-02 12:16:00 +00:00
|
|
|
|
|
|
|
|
2009-07-23 20:08:05 +00:00
|
|
|
bool author_smaller(Author const & lhs, Author const & rhs) {
|
|
|
|
return lhs.buffer_id() < rhs.buffer_id();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-03-12 23:25:59 +00:00
|
|
|
AuthorList::AuthorList()
|
|
|
|
: last_id_(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-02-08 19:18:01 +00:00
|
|
|
int AuthorList::record(Author const & a)
|
|
|
|
{
|
|
|
|
Authors::const_iterator it(authors_.begin());
|
|
|
|
Authors::const_iterator itend(authors_.end());
|
2003-03-02 12:16:00 +00:00
|
|
|
|
2009-07-23 20:08:05 +00:00
|
|
|
for (int i = 0; it != itend; ++it, ++i) {
|
|
|
|
if (*it == a) {
|
|
|
|
if (it->buffer_id() == 0)
|
|
|
|
// The current author is internally represented as
|
|
|
|
// author 0, but it appears he has already an id.
|
|
|
|
it->setBufferId(a.buffer_id());
|
|
|
|
return i;
|
|
|
|
}
|
2003-03-02 12:16:00 +00:00
|
|
|
}
|
2009-07-23 20:08:05 +00:00
|
|
|
authors_.push_back(a);
|
|
|
|
return last_id_++;
|
2003-02-08 19:18:01 +00:00
|
|
|
}
|
2003-03-02 12:16:00 +00:00
|
|
|
|
2003-02-08 19:18:01 +00:00
|
|
|
|
|
|
|
void AuthorList::record(int id, Author const & a)
|
|
|
|
{
|
2008-04-10 21:49:34 +00:00
|
|
|
LASSERT(unsigned(id) < authors_.size(), /**/);
|
2003-03-02 12:16:00 +00:00
|
|
|
|
2003-02-08 19:18:01 +00:00
|
|
|
authors_[id] = a;
|
|
|
|
}
|
|
|
|
|
2003-03-02 12:16:00 +00:00
|
|
|
|
2007-03-04 09:37:32 +00:00
|
|
|
Author const & AuthorList::get(int id) const
|
2003-02-08 19:18:01 +00:00
|
|
|
{
|
2009-07-23 20:08:05 +00:00
|
|
|
LASSERT(id < (int)authors_.size() , /**/);
|
|
|
|
return authors_[id];
|
2003-02-08 19:18:01 +00:00
|
|
|
}
|
|
|
|
|
2003-03-02 12:16:00 +00:00
|
|
|
|
2003-02-08 19:18:01 +00:00
|
|
|
AuthorList::Authors::const_iterator AuthorList::begin() const
|
|
|
|
{
|
|
|
|
return authors_.begin();
|
|
|
|
}
|
2003-03-02 12:16:00 +00:00
|
|
|
|
2003-02-08 19:18:01 +00:00
|
|
|
|
|
|
|
AuthorList::Authors::const_iterator AuthorList::end() const
|
|
|
|
{
|
|
|
|
return authors_.end();
|
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
2009-07-23 20:08:05 +00:00
|
|
|
void AuthorList::sort() {
|
|
|
|
std::sort(authors_.begin(), authors_.end(), author_smaller);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ostream & operator<<(ostream & os, AuthorList const & a) {
|
|
|
|
// Copy the authorlist, because we don't want to sort the original
|
|
|
|
AuthorList sorted = a;
|
|
|
|
sorted.sort();
|
|
|
|
|
|
|
|
AuthorList::Authors::const_iterator a_it = sorted.begin();
|
|
|
|
AuthorList::Authors::const_iterator a_end = sorted.end();
|
|
|
|
|
|
|
|
// Find the buffer id for the current author (internal id 0),
|
|
|
|
// if he doesn't have a buffer_id yet.
|
|
|
|
if (sorted.get(0).buffer_id() == 0) {
|
|
|
|
unsigned int cur_id = 1;
|
|
|
|
for (; a_it != a_end; ++a_it) {
|
|
|
|
if (a_it->buffer_id() == cur_id)
|
|
|
|
++cur_id;
|
|
|
|
else if (a_it->buffer_id() > cur_id) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Set the id in both the original authorlist,
|
|
|
|
// as in the copy.
|
|
|
|
a.get(0).setBufferId(cur_id);
|
|
|
|
sorted.get(0).setBufferId(cur_id);
|
|
|
|
sorted.sort();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (a_it = sorted.begin(); a_it != a_end; ++a_it) {
|
|
|
|
if (a_it->used())
|
|
|
|
os << "\\author " << *a_it << "\n";
|
|
|
|
}
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
} // namespace lyx
|