summaryrefslogtreecommitdiff
path: root/include/crawler/MIMEType.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'include/crawler/MIMEType.hpp')
-rw-r--r--include/crawler/MIMEType.hpp100
1 files changed, 100 insertions, 0 deletions
diff --git a/include/crawler/MIMEType.hpp b/include/crawler/MIMEType.hpp
new file mode 100644
index 0000000..3a628ca
--- /dev/null
+++ b/include/crawler/MIMEType.hpp
@@ -0,0 +1,100 @@
+#ifndef __MIMETYPE_H
+#define __MIMETYPE_H
+
+#include <string>
+#include <cstring>
+#include <iostream>
+#include <sstream>
+
+class MIMEType {
+ protected:
+ std::string m_type;
+ std::string m_subtype;
+
+ public:
+ MIMEType( )
+ : m_type( "" ), m_subtype( "" )
+ {
+ }
+
+ MIMEType( const std::string _type, const std::string _subtype )
+ : m_type( _type ), m_subtype( _subtype )
+ {
+ }
+
+ MIMEType( const MIMEType &m )
+ : m_type( m.m_type ), m_subtype( m.m_subtype )
+ {
+ }
+
+ MIMEType( const char *s )
+ {
+ const char *pos;
+ if( ( pos = strchr( s, '/' ) ) == NULL ) {
+ *this = Null;
+ } else {
+ m_type = std::string( s, 0, pos - s );
+ m_subtype = std::string( s, pos - s + 1, strlen( s ) - ( pos - s + 1 ) );
+ }
+ }
+
+ MIMEType& operator=( const MIMEType &m )
+ {
+ if( this != &m ) {
+ this->m_type = m.m_type;
+ this->m_subtype = m.m_subtype;
+ }
+ return *this;
+ }
+
+ const std::string type( ) const
+ {
+ return m_type;
+ }
+
+ const std::string subtype( ) const
+ {
+ return m_subtype;
+ }
+
+ std::string str( ) const
+ {
+ std::ostringstream os;
+ os << *this;
+ return os.str( );
+ }
+
+ static MIMEType Null;
+
+ bool operator!=( const MIMEType &other ) const
+ {
+ return( str( ) != other.str( ) );
+ }
+
+ bool operator==( const MIMEType &other ) const
+ {
+ return( str( ) == other.str( ) );
+ }
+
+ bool operator<( const MIMEType &other ) const
+ {
+ return( str( ) < other.str( ) );
+ }
+
+ template< typename CharT, typename TraitsT > friend
+ std::basic_ostream< CharT, TraitsT >& operator<<( std::basic_ostream<CharT, TraitsT>&s, const MIMEType& m );
+};
+
+template< typename CharT, typename TraitsT >
+inline std::basic_ostream< CharT, TraitsT >& operator<<( std::basic_ostream< CharT, TraitsT > &s, const MIMEType &m )
+{
+ if( m.type( ).empty( ) ) {
+ return s;
+ }
+
+ s << m.type( ) << "/" << m.subtype( );
+
+ return s;
+}
+
+#endif