X-Git-Url: https://git.lyx.org/gitweb/?a=blobdiff_plain;f=src%2FAuthor.cpp;h=0ad9b898ead7c64fe6112bebd2048a0612bf37f5;hb=5678dc566f8873b489c66ffa5772a74c387a6396;hp=2a74fe17d190c9107e0367dad6e4083fcea193b8;hpb=f630be890494c849981e4fb52ea4740506e92bed;p=lyx.git diff --git a/src/Author.cpp b/src/Author.cpp index 2a74fe17d1..0ad9b898ea 100644 --- a/src/Author.cpp +++ b/src/Author.cpp @@ -14,18 +14,16 @@ #include "support/lstrings.h" -#include +#include "support/lassert.h" -#include "support/std_istream.h" +#include +#include +using namespace std; +using namespace lyx::support; namespace lyx { -using support::token; -using support::trim; - -using std::string; - bool operator==(Author const & l, Author const & r) { @@ -33,16 +31,19 @@ bool operator==(Author const & l, Author const & r) } -std::ostream & operator<<(std::ostream & os, Author const & a) +ostream & operator<<(ostream & os, Author const & a) { // FIXME UNICODE - os << "\"" << to_utf8(a.name()) << "\" " << to_utf8(a.email()); + os << a.buffer_id() << " \"" << to_utf8(a.name()) + << "\" " << to_utf8(a.email()); + return os; } -std::istream & operator>>(std::istream & is, Author & a) +istream & operator>>(istream & is, Author & a) { string s; + is >> a.buffer_id_; getline(is, s); // FIXME UNICODE a.name_ = from_utf8(trim(token(s, '\"', 1))); @@ -51,6 +52,11 @@ std::istream & operator>>(std::istream & is, Author & a) } +bool author_smaller(Author const & lhs, Author const & rhs) { + return lhs.buffer_id() < rhs.buffer_id(); +} + + AuthorList::AuthorList() : last_id_(0) { @@ -62,19 +68,23 @@ int AuthorList::record(Author const & a) Authors::const_iterator it(authors_.begin()); Authors::const_iterator itend(authors_.end()); - for (; it != itend; ++it) { - if (it->second == a) - return it->first; + 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; + } } - - authors_[last_id_++] = a; - return last_id_ - 1; + authors_.push_back(a); + return last_id_++; } void AuthorList::record(int id, Author const & a) { - BOOST_ASSERT(unsigned(id) < authors_.size()); + LASSERT(unsigned(id) < authors_.size(), /**/); authors_[id] = a; } @@ -82,9 +92,8 @@ void AuthorList::record(int id, Author const & a) Author const & AuthorList::get(int id) const { - Authors::const_iterator it(authors_.find(id)); - BOOST_ASSERT(it != authors_.end()); - return it->second; + LASSERT(id < (int)authors_.size() , /**/); + return authors_[id]; } @@ -100,4 +109,43 @@ AuthorList::Authors::const_iterator AuthorList::end() const } +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; +} + + } // namespace lyx