vmime/src/messageIdSequence.cpp

248 lines
5.2 KiB
C++
Raw Normal View History

//
// VMime library (http://www.vmime.org)
2008-01-04 18:07:40 +00:00
// Copyright (C) 2002-2008 Vincent Richard <vincent@vincent-richard.net>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of
// the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
2005-09-17 10:10:29 +00:00
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// Linking this library statically or dynamically with other modules is making
// a combined work based on this library. Thus, the terms and conditions of
// the GNU General Public License cover the whole combination.
//
#include "vmime/messageIdSequence.hpp"
#include "vmime/exception.hpp"
2005-03-27 18:27:05 +00:00
#include <algorithm>
namespace vmime
{
messageIdSequence::messageIdSequence()
{
}
messageIdSequence::~messageIdSequence()
{
removeAllMessageIds();
}
messageIdSequence::messageIdSequence(const messageIdSequence& midSeq)
: headerFieldValue()
{
copyFrom(midSeq);
}
2005-07-12 22:28:02 +00:00
ref <component> messageIdSequence::clone() const
{
2005-07-12 22:28:02 +00:00
return vmime::create <messageIdSequence>(*this);
}
void messageIdSequence::copyFrom(const component& other)
{
const messageIdSequence& midSeq = dynamic_cast <const messageIdSequence&>(other);
removeAllMessageIds();
for (unsigned int i = 0 ; i < midSeq.m_list.size() ; ++i)
2005-07-12 22:28:02 +00:00
m_list.push_back(midSeq.m_list[i]->clone().dynamicCast <messageId>());
}
messageIdSequence& messageIdSequence::operator=(const messageIdSequence& other)
{
copyFrom(other);
return (*this);
}
2005-07-12 22:28:02 +00:00
const std::vector <ref <const component> > messageIdSequence::getChildComponents() const
{
2005-07-12 22:28:02 +00:00
std::vector <ref <const component> > res;
copy_vector(m_list, res);
return (res);
}
void messageIdSequence::parse(const string& buffer, const string::size_type position,
const string::size_type end, string::size_type* newPosition)
{
removeAllMessageIds();
string::size_type pos = position;
while (pos < end)
{
2005-07-12 22:28:02 +00:00
ref <messageId> parsedMid = messageId::parseNext(buffer, pos, end, &pos);
if (parsedMid != NULL)
m_list.push_back(parsedMid);
}
setParsedBounds(position, end);
if (newPosition)
*newPosition = end;
}
void messageIdSequence::generate(utility::outputStream& os, const string::size_type maxLineLength,
const string::size_type curLinePos, string::size_type* newLinePos) const
{
string::size_type pos = curLinePos;
if (!m_list.empty())
{
2005-07-12 22:28:02 +00:00
for (std::vector <ref <messageId> >::const_iterator it = m_list.begin() ; ; )
{
(*it)->generate(os, maxLineLength - 2, pos, &pos);
if (++it == m_list.end())
break;
os << " ";
pos++;
}
}
if (newLinePos)
*newLinePos = pos;
}
2005-07-12 22:28:02 +00:00
void messageIdSequence::appendMessageId(ref <messageId> mid)
{
m_list.push_back(mid);
}
2005-07-12 22:28:02 +00:00
void messageIdSequence::insertMessageIdBefore(ref <messageId> beforeMid, ref <messageId> mid)
{
2005-07-12 22:28:02 +00:00
const std::vector <ref <messageId> >::iterator it = std::find
(m_list.begin(), m_list.end(), beforeMid);
if (it == m_list.end())
throw exceptions::no_such_message_id();
m_list.insert(it, mid);
}
2005-07-12 22:28:02 +00:00
void messageIdSequence::insertMessageIdBefore(const int pos, ref <messageId> mid)
{
m_list.insert(m_list.begin() + pos, mid);
}
2005-07-12 22:28:02 +00:00
void messageIdSequence::insertMessageIdAfter(ref <messageId> afterMid, ref <messageId> mid)
{
2005-07-12 22:28:02 +00:00
const std::vector <ref <messageId> >::iterator it = std::find
(m_list.begin(), m_list.end(), afterMid);
if (it == m_list.end())
throw exceptions::no_such_message_id();
m_list.insert(it + 1, mid);
}
2005-07-12 22:28:02 +00:00
void messageIdSequence::insertMessageIdAfter(const int pos, ref <messageId> mid)
{
m_list.insert(m_list.begin() + pos + 1, mid);
}
2005-07-12 22:28:02 +00:00
void messageIdSequence::removeMessageId(ref <messageId> mid)
{
2005-07-12 22:28:02 +00:00
const std::vector <ref <messageId> >::iterator it = std::find
(m_list.begin(), m_list.end(), mid);
if (it == m_list.end())
throw exceptions::no_such_message_id();
m_list.erase(it);
}
void messageIdSequence::removeMessageId(const int pos)
{
2005-07-12 22:28:02 +00:00
const std::vector <ref <messageId> >::iterator it = m_list.begin() + pos;
m_list.erase(it);
}
void messageIdSequence::removeAllMessageIds()
{
2005-07-12 22:28:02 +00:00
m_list.clear();
}
const int messageIdSequence::getMessageIdCount() const
{
return (m_list.size());
}
const bool messageIdSequence::isEmpty() const
{
return (m_list.empty());
}
2005-07-12 22:28:02 +00:00
const ref <messageId> messageIdSequence::getMessageIdAt(const int pos)
{
return (m_list[pos]);
}
2005-07-12 22:28:02 +00:00
const ref <const messageId> messageIdSequence::getMessageIdAt(const int pos) const
{
return (m_list[pos]);
}
2005-07-12 22:28:02 +00:00
const std::vector <ref <const messageId> > messageIdSequence::getMessageIdList() const
{
2005-07-12 22:28:02 +00:00
std::vector <ref <const messageId> > list;
list.reserve(m_list.size());
2005-07-12 22:28:02 +00:00
for (std::vector <ref <messageId> >::const_iterator it = m_list.begin() ;
it != m_list.end() ; ++it)
{
list.push_back(*it);
}
return (list);
}
2005-07-12 22:28:02 +00:00
const std::vector <ref <messageId> > messageIdSequence::getMessageIdList()
{
return (m_list);
}
} // vmime