]> git.saurik.com Git - wxWidgets.git/blame - src/common/webviewfilehandler.cpp
Fix typo in last commit
[wxWidgets.git] / src / common / webviewfilehandler.cpp
CommitLineData
9e3d4a32
SL
1/////////////////////////////////////////////////////////////////////////////
2// Name: webviewfilehandler.cpp
3// Purpose: Custom handler for the file scheme to allow archive browsing
4// Author: Steven Lamerton
5// Id: $Id$
6// Copyright: (c) 2011 Steven Lamerton
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
13#if wxUSE_WEB
14
15#if defined(__BORLANDC__)
16 #pragma hdrstop
17#endif
18
19#include "wx/webviewfilehandler.h"
20#include "wx/filesys.h"
9e3d4a32
SL
21
22//Taken from wx/filesys.cpp
23static wxString EscapeFileNameCharsInURL(const char *in)
24{
25 wxString s;
26
27 for ( const unsigned char *p = (const unsigned char*)in; *p; ++p )
28 {
29 const unsigned char c = *p;
30
31 if ( c == '/' || c == '-' || c == '.' || c == '_' || c == '~' ||
32 (c >= '0' && c <= '9') ||
33 (c >= 'a' && c <= 'z') ||
34 (c >= 'A' && c <= 'Z') )
35 {
36 s << c;
37 }
38 else
39 {
40 s << wxString::Format("%%%02x", c);
41 }
42 }
43
44 return s;
45}
46
47wxWebFileHandler::wxWebFileHandler()
48{
00b89a1d 49 m_name = "file";
9e3d4a32
SL
50 m_fileSystem = new wxFileSystem();
51}
52
53wxFSFile* wxWebFileHandler::GetFile(const wxString &uri)
54{
e6db07c3
SL
55 //If there is a fragment at the end of the path then we need to strip it
56 //off as not all backends do this for us
57 wxString path = uri;
58 size_t hashloc = uri.find('#');
59 if(hashloc != wxString::npos)
60 {
61 path = uri.substr(0, hashloc);
62 }
63
b7d3a622 64 //We iterate through the string to see if there is a protocol description
e6db07c3
SL
65 size_t start = wxString::npos;
66 for(size_t i = 0; i < path.length(); i++)
b7d3a622 67 {
e6db07c3 68 if(path[i] == ';' && path.substr(i, 10) == ";protocol=")
b7d3a622
SL
69 {
70 start = i;
71 break;
72 }
73 }
74
75 //We do not have a protocol string so we just pass the path withouth the
e6db07c3 76 if(start == wxString::npos)
9e3d4a32 77 {
e6db07c3 78 size_t doubleslash = path.find("//");
b7d3a622 79 //The path is incorrectly formed without // after the scheme
9e3d4a32
SL
80 if(doubleslash == wxString::npos)
81 return NULL;
82
83 wxString fspath = "file:" +
e6db07c3 84 EscapeFileNameCharsInURL(path.substr(doubleslash + 2));
9e3d4a32
SL
85 return m_fileSystem->OpenFile(fspath);
86 }
b7d3a622
SL
87 //Otherwise we need to extract the protocol
88 else
89 {
e6db07c3 90 size_t end = path.find('/', start);
b7d3a622
SL
91 //For the path to be valid there must to a path after the protocol
92 if(end == wxString::npos)
93 {
94 return NULL;
95 }
e6db07c3
SL
96 wxString mainpath = path.substr(0, start);
97 wxString archivepath = path.substr(end);
98 wxString protstring = path.substr(start, end - start);
b7d3a622
SL
99 wxString protocol = protstring.substr(10);
100 //We can now construct the correct path
e6db07c3 101 size_t doubleslash = path.find("//");
9e3d4a32
SL
102 //The path is incorrectly formed without // after the first protocol
103 if(doubleslash == wxString::npos)
104 return NULL;
105
106 wxString fspath = "file:" +
b7d3a622
SL
107 EscapeFileNameCharsInURL(mainpath.substr(doubleslash + 2))
108 + "#" + protocol +":" + archivepath;
9e3d4a32
SL
109 return m_fileSystem->OpenFile(fspath);
110 }
111}
112
9e3d4a32 113#endif // wxUSE_WEB