From d8b3d2b6414f307bb166fe4f5d4aa16003007ce6 Mon Sep 17 00:00:00 2001 From: Vincent Richard Date: Sun, 17 Feb 2013 22:18:32 +0100 Subject: [PATCH] Added helper function to replace header field. --- src/header.cpp | 7 +++++++ tests/parser/headerTest.cpp | 19 +++++++++++++++++++ vmime/header.hpp | 8 ++++++++ 3 files changed, 34 insertions(+) diff --git a/src/header.cpp b/src/header.cpp index 663eb8df..6543a302 100644 --- a/src/header.cpp +++ b/src/header.cpp @@ -276,6 +276,13 @@ void header::removeField(const size_t pos) } +void header::replaceField(ref field, ref newField) +{ + insertFieldBefore(field, newField); + removeField(field); +} + + void header::removeAllFields() { m_fields.clear(); diff --git a/tests/parser/headerTest.cpp b/tests/parser/headerTest.cpp index d096b28c..47bd0ebe 100644 --- a/tests/parser/headerTest.cpp +++ b/tests/parser/headerTest.cpp @@ -43,6 +43,8 @@ VMIME_TEST_SUITE_BEGIN VMIME_TEST(testInsertFieldAfter1) VMIME_TEST(testInsertFieldAfter2) + VMIME_TEST(testReplaceField) + VMIME_TEST(testRemoveField1) VMIME_TEST(testRemoveField2) @@ -192,6 +194,23 @@ VMIME_TEST_SUITE_BEGIN VASSERT_EQ("Third value", "C: c", headerTest::getFieldValue(*res[2])); } + // replaceField + void testReplaceField() + { + vmime::header hdr; + hdr.parse("A: a\r\nB: b\r\nC: c\r\n"); + + vmime::ref hf = vmime::headerFieldFactory::getInstance()->create("Z", "z"); + hdr.replaceField(hdr.getField("B"), hf); + + std::vector > res = hdr.getFieldList(); + + VASSERT_EQ("Count", static_cast (3), res.size()); + VASSERT_EQ("First value", "A: a", headerTest::getFieldValue(*res[0])); + VASSERT_EQ("Second value", "Z: z", headerTest::getFieldValue(*res[1])); + VASSERT_EQ("Third value", "C: c", headerTest::getFieldValue(*res[2])); + } + // removeField void testRemoveField1() { diff --git a/vmime/header.hpp b/vmime/header.hpp index fcbd09e8..6f5bf6d7 100644 --- a/vmime/header.hpp +++ b/vmime/header.hpp @@ -170,6 +170,14 @@ public: */ void removeField(const size_t pos); + /** Replaces a field with another field. + * + * @param field field to be replaced + * @param newField field to replace with + * @throw exceptions::no_such_field if the field is not in the list + */ + void replaceField(ref field, ref newField); + /** Remove all fields from the list. */ void removeAllFields();