Source

Magnum / source / libs / magnum_net / source / magnum / net / MediaType.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/MediaType.h"


namespace magnum { namespace net {
using magnum::types::index_t;


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


/*--------------------------------------------------------------------------*/
/**
 */
MediaType::MediaType(const CString& type)
{
    parse(type);
}


/*--------------------------------------------------------------------------*/
/**
 */
MediaType::MediaType(const CString& type, const CString& subtype)
 : m_Type(type)
 , m_Subtype(subtype)
{
}
        

/*--------------------------------------------------------------------------*/
/**
 */
bool MediaType::isEmpty() const
{
    return m_Type.isEmpty() && m_Subtype.isEmpty();
}


/*--------------------------------------------------------------------------*/
/**
 */
bool MediaType::isValid() const
{
    return !m_Type.isEmpty() && !m_Subtype.isEmpty();
}


/*--------------------------------------------------------------------------*/
/**
 */
String MediaType::toString() const
{
    return m_Type.toString() + '/' + m_Subtype;
}


/*--------------------------------------------------------------------------*/
/**
 */
CString MediaType::toCString() const
{
    return m_Type + '/' + m_Subtype;
}


/*--------------------------------------------------------------------------*/
/**
 */
void MediaType::parse(const CString& str)
{
    index_t split = str.indexOf('/');
    if (split == CString::npos) {
        m_Type = str;
        m_Subtype.clear();
    }
    else {
        m_Type = str.substring(0, split);
        m_Subtype = str.substring(split+1);
    }
}

} }