]> git.saurik.com Git - wxWidgets.git/blame - src/common/fs_zip.cpp
Pressing build-in joystick on WinCE phones fires wxEVT_JOY_BUTTON_DOWN event.
[wxWidgets.git] / src / common / fs_zip.cpp
CommitLineData
5526e819
VS
1/////////////////////////////////////////////////////////////////////////////
2// Name: fs_zip.cpp
3// Purpose: ZIP file system
4// Author: Vaclav Slavik
5// Copyright: (c) 1999 Vaclav Slavik
de0702d0 6// CVS-ID: $Id$
65571936 7// Licence: wxWindows licence
5526e819
VS
8/////////////////////////////////////////////////////////////////////////////
9
3096bd2f 10#include "wx/wxprec.h"
5526e819 11
2b5f62a0 12#ifdef __BORLANDC__
5526e819
VS
13#pragma hdrstop
14#endif
15
9777274a 16#if wxUSE_FILESYSTEM && wxUSE_FS_ZIP && wxUSE_ZIPSTREAM && wxUSE_ZLIB
e3e717ec 17
5526e819 18#ifndef WXPRECOMP
04dbb646
VZ
19 #include "wx/intl.h"
20 #include "wx/log.h"
5526e819
VS
21#endif
22
23#include "wx/filesys.h"
00375592 24#include "wx/wfstream.h"
6001e347 25#include "wx/zipstrm.h"
5526e819
VS
26#include "wx/fs_zip.h"
27
aaa66113 28
08335000
MW
29//---------------------------------------------------------------------------
30// wxZipFSInputStream
31//---------------------------------------------------------------------------
32// Helper class for wxZipFSHandler
33
34class wxZipFSInputStream : public wxZipInputStream
35{
36 public:
37 wxZipFSInputStream(wxFSFile *file)
38 : wxZipInputStream(*file->GetStream())
39 {
40 m_file = file;
41#if 1 //WXWIN_COMPATIBILITY_2_6
42 m_allowSeeking = true;
43#endif
44 }
45
46 virtual ~wxZipFSInputStream() { delete m_file; }
47
48 private:
49 wxFSFile *m_file;
50};
51
ddaf5566 52//----------------------------------------------------------------------------
5526e819 53// wxZipFSHandler
ddaf5566 54//----------------------------------------------------------------------------
5526e819 55
aaa66113
VS
56wxZipFSHandler::wxZipFSHandler() : wxFileSystemHandler()
57{
58 m_Archive = NULL;
59 m_ZipFile = m_Pattern = m_BaseDir = wxEmptyString;
a62848fd 60 m_AllowDirs = m_AllowFiles = true;
de0702d0 61 m_DirsFound = NULL;
aaa66113
VS
62}
63
64
65
66wxZipFSHandler::~wxZipFSHandler()
67{
f60b1d82 68 Cleanup();
aaa66113
VS
69}
70
71
f60b1d82
VZ
72void wxZipFSHandler::Cleanup()
73{
74 wxDELETE(m_Archive);
75 wxDELETE(m_DirsFound);
76}
77
78
aaa66113 79
5526e819
VS
80bool wxZipFSHandler::CanOpen(const wxString& location)
81{
82 wxString p = GetProtocol(location);
08335000 83 return (p == wxT("zip"));
5526e819
VS
84}
85
86
ba75c6bb 87wxFSFile* wxZipFSHandler::OpenFile(wxFileSystem& WXUNUSED(fs), const wxString& location)
5526e819
VS
88{
89 wxString right = GetRightLocation(location);
90 wxString left = GetLeftLocation(location);
08335000 91 wxZipInputStream *s;
5526e819 92
ddaf5566
VS
93 if (right.Contains(wxT("./")))
94 {
95 if (right.GetChar(0) != wxT('/')) right = wxT('/') + right;
96 wxFileName rightPart(right, wxPATH_UNIX);
97 rightPart.Normalize(wxPATH_NORM_DOTS, wxT("/"), wxPATH_UNIX);
98 right = rightPart.GetFullPath(wxPATH_UNIX);
99 }
a62848fd 100
505710ca
KB
101 if (right.GetChar(0) == wxT('/')) right = right.Mid(1);
102
ba75c6bb
MW
103 // a new wxFileSystem object is needed here to avoid infinite recursion
104 wxFSFile *leftFile = wxFileSystem().OpenFile(left);
08335000
MW
105 if (!leftFile)
106 return NULL;
2b5f62a0 107
08335000
MW
108 s = new wxZipFSInputStream(leftFile);
109 if (s && s->IsOk())
095472c0 110 {
08335000 111 bool found = false;
49178e65
WS
112 while (!found)
113 {
114 wxZipEntry *ent = s->GetNextEntry();
115 if (!ent)
116 break;
08335000
MW
117 if (ent->GetInternalName() == right)
118 found = true;
119 delete ent;
120 }
121 if (found)
122 return new wxFSFile(s,
58c837a4 123 left + wxT("#zip:") + right,
5526e819 124 GetMimeTypeFromExt(location),
e2b87f38
VZ
125 GetAnchor(location)
126#if wxUSE_DATETIME
127 , wxDateTime(wxFileModificationTime(left))
128#endif // wxUSE_DATETIME
129 );
5526e819 130 }
3ca6a5f0
BP
131
132 delete s;
133 return NULL;
5526e819
VS
134}
135
136
137
aaa66113
VS
138wxString wxZipFSHandler::FindFirst(const wxString& spec, int flags)
139{
140 wxString right = GetRightLocation(spec);
141 wxString left = GetLeftLocation(spec);
505710ca 142
ba75c6bb 143 if (!right.empty() && right.Last() == wxT('/')) right.RemoveLast();
aaa66113 144
505710ca 145 if (m_Archive)
095472c0 146 {
08335000 147 delete m_Archive;
aaa66113
VS
148 m_Archive = NULL;
149 }
150
505710ca 151 switch (flags)
095472c0 152 {
505710ca 153 case wxFILE:
a62848fd 154 m_AllowDirs = false, m_AllowFiles = true; break;
505710ca 155 case wxDIR:
a62848fd 156 m_AllowDirs = true, m_AllowFiles = false; break;
505710ca 157 default:
a62848fd 158 m_AllowDirs = m_AllowFiles = true; break;
aaa66113
VS
159 }
160
161 m_ZipFile = left;
e06cac58 162
08335000
MW
163 wxFSFile *leftFile = wxFileSystem().OpenFile(left);
164 if (leftFile)
165 m_Archive = new wxZipFSInputStream(leftFile);
e06cac58 166
aaa66113
VS
167 m_Pattern = right.AfterLast(wxT('/'));
168 m_BaseDir = right.BeforeLast(wxT('/'));
ba75c6bb
MW
169 if (m_BaseDir.StartsWith(wxT("/")))
170 m_BaseDir = m_BaseDir.Mid(1);
aaa66113 171
505710ca 172 if (m_Archive)
095472c0 173 {
00375592 174 if (m_AllowDirs)
de0702d0 175 {
00375592 176 delete m_DirsFound;
62d9eab6 177 m_DirsFound = new wxZipFilenameHashMap();
ba75c6bb
MW
178 if (right.empty()) // allow "/" to match the archive root
179 return spec;
de0702d0 180 }
00375592 181 return DoFind();
aaa66113 182 }
095472c0 183 return wxEmptyString;
aaa66113
VS
184}
185
186
187
188wxString wxZipFSHandler::FindNext()
5526e819 189{
aaa66113
VS
190 if (!m_Archive) return wxEmptyString;
191 return DoFind();
5526e819
VS
192}
193
aaa66113
VS
194
195
196wxString wxZipFSHandler::DoFind()
197{
de0702d0 198 wxString namestr, dir, filename;
aaa66113 199 wxString match = wxEmptyString;
aaa66113
VS
200
201 while (match == wxEmptyString)
202 {
00375592
VZ
203 wxZipEntry *entry = m_Archive->GetNextEntry();
204 if (!entry)
205 {
08335000 206 delete m_Archive;
00375592
VZ
207 m_Archive = NULL;
208 break;
209 }
210 namestr = entry->GetName(wxPATH_UNIX);
211 delete entry;
aaa66113 212
de0702d0 213 if (m_AllowDirs)
095472c0 214 {
505710ca 215 dir = namestr.BeforeLast(wxT('/'));
489f6cf7 216 while (!dir.empty())
de0702d0 217 {
62d9eab6 218 if( m_DirsFound->find(dir) == m_DirsFound->end() )
de0702d0 219 {
62d9eab6 220 (*m_DirsFound)[dir] = 1;
de0702d0
VS
221 filename = dir.AfterLast(wxT('/'));
222 dir = dir.BeforeLast(wxT('/'));
489f6cf7 223 if (!filename.empty() && m_BaseDir == dir &&
a62848fd 224 wxMatchWild(m_Pattern, filename, false))
de0702d0
VS
225 match = m_ZipFile + wxT("#zip:") + dir + wxT("/") + filename;
226 }
505710ca 227 else
de0702d0
VS
228 break; // already tranversed
229 }
aaa66113 230 }
de0702d0
VS
231
232 filename = namestr.AfterLast(wxT('/'));
233 dir = namestr.BeforeLast(wxT('/'));
489f6cf7 234 if (m_AllowFiles && !filename.empty() && m_BaseDir == dir &&
a62848fd 235 wxMatchWild(m_Pattern, filename, false))
de0702d0 236 match = m_ZipFile + wxT("#zip:") + namestr;
aaa66113 237 }
505710ca 238
aaa66113
VS
239 return match;
240}
241
242
243
505710ca 244#endif
24528b0c 245 //wxUSE_FILESYSTEM && wxUSE_FS_ZIP && wxUSE_ZIPSTREAM