2004-10-05 10:28:21 +00:00
|
|
|
//
|
2005-03-18 21:33:11 +00:00
|
|
|
// VMime library (http://www.vmime.org)
|
2005-01-03 12:26:48 +00:00
|
|
|
// Copyright (C) 2002-2005 Vincent Richard <vincent@vincent-richard.net>
|
2004-10-05 10:28:21 +00:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// 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., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
//
|
|
|
|
|
2004-12-26 20:23:29 +00:00
|
|
|
#include "vmime/text.hpp"
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2005-01-05 22:17:23 +00:00
|
|
|
#include "vmime/parserHelpers.hpp"
|
2004-11-07 10:33:01 +00:00
|
|
|
|
2004-10-05 10:28:21 +00:00
|
|
|
|
|
|
|
namespace vmime
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
text::text()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
text::text(const text& t)
|
2004-10-21 15:05:47 +00:00
|
|
|
: component()
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
2004-10-21 15:05:47 +00:00
|
|
|
copyFrom(t);
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
text::text(const string& t, const charset& ch)
|
|
|
|
{
|
2004-10-21 15:05:47 +00:00
|
|
|
text::newFromString(t, ch, this);
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
text::text(const string& t)
|
|
|
|
{
|
2004-10-21 15:05:47 +00:00
|
|
|
text::newFromString(t, charset::getLocaleCharset(), this);
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
text::text(const word& w)
|
|
|
|
{
|
2004-10-21 15:05:47 +00:00
|
|
|
appendWord(new word(w));
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
text::~text()
|
|
|
|
{
|
2004-10-21 15:05:47 +00:00
|
|
|
removeAllWords();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void text::parse(const string& buffer, const string::size_type position,
|
|
|
|
const string::size_type end, string::size_type* newPosition)
|
|
|
|
{
|
2005-03-15 10:30:42 +00:00
|
|
|
removeAllWords();
|
|
|
|
|
|
|
|
string::size_type newPos;
|
|
|
|
|
|
|
|
const std::vector <word*> words = word::parseMultiple(buffer, position, end, &newPos);
|
2004-10-21 15:05:47 +00:00
|
|
|
|
2005-03-15 10:30:42 +00:00
|
|
|
copy_vector(words, m_words);
|
|
|
|
|
|
|
|
setParsedBounds(position, newPos);
|
2004-12-15 20:28:09 +00:00
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
if (newPosition)
|
2005-03-15 10:30:42 +00:00
|
|
|
*newPosition = newPos;
|
2004-10-21 15:05:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void text::generate(utility::outputStream& os, const string::size_type maxLineLength,
|
|
|
|
const string::size_type curLinePos, string::size_type* newLinePos) const
|
|
|
|
{
|
2004-11-07 10:33:01 +00:00
|
|
|
encodeAndFold(os, maxLineLength, curLinePos, newLinePos, 0);
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#if VMIME_WIDE_CHAR_SUPPORT
|
|
|
|
|
|
|
|
const wstring text::getDecodedText() const
|
|
|
|
{
|
|
|
|
wstring out;
|
|
|
|
|
|
|
|
for (std::vector <word*>::const_iterator i = m_words.begin() ; i != m_words.end() ; ++i)
|
|
|
|
out += (*i)->getDecodedText();
|
|
|
|
|
|
|
|
return (out);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
void text::copyFrom(const component& other)
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
2004-10-21 15:05:47 +00:00
|
|
|
const text& t = dynamic_cast <const text&>(other);
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
removeAllWords();
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
for (std::vector <word*>::const_iterator i = t.m_words.begin() ; i != t.m_words.end() ; ++i)
|
|
|
|
m_words.push_back(new word(**i));
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
text& text::operator=(const component& other)
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
2004-10-21 15:05:47 +00:00
|
|
|
copyFrom(other);
|
|
|
|
return (*this);
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
text& text::operator=(const text& other)
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
2004-10-21 15:05:47 +00:00
|
|
|
copyFrom(other);
|
2004-10-05 10:28:21 +00:00
|
|
|
return (*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const bool text::operator==(const text& t) const
|
|
|
|
{
|
2004-10-21 15:05:47 +00:00
|
|
|
if (getWordCount() == t.getWordCount())
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
2004-10-26 20:17:28 +00:00
|
|
|
bool equal = true;
|
2004-10-05 10:28:21 +00:00
|
|
|
|
|
|
|
std::vector <word*>::const_iterator i = m_words.begin();
|
|
|
|
std::vector <word*>::const_iterator j = t.m_words.begin();
|
|
|
|
|
|
|
|
for ( ; equal && i != m_words.end() ; ++i, ++j)
|
2004-10-26 20:17:28 +00:00
|
|
|
equal = (**i == **j);
|
2004-10-05 10:28:21 +00:00
|
|
|
|
|
|
|
return (equal);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const bool text::operator!=(const text& t) const
|
|
|
|
{
|
|
|
|
return !(*this == t);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const string text::getConvertedText(const charset& dest) const
|
|
|
|
{
|
|
|
|
string out;
|
|
|
|
|
|
|
|
for (std::vector <word*>::const_iterator i = m_words.begin() ; i != m_words.end() ; ++i)
|
|
|
|
out += (*i)->getConvertedText(dest);
|
|
|
|
|
|
|
|
return (out);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
void text::appendWord(word* w)
|
|
|
|
{
|
|
|
|
m_words.push_back(w);
|
|
|
|
}
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
|
|
|
|
void text::insertWordBefore(const int pos, word* w)
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
2004-10-21 15:05:47 +00:00
|
|
|
m_words.insert(m_words.begin() + pos, w);
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
void text::insertWordAfter(const int pos, word* w)
|
|
|
|
{
|
|
|
|
m_words.insert(m_words.begin() + pos + 1, w);
|
|
|
|
}
|
|
|
|
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
void text::removeWord(const int pos)
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
2004-10-21 15:05:47 +00:00
|
|
|
const std::vector <word*>::iterator it = m_words.begin() + pos;
|
|
|
|
|
|
|
|
delete (*it);
|
|
|
|
|
|
|
|
m_words.erase(it);
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
void text::removeAllWords()
|
|
|
|
{
|
|
|
|
free_container(m_words);
|
|
|
|
}
|
|
|
|
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
const int text::getWordCount() const
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
|
|
|
return (m_words.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
const bool text::isEmpty() const
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
2004-10-21 15:05:47 +00:00
|
|
|
return (m_words.empty());
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
word* text::getWordAt(const int pos)
|
|
|
|
{
|
|
|
|
return (m_words[pos]);
|
|
|
|
}
|
|
|
|
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2005-01-06 20:01:41 +00:00
|
|
|
const word* text::getWordAt(const int pos) const
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
2004-10-21 15:05:47 +00:00
|
|
|
return (m_words[pos]);
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
const std::vector <const word*> text::getWordList() const
|
|
|
|
{
|
|
|
|
std::vector <const word*> list;
|
|
|
|
|
|
|
|
list.reserve(m_words.size());
|
|
|
|
|
|
|
|
for (std::vector <word*>::const_iterator it = m_words.begin() ;
|
|
|
|
it != m_words.end() ; ++it)
|
|
|
|
{
|
|
|
|
list.push_back(*it);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (list);
|
|
|
|
}
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
|
|
|
|
const std::vector <word*> text::getWordList()
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
2004-10-21 15:05:47 +00:00
|
|
|
return (m_words);
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
text* text::clone() const
|
|
|
|
{
|
|
|
|
return new text(*this);
|
|
|
|
}
|
|
|
|
|
2004-10-05 10:28:21 +00:00
|
|
|
|
2004-10-21 15:05:47 +00:00
|
|
|
text* text::newFromString(const string& in, const charset& ch, text* generateInExisting)
|
2004-10-05 10:28:21 +00:00
|
|
|
{
|
2004-10-21 15:05:47 +00:00
|
|
|
const string::const_iterator end = in.end();
|
|
|
|
string::const_iterator p = in.begin();
|
|
|
|
string::const_iterator start = in.begin();
|
|
|
|
|
|
|
|
bool is8bit = false; // is the current word 8-bit?
|
|
|
|
bool prevIs8bit = false; // is previous word 8-bit?
|
|
|
|
unsigned int count = 0; // total number of words
|
|
|
|
|
|
|
|
text* out = (generateInExisting != NULL) ? generateInExisting : new text();
|
|
|
|
|
|
|
|
out->removeAllWords();
|
|
|
|
|
|
|
|
for ( ; ; )
|
|
|
|
{
|
2005-03-16 17:13:08 +00:00
|
|
|
if (p == end || parserHelpers::isSpace(*p))
|
2004-10-21 15:05:47 +00:00
|
|
|
{
|
|
|
|
if (p != end)
|
|
|
|
++p;
|
|
|
|
|
|
|
|
if (is8bit)
|
|
|
|
{
|
|
|
|
if (count && prevIs8bit)
|
|
|
|
{
|
|
|
|
// No need to create a new encoded word, just append
|
|
|
|
// the current word to the previous one.
|
|
|
|
out->getWordAt(out->getWordCount() - 1)->
|
|
|
|
getBuffer() += string(start, p);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
out->appendWord(new word(string(start, p), ch));
|
|
|
|
|
|
|
|
prevIs8bit = true;
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (count && !prevIs8bit)
|
|
|
|
{
|
|
|
|
out->getWordAt(out->getWordCount() - 1)->
|
|
|
|
getBuffer() += string(start, p);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
out->appendWord(new word
|
|
|
|
(string(start, p), charset(charsets::US_ASCII)));
|
|
|
|
|
|
|
|
prevIs8bit = false;
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p == end)
|
|
|
|
break;
|
|
|
|
|
|
|
|
is8bit = false;
|
|
|
|
start = p;
|
|
|
|
}
|
2005-03-16 17:13:08 +00:00
|
|
|
else if (!parserHelpers::isAscii(*p))
|
2004-10-21 15:05:47 +00:00
|
|
|
{
|
|
|
|
is8bit = true;
|
|
|
|
++p;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
++p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (out);
|
2004-10-05 10:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-11-07 10:33:01 +00:00
|
|
|
void text::encodeAndFold(utility::outputStream& os, const string::size_type maxLineLength,
|
|
|
|
const string::size_type firstLineOffset, string::size_type* lastLineLength, const int flags) const
|
|
|
|
{
|
|
|
|
string::size_type curLineLength = firstLineOffset;
|
|
|
|
|
|
|
|
for (int wi = 0 ; wi < getWordCount() ; ++wi)
|
|
|
|
{
|
2005-03-15 10:30:42 +00:00
|
|
|
getWordAt(wi)->generate(os, maxLineLength, curLineLength,
|
|
|
|
&curLineLength, flags, (wi == 0));
|
2004-11-07 10:33:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (lastLineLength)
|
|
|
|
*lastLineLength = curLineLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
text* text::decodeAndUnfold(const string& in, text* generateInExisting)
|
|
|
|
{
|
|
|
|
text* out = (generateInExisting != NULL) ? generateInExisting : new text();
|
|
|
|
|
|
|
|
out->removeAllWords();
|
|
|
|
|
2005-03-15 10:30:42 +00:00
|
|
|
const std::vector <word*> words = word::parseMultiple(in, 0, in.length(), NULL);
|
|
|
|
|
|
|
|
copy_vector(words, out->m_words);
|
2004-11-07 10:33:01 +00:00
|
|
|
|
|
|
|
return (out);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-03-15 10:30:42 +00:00
|
|
|
const std::vector <const component*> text::getChildComponents() const
|
2004-11-07 10:33:01 +00:00
|
|
|
{
|
2005-03-15 10:30:42 +00:00
|
|
|
std::vector <const component*> list;
|
2004-11-07 10:33:01 +00:00
|
|
|
|
2005-03-15 10:30:42 +00:00
|
|
|
copy_vector(m_words, list);
|
2004-11-07 10:33:01 +00:00
|
|
|
|
2005-03-15 10:30:42 +00:00
|
|
|
return (list);
|
2004-12-20 12:33:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-10-05 10:28:21 +00:00
|
|
|
} // vmime
|