]> git.saurik.com Git - wxWidgets.git/blob - src/html/htmlfilt.cpp
don't expect errno to be set to EOVERFLOW if vsnprintf() fails
[wxWidgets.git] / src / html / htmlfilt.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: htmlfilt.cpp
3 // Purpose: wxHtmlFilter - input filter for translating into HTML format
4 // Author: Vaclav Slavik
5 // RCS-ID: $Id$
6 // Copyright: (c) 1999 Vaclav Slavik
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #include "wx/wxprec.h"
11
12 #include "wx/defs.h"
13 #if wxUSE_HTML && wxUSE_STREAMS
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #ifndef WXPRECOMP
20 #include "wx/log.h"
21 #include "wx/intl.h"
22 #endif
23
24 #include "wx/strconv.h"
25 #include "wx/html/htmlfilt.h"
26 #include "wx/html/htmlwin.h"
27
28 // utility function: read a wxString from a wxInputStream
29 static void ReadString(wxString& str, wxInputStream* s, wxMBConv& conv)
30 {
31 size_t streamSize = s->GetSize();
32
33 if (streamSize == ~(size_t)0)
34 {
35 const size_t bufSize = 4095;
36 char buffer[bufSize+1];
37 size_t lastRead;
38
39 do
40 {
41 s->Read(buffer, bufSize);
42 lastRead = s->LastRead();
43 buffer[lastRead] = 0;
44 str.Append(wxString(buffer, conv));
45 }
46 while (lastRead == bufSize);
47 }
48 else
49 {
50 char* src = new char[streamSize+1];
51 s->Read(src, streamSize);
52 src[streamSize] = 0;
53 str = wxString(src, conv);
54 delete[] src;
55 }
56 }
57
58 /*
59
60 There is code for several default filters:
61
62 */
63
64 IMPLEMENT_ABSTRACT_CLASS(wxHtmlFilter, wxObject)
65
66 //--------------------------------------------------------------------------------
67 // wxHtmlFilterPlainText
68 // filter for text/plain or uknown
69 //--------------------------------------------------------------------------------
70
71 IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterPlainText, wxHtmlFilter)
72
73 bool wxHtmlFilterPlainText::CanRead(const wxFSFile& WXUNUSED(file)) const
74 {
75 return true;
76 }
77
78
79
80 wxString wxHtmlFilterPlainText::ReadFile(const wxFSFile& file) const
81 {
82 wxInputStream *s = file.GetStream();
83 wxString doc, doc2;
84
85 if (s == NULL) return wxEmptyString;
86 ReadString(doc, s, wxConvISO8859_1);
87
88 doc.Replace(wxT("&"), wxT("&"), true);
89 doc.Replace(wxT("<"), wxT("&lt;"), true);
90 doc.Replace(wxT(">"), wxT("&gt;"), true);
91 doc2 = wxT("<HTML><BODY><PRE>\n") + doc + wxT("\n</PRE></BODY></HTML>");
92 return doc2;
93 }
94
95
96
97
98
99 //--------------------------------------------------------------------------------
100 // wxHtmlFilterImage
101 // filter for image/*
102 //--------------------------------------------------------------------------------
103
104 class wxHtmlFilterImage : public wxHtmlFilter
105 {
106 DECLARE_DYNAMIC_CLASS(wxHtmlFilterImage)
107
108 public:
109 virtual bool CanRead(const wxFSFile& file) const;
110 virtual wxString ReadFile(const wxFSFile& file) const;
111 };
112
113 IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterImage, wxHtmlFilter)
114
115
116
117 bool wxHtmlFilterImage::CanRead(const wxFSFile& file) const
118 {
119 return (file.GetMimeType().Left(6) == wxT("image/"));
120 }
121
122
123
124 wxString wxHtmlFilterImage::ReadFile(const wxFSFile& file) const
125 {
126 wxString res = wxT("<HTML><BODY><IMG SRC=\"") + file.GetLocation() + wxT("\"></BODY></HTML>");
127 return res;
128 }
129
130
131
132
133 //--------------------------------------------------------------------------------
134 // wxHtmlFilterHTML
135 // filter for text/html
136 //--------------------------------------------------------------------------------
137
138
139 IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterHTML, wxHtmlFilter)
140
141 bool wxHtmlFilterHTML::CanRead(const wxFSFile& file) const
142 {
143 // return (file.GetMimeType() == "text/html");
144 // This is true in most case but some page can return:
145 // "text/html; char-encoding=...."
146 // So we use Find instead
147 return (file.GetMimeType().Find(wxT("text/html")) == 0);
148 }
149
150
151
152 wxString wxHtmlFilterHTML::ReadFile(const wxFSFile& file) const
153 {
154 wxInputStream *s = file.GetStream();
155 wxString doc;
156
157 if (s == NULL)
158 {
159 wxLogError(_("Cannot open HTML document: %s"), file.GetLocation().c_str());
160 return wxEmptyString;
161 }
162
163 // NB: We convert input file to wchar_t here in Unicode mode, based on
164 // either Content-Type header or <meta> tags. In ANSI mode, we don't
165 // do it as it is done by wxHtmlParser (for this reason, we add <meta>
166 // tag if we used Content-Type header).
167 #if wxUSE_UNICODE
168 int charsetPos;
169 if ((charsetPos = file.GetMimeType().Find(_T("; charset="))) != wxNOT_FOUND)
170 {
171 wxString charset = file.GetMimeType().Mid(charsetPos + 10);
172 wxCSConv conv(charset);
173 ReadString(doc, s, conv);
174 }
175 else
176 {
177 wxString tmpdoc;
178 ReadString(tmpdoc, s, wxConvISO8859_1);
179 wxString charset = wxHtmlParser::ExtractCharsetInformation(tmpdoc);
180 if (charset.empty())
181 doc = tmpdoc;
182 else
183 {
184 wxCSConv conv(charset);
185 doc = wxString(tmpdoc.mb_str(wxConvISO8859_1), conv);
186 }
187 }
188 #else // !wxUSE_UNICODE
189 ReadString(doc, s, wxConvLibc);
190 // add meta tag if we obtained this through http:
191 if (!file.GetMimeType().empty())
192 {
193 wxString hdr;
194 wxString mime = file.GetMimeType();
195 hdr.Printf(_T("<meta http-equiv=\"Content-Type\" content=\"%s\">"), mime.c_str());
196 return hdr+doc;
197 }
198 #endif
199
200 return doc;
201 }
202
203
204
205
206 ///// Module:
207
208 class wxHtmlFilterModule : public wxModule
209 {
210 DECLARE_DYNAMIC_CLASS(wxHtmlFilterModule)
211
212 public:
213 virtual bool OnInit()
214 {
215 wxHtmlWindow::AddFilter(new wxHtmlFilterHTML);
216 wxHtmlWindow::AddFilter(new wxHtmlFilterImage);
217 return true;
218 }
219 virtual void OnExit() {}
220 };
221
222 IMPLEMENT_DYNAMIC_CLASS(wxHtmlFilterModule, wxModule)
223
224 #endif