From 681297e10b666e13cc463f6fbb16236f36c3266c Mon Sep 17 00:00:00 2001 From: Vincent Richard Date: Tue, 12 Jul 2005 22:28:02 +0000 Subject: Reference counting and smart pointers. --- src/text.cpp | 94 ++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 54 insertions(+), 40 deletions(-) (limited to 'src/text.cpp') diff --git a/src/text.cpp b/src/text.cpp index a24d6668..7183e53f 100644 --- a/src/text.cpp +++ b/src/text.cpp @@ -40,19 +40,19 @@ text::text(const text& t) text::text(const string& t, const charset& ch) { - text::newFromString(t, ch, this); + createFromString(t, ch); } text::text(const string& t) { - text::newFromString(t, charset::getLocaleCharset(), this); + createFromString(t, charset::getLocaleCharset()); } text::text(const word& w) { - appendWord(new word(w)); + appendWord(vmime::create (w)); } @@ -69,7 +69,7 @@ void text::parse(const string& buffer, const string::size_type position, string::size_type newPos; - const std::vector words = word::parseMultiple(buffer, position, end, &newPos); + const std::vector > words = word::parseMultiple(buffer, position, end, &newPos); copy_vector(words, m_words); @@ -93,7 +93,7 @@ const wstring text::getDecodedText() const { wstring out; - for (std::vector ::const_iterator i = m_words.begin() ; i != m_words.end() ; ++i) + for (std::vector >::const_iterator i = m_words.begin() ; i != m_words.end() ; ++i) out += (*i)->getDecodedText(); return (out); @@ -108,8 +108,8 @@ void text::copyFrom(const component& other) removeAllWords(); - for (std::vector ::const_iterator i = t.m_words.begin() ; i != t.m_words.end() ; ++i) - m_words.push_back(new word(**i)); + for (std::vector >::const_iterator i = t.m_words.begin() ; i != t.m_words.end() ; ++i) + m_words.push_back(vmime::create (**i)); } @@ -133,8 +133,8 @@ const bool text::operator==(const text& t) const { bool equal = true; - std::vector ::const_iterator i = m_words.begin(); - std::vector ::const_iterator j = t.m_words.begin(); + std::vector >::const_iterator i = m_words.begin(); + std::vector >::const_iterator j = t.m_words.begin(); for ( ; equal && i != m_words.end() ; ++i, ++j) equal = (**i == **j); @@ -156,26 +156,26 @@ const string text::getConvertedText(const charset& dest) const { string out; - for (std::vector ::const_iterator i = m_words.begin() ; i != m_words.end() ; ++i) + for (std::vector >::const_iterator i = m_words.begin() ; i != m_words.end() ; ++i) out += (*i)->getConvertedText(dest); return (out); } -void text::appendWord(word* w) +void text::appendWord(ref w) { m_words.push_back(w); } -void text::insertWordBefore(const int pos, word* w) +void text::insertWordBefore(const int pos, ref w) { m_words.insert(m_words.begin() + pos, w); } -void text::insertWordAfter(const int pos, word* w) +void text::insertWordAfter(const int pos, ref w) { m_words.insert(m_words.begin() + pos + 1, w); } @@ -183,9 +183,7 @@ void text::insertWordAfter(const int pos, word* w) void text::removeWord(const int pos) { - const std::vector ::iterator it = m_words.begin() + pos; - - delete (*it); + const std::vector >::iterator it = m_words.begin() + pos; m_words.erase(it); } @@ -193,7 +191,7 @@ void text::removeWord(const int pos) void text::removeAllWords() { - free_container(m_words); + m_words.clear(); } @@ -209,25 +207,25 @@ const bool text::isEmpty() const } -word* text::getWordAt(const int pos) +const ref text::getWordAt(const int pos) { return (m_words[pos]); } -const word* text::getWordAt(const int pos) const +const ref text::getWordAt(const int pos) const { return (m_words[pos]); } -const std::vector text::getWordList() const +const std::vector > text::getWordList() const { - std::vector list; + std::vector > list; list.reserve(m_words.size()); - for (std::vector ::const_iterator it = m_words.begin() ; + for (std::vector >::const_iterator it = m_words.begin() ; it != m_words.end() ; ++it) { list.push_back(*it); @@ -237,19 +235,29 @@ const std::vector text::getWordList() const } -const std::vector text::getWordList() +const std::vector > text::getWordList() { return (m_words); } -text* text::clone() const +ref text::clone() const +{ + return vmime::create (*this); +} + + +ref text::newFromString(const string& in, const charset& ch) { - return new text(*this); + ref t = vmime::create (); + + t->createFromString(in, ch); + + return t; } -text* text::newFromString(const string& in, const charset& ch, text* generateInExisting) +void text::createFromString(const string& in, const charset& ch) { const string::const_iterator end = in.end(); string::const_iterator p = in.begin(); @@ -259,9 +267,7 @@ text* text::newFromString(const string& in, const charset& ch, text* generateInE 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(); + removeAllWords(); for ( ; ; ) { @@ -276,12 +282,12 @@ text* text::newFromString(const string& in, const charset& ch, text* generateInE { // 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); + ref w = getWordAt(getWordCount() - 1); + w->getBuffer() += string(start, p); } else { - out->appendWord(new word(string(start, p), ch)); + appendWord(vmime::create (string(start, p), ch)); prevIs8bit = true; ++count; @@ -291,12 +297,12 @@ text* text::newFromString(const string& in, const charset& ch, text* generateInE { if (count && !prevIs8bit) { - out->getWordAt(out->getWordCount() - 1)-> - getBuffer() += string(start, p); + ref w = getWordAt(getWordCount() - 1); + w->getBuffer() += string(start, p); } else { - out->appendWord(new word + appendWord(vmime::create (string(start, p), charset(charsets::US_ASCII))); prevIs8bit = false; @@ -320,8 +326,6 @@ text* text::newFromString(const string& in, const charset& ch, text* generateInE ++p; } } - - return (out); } @@ -341,13 +345,23 @@ void text::encodeAndFold(utility::outputStream& os, const string::size_type maxL } +ref text::decodeAndUnfold(const string& in) +{ + ref t = vmime::create (); + + decodeAndUnfold(in, t.get()); + + return t; +} + + text* text::decodeAndUnfold(const string& in, text* generateInExisting) { text* out = (generateInExisting != NULL) ? generateInExisting : new text(); out->removeAllWords(); - const std::vector words = word::parseMultiple(in, 0, in.length(), NULL); + const std::vector > words = word::parseMultiple(in, 0, in.length(), NULL); copy_vector(words, out->m_words); @@ -355,9 +369,9 @@ text* text::decodeAndUnfold(const string& in, text* generateInExisting) } -const std::vector text::getChildComponents() const +const std::vector > text::getChildComponents() const { - std::vector list; + std::vector > list; copy_vector(m_words, list); -- cgit v1.2.3