]> git.saurik.com Git - wxWidgets.git/blame - src/common/fs_inet.cpp
wxTextOutputStream::PutChar and text stream test
[wxWidgets.git] / src / common / fs_inet.cpp
CommitLineData
5526e819
VS
1/////////////////////////////////////////////////////////////////////////////
2// Name: fs_inet.cpp
3// Purpose: HTTP and FTP file system
4// Author: Vaclav Slavik
5// Copyright: (c) 1999 Vaclav Slavik
65571936 6// Licence: wxWindows licence
5526e819
VS
7/////////////////////////////////////////////////////////////////////////////
8
14f355c2 9#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
c3f4609e 10#pragma implementation "fs_inet.h"
5526e819
VS
11#endif
12
3096bd2f 13#include "wx/wxprec.h"
5526e819 14
2b5f62a0 15#ifdef __BORLANDC__
5526e819
VS
16#pragma hdrstop
17#endif
18
31528cd3
VZ
19#if !wxUSE_SOCKETS
20 #undef wxUSE_FS_INET
21 #define wxUSE_FS_INET 0
22#endif
23
24528b0c 24#if wxUSE_FILESYSTEM && wxUSE_FS_INET
e3e717ec 25
5526e819 26#ifndef WXPRECOMP
5526e819
VS
27#endif
28
29#include "wx/wfstream.h"
30#include "wx/url.h"
31#include "wx/filesys.h"
32#include "wx/fs_inet.h"
cf1f0870 33#include "wx/module.h"
5526e819 34
3caa34b7
VS
35// ----------------------------------------------------------------------------
36// Helper classes
37// ----------------------------------------------------------------------------
38
39// This stream deletes the file when destroyed
40class wxTemporaryFileInputStream : public wxFileInputStream
5526e819 41{
3caa34b7
VS
42public:
43 wxTemporaryFileInputStream(const wxString& filename) :
44 wxFileInputStream(filename), m_filename(filename) {}
a62848fd 45
3caa34b7
VS
46 ~wxTemporaryFileInputStream()
47 {
48 // NB: copied from wxFileInputStream dtor, we need to do it before
49 // wxRemoveFile
50 if (m_file_destroy)
51 {
52 delete m_file;
53 m_file_destroy = false;
54 }
a62848fd 55 wxRemoveFile(m_filename);
3caa34b7 56 }
5526e819 57
3caa34b7
VS
58protected:
59 wxString m_filename;
5526e819
VS
60};
61
62
3caa34b7 63// ----------------------------------------------------------------------------
5526e819 64// wxInternetFSHandler
3caa34b7 65// ----------------------------------------------------------------------------
5526e819 66
c3a1c7d2
VS
67static wxString StripProtocolAnchor(const wxString& location)
68{
69 wxString myloc(location.BeforeLast(wxT('#')));
70 if (myloc.IsEmpty()) myloc = location.AfterFirst(wxT(':'));
71 else myloc = myloc.AfterFirst(wxT(':'));
72
73 // fix malformed url:
04dbb646 74 if (myloc.Left(2) != wxT("//"))
c3a1c7d2
VS
75 {
76 if (myloc.GetChar(0) != wxT('/')) myloc = wxT("//") + myloc;
77 else myloc = wxT("/") + myloc;
78 }
79 if (myloc.Mid(2).Find(wxT('/')) == wxNOT_FOUND) myloc << wxT('/');
80
81 return myloc;
82}
83
84
5526e819
VS
85bool wxInternetFSHandler::CanOpen(const wxString& location)
86{
34e0d9f8 87#if wxUSE_URL
5526e819 88 wxString p = GetProtocol(location);
04dbb646 89 if ((p == wxT("http")) || (p == wxT("ftp")))
81915974 90 {
c3a1c7d2 91 wxURL url(p + wxT(":") + StripProtocolAnchor(location));
81915974
VS
92 return (url.GetError() == wxURL_NOERR);
93 }
34e0d9f8 94#endif
a62848fd 95 return false;
5526e819
VS
96}
97
98
3caa34b7
VS
99wxFSFile* wxInternetFSHandler::OpenFile(wxFileSystem& WXUNUSED(fs),
100 const wxString& location)
5526e819 101{
34e0d9f8
JS
102#if !wxUSE_URL
103 return NULL;
104#else
3caa34b7
VS
105 wxString right =
106 GetProtocol(location) + wxT(":") + StripProtocolAnchor(location);
5526e819 107
3caa34b7
VS
108 wxURL url(right);
109 if (url.GetError() == wxURL_NOERR)
324dbfec 110 {
3caa34b7
VS
111 wxInputStream *s = url.GetInputStream();
112 wxString content = url.GetProtocol().GetContentType();
113 if (content == wxEmptyString) content = GetMimeTypeFromExt(location);
114 if (s)
07d4b94c 115 {
3caa34b7
VS
116 wxString tmpfile =
117 wxFileName::CreateTempFileName(wxT("wxhtml"));
5526e819 118
3caa34b7
VS
119 { // now copy streams content to temporary file:
120 wxFileOutputStream sout(tmpfile);
121 s->Read(sout);
122 }
123 delete s;
a62848fd 124
3caa34b7
VS
125 return new wxFSFile(new wxTemporaryFileInputStream(tmpfile),
126 right,
127 content,
128 GetAnchor(location)
e2b87f38 129#if wxUSE_DATETIME
3caa34b7 130 , wxDateTime::Now()
e2b87f38
VZ
131#endif // wxUSE_DATETIME
132 );
3caa34b7 133 }
5526e819 134 }
3caa34b7
VS
135
136 return (wxFSFile*) NULL; // incorrect URL
34e0d9f8 137#endif
5526e819
VS
138}
139
3caa34b7 140
f61815af
GL
141class wxFileSystemInternetModule : public wxModule
142{
143 DECLARE_DYNAMIC_CLASS(wxFileSystemInternetModule)
144
145 public:
146 virtual bool OnInit()
147 {
148 wxFileSystem::AddHandler(new wxInternetFSHandler);
a62848fd 149 return true;
f61815af
GL
150 }
151 virtual void OnExit() {}
152};
153
154IMPLEMENT_DYNAMIC_CLASS(wxFileSystemInternetModule, wxModule)
155
24528b0c 156#endif // wxUSE_FILESYSTEM && wxUSE_FS_INET