Added helper function to replace header field.

This commit is contained in:
Vincent Richard 2013-02-17 22:18:32 +01:00
parent 281300ed6a
commit d8b3d2b641
3 changed files with 34 additions and 0 deletions

View File

@ -276,6 +276,13 @@ void header::removeField(const size_t pos)
} }
void header::replaceField(ref <headerField> field, ref <headerField> newField)
{
insertFieldBefore(field, newField);
removeField(field);
}
void header::removeAllFields() void header::removeAllFields()
{ {
m_fields.clear(); m_fields.clear();

View File

@ -43,6 +43,8 @@ VMIME_TEST_SUITE_BEGIN
VMIME_TEST(testInsertFieldAfter1) VMIME_TEST(testInsertFieldAfter1)
VMIME_TEST(testInsertFieldAfter2) VMIME_TEST(testInsertFieldAfter2)
VMIME_TEST(testReplaceField)
VMIME_TEST(testRemoveField1) VMIME_TEST(testRemoveField1)
VMIME_TEST(testRemoveField2) VMIME_TEST(testRemoveField2)
@ -192,6 +194,23 @@ VMIME_TEST_SUITE_BEGIN
VASSERT_EQ("Third value", "C: c", headerTest::getFieldValue(*res[2])); 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 <vmime::headerField> hf = vmime::headerFieldFactory::getInstance()->create("Z", "z");
hdr.replaceField(hdr.getField("B"), hf);
std::vector <vmime::ref <vmime::headerField> > res = hdr.getFieldList();
VASSERT_EQ("Count", static_cast <unsigned int>(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 // removeField
void testRemoveField1() void testRemoveField1()
{ {

View File

@ -170,6 +170,14 @@ public:
*/ */
void removeField(const size_t pos); 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 <headerField> field, ref <headerField> newField);
/** Remove all fields from the list. /** Remove all fields from the list.
*/ */
void removeAllFields(); void removeAllFields();