summaryrefslogtreecommitdiffstats
path: root/src/parser/media_type.cpp
blob: 44710d49199bc0c6607cf68589474cf77512c6e7 (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
/*
    Copyright (C) 2005-2009  Michel de Boer <michel@twinklephone.com>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

#include <cassert>
#include <cstdlib>

#include "media_type.h"
#include "util.h"
#include "utils/mime_database.h"

using namespace std;
using namespace utils;

t_media::t_media() : q(1.0) {}

t_media::t_media(const string &t, const string &s) :
	type(t),
	subtype(s),
	q(1.0)
{}

t_media::t_media(const string &mime_type) : q(1.0) 
{
	vector<string> v = split(mime_type, '/');
	
	if (v.size() == 2) {
		type = v[0];
		subtype = v[1];
	}
}

void t_media::add_params(const list<t_parameter> &l) {
	list<t_parameter>::const_iterator i = l.begin();

	media_param_list.clear();
	accept_extension_list.clear();

	// Add media parameters
	while (i != l.end() && i->name != "q") {
		if (i->name == "charset") {
			charset = i->value;
		} else {
			media_param_list.push_back(*i);
		}
		++i;
	}

	// Set the quality factor
	if (i != l.end()) {
		q = atof(i->value.c_str());
		i++;
	}

	// Add accept extension parameters
	while (i != l.end()) {
		accept_extension_list.push_back(*i);
		i++;
	}
}


string t_media::encode(void) const {
	string s;

	s = type + '/' + subtype;
	if (!charset.empty()) {
		s += ";charset=";
		s += charset;
	}
	s += param_list2str(media_param_list);
	
	if (q != 1) {
		s += ";q=";
		s += float2str(q, 1);
	}
	
	s += param_list2str(accept_extension_list);

	return s;
}

string t_media::get_file_glob(void) const {
	string file_glob = mime_database->get_glob(type + '/' + subtype);
	return file_glob;
}