/[debian]/mimetic/branches/upstream/current/mimetic/contenttype.cxx
ViewVC logotype

Contents of /mimetic/branches/upstream/current/mimetic/contenttype.cxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 128 - (show annotations)
Sat Feb 25 16:49:20 2006 UTC (15 years, 5 months ago) by gregoa
File size: 4811 byte(s)
[svn-inject] Installing original source of mimetic
1 /***************************************************************************
2 copyright : (C) 2002-2005 by Stefano Barbato
3 email : stefano@codesink.org
4
5 $Id: contenttype.cxx,v 1.2 2005/02/23 10:26:14 tat Exp $
6 ***************************************************************************/
7
8 /***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16 #include <cstdlib>
17 #include <ctime>
18 #include <sstream>
19 #include <iomanip>
20 #include <cassert>
21 #include <mimetic/contenttype.h>
22 #include <mimetic/tokenizer.h>
23 #include <mimetic/utils.h>
24
25 namespace mimetic
26 {
27 using namespace std;
28
29 const char ContentType::label[] = "Content-Type";
30
31 int ContentType::Boundary::ms_i = 0;
32 string ContentType::Boundary::ms_common_boundary = string();
33
34
35 ContentType::Boundary::Boundary()
36 {
37 if(ms_i++ == 0)
38 { // initialize static boundary string
39 const char tb[] = "0123456789"
40 "abcdefghijklmnopqrstuvwxyz"
41 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
42 "-_."; // "=+,()/"
43 stringstream ss;
44 srand(time(0));
45 short tbSize = sizeof(tb)-1;
46 for(uint i=0; i < 48; ++i)
47 {
48 unsigned int r = rand();
49 ss << tb[r % tbSize];
50 }
51 ms_common_boundary = "----" + ss.str();
52 }
53 m_boundary = ms_common_boundary + "=_" + utils::int2hex(ms_i) + "_";
54 }
55
56 ContentType::Boundary::operator const string&() const
57 {
58 return m_boundary;
59 }
60
61 ContentType::ContentType()
62 {
63 // m_paramList.push_back(Param("charset", "us-ascii"));
64 }
65
66 ContentType::ContentType(const char* cstr)
67 {
68 set(cstr);
69 }
70
71 ContentType::ContentType(const string& value)
72 {
73 set(value);
74 }
75
76 ContentType::ContentType(const string& stype, const string& ssubtype)
77 {
78 set(stype, ssubtype);
79 }
80
81 void ContentType::set(const string& stype, const string& ssubtype)
82 {
83 type(stype);
84 subtype(ssubtype);
85 }
86
87 bool ContentType::isMultipart() const
88 {
89 return m_type == "multipart";
90 }
91
92 void ContentType::param(const string& name, const string& value)
93 {
94 ParamList::iterator bit = m_paramList.begin(), eit = m_paramList.end();
95 for(; bit != eit; ++bit)
96 {
97 if(bit->name() == name)
98 {
99 bit->value(value);
100 return;
101 }
102 }
103 m_paramList.push_back(Param(name, value));
104 }
105
106 const string& ContentType::param(const string& field) const
107 {
108 ParamList::const_iterator bit = m_paramList.begin(), eit = m_paramList.end();
109 for(; bit != eit; ++bit)
110 {
111 if(bit->name() == field)
112 return bit->value();
113 }
114 return nullstring;
115 }
116
117 void ContentType::type(const string& v)
118 {
119 m_type = v;
120 // if(isMultipart())
121 // m_paramList.push_back(ContentType::Param("boundary", Boundary()));
122 }
123
124 void ContentType::subtype(const string& v)
125 {
126 m_subtype = v;
127 }
128
129 const istring& ContentType::type() const
130 {
131 return m_type;
132 }
133
134 const istring& ContentType::subtype() const
135 {
136 return m_subtype;
137 }
138
139 const ContentType::ParamList& ContentType::paramList() const
140 {
141 return m_paramList;
142 }
143
144 ContentType::ParamList& ContentType::paramList()
145 {
146 return m_paramList;
147 }
148
149
150 void ContentType::set(const string& val)
151 {
152 StringTokenizer stok(&val, ";");
153 string ct;
154 if(!stok.next(ct))
155 return;
156
157 // parse type/subtype
158 string stype, ssubtype;
159 stok.setDelimList("/");
160 stok.setSource(&ct);
161 stok.next(stype);
162 stok.next(ssubtype);
163 set(stype, ssubtype);
164
165 // parse field params
166 string params(val, min(val.length(), ct.length() + 1));
167 if(!params.length())
168 return;
169 string paramValue;
170 stok.setDelimList(";");
171 stok.setSource(&params);
172 while(stok.next(paramValue))
173 {
174 ContentType::Param p(paramValue);
175 m_paramList.push_back(p);
176 }
177 }
178
179 string ContentType::str() const
180 {
181 string ostr = m_type + "/" + m_subtype;
182 ParamList::const_iterator bit = m_paramList.begin(),
183 eit = m_paramList.end();
184 for(; bit != eit; ++bit)
185 ostr += "; " + bit->name() + "=\"" + bit->value() + "\"";
186 return ostr;
187 }
188
189
190 FieldValue* ContentType::clone() const
191 {
192 return new ContentType(*this);
193 }
194
195 }
196

  ViewVC Help
Powered by ViewVC 1.1.26