1. dimajix
  2. Untitled project
  3. Magnum

Source

Magnum / source / libs / magnum_net / source / magnum / net / MimeHeaders.cpp

/* 
  Magnum 
  Copyright (C) 2002-2012 Kaya Kupferschmidt. All Rights Reserved.

  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any damages
  arising from the use of this software.

  Permission is granted to anyone to use this software for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely, under the terms in LICENSE.TXT.

  Kaya Kupferschmidt  (k.kupferschmidt@dimajix.de)
*/

#include "magnum/net/Exception.h"
#include "magnum/net/MimeHeaders.h"


namespace magnum { namespace net {
using magnum::object::NullPointerException;
using magnum::object::IllegalArgumentException;


/*--------------------------------------------------------------------------*/
/**
 */
MimeHeaders::MimeHeaders() 
{
}


/*--------------------------------------------------------------------------*/
/**
 */
MimeHeaders::~MimeHeaders() 
{
}


/*--------------------------------------------------------------------------*/
/**
 */
void MimeHeaders::insert(const CString& key,const CString& val)
{
	if (key.isEmpty())
		throw IllegalArgumentException();
		
    m_Headers.insert(Pair<CString, CString>(key, val));
}


/*--------------------------------------------------------------------------*/
/**
 */
void MimeHeaders::replace(const CString& key,const CString& val)
{
	if (key.isEmpty())
		throw IllegalArgumentException();
		
	Iterator iter = at(key);
	if (iter == m_Headers.end())
        m_Headers.insert(Pair<CString, CString>(key, val));
	else
		iter->second = val;
}


/*--------------------------------------------------------------------------*/
/**
 */
void MimeHeaders::remove(const CString& key)
{
    for (;;) {
	    Iterator iter = at(key);
	    if (iter == m_Headers.end())
	        break;
	    m_Headers.remove(iter);
    }
}


/*--------------------------------------------------------------------------*/
/**
 */
bool MimeHeaders::contains(const CString& key)
{
	return (at(key) != m_Headers.end());
}


/*--------------------------------------------------------------------------*/
/**
 */
const CString& MimeHeaders::find(const CString& key) const
{
	ConstIterator iter = at(key);
	if (iter == m_Headers.end())
        return CString::Null;
	else
		return iter->second;
}


/*--------------------------------------------------------------------------*/
/**
 */
MimeHeaders::Iterator MimeHeaders::at(const CString& key)
{
	Iterator iter = m_Headers.begin();
	while (iter != m_Headers.end()) {
        if (iter->first.compare(key, CString::CaseInsensitive) == 0)
			break;
		++iter;
	}
	
	return iter;
}


/*--------------------------------------------------------------------------*/
/**
 */
MimeHeaders::ConstIterator MimeHeaders::at(const CString& key) const
{
	ConstIterator iter = m_Headers.begin();
	while (iter != m_Headers.end()) {
        if (iter->first.compare(key, CString::CaseInsensitive) == 0)
			break;
		++iter;
	}
	
	return iter;
}


/*--------------------------------------------------------------------------*/
/**
 */
void MimeHeaders::writeTo(OutputStream* os) const 
{
	if (os == NULL)
		throw NullPointerException();
	
	for (ConstIterator iter = m_Headers.begin(); iter != m_Headers.end(); ++iter) {
        *os << iter->first << ": " << iter->second << "\r\n";
	}
}

} }