summaryrefslogtreecommitdiff
path: root/include/crawler/MIMEType.hpp
blob: d5557b8acc757c2d36d18f4bbd3789bc96902cc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#ifndef __MIMETYPE_H
#define __MIMETYPE_H

#include <string>
#include <cstring>
#include <iostream>
#include <sstream>

#include "CrawlerExportable.hpp"

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 CRAWLER_DLL_VISIBLE 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