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