]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/common/fs_zip.cpp
compilation fixes
[wxWidgets.git] / src / common / fs_zip.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: fs_zip.cpp
3// Purpose: ZIP file system
4// Author: Vaclav Slavik
5// Copyright: (c) 1999 Vaclav Slavik
6// CVS-ID: $Id$
7// Licence: wxWindows Licence
8/////////////////////////////////////////////////////////////////////////////
9
10
11
12#ifdef __GNUG__
13#pragma implementation "fs_zip.h"
14#endif
15
16#include "wx/wxprec.h"
17
18#ifdef __BORDLANDC__
19#pragma hdrstop
20#endif
21
22#if wxUSE_FILESYSTEM && wxUSE_FS_ZIP && wxUSE_ZIPSTREAM
23
24#ifndef WXPRECOMP
25#include "wx/wx.h"
26#endif
27
28#include "wx/hash.h"
29#include "wx/filesys.h"
30#include "wx/zipstrm.h"
31#include "wx/fs_zip.h"
32
33/* Not the right solution (paths in makefiles) but... */
34#ifdef __BORLANDC__
35#include "../common/unzip.h"
36#else
37#include "unzip.h"
38#endif
39
40
41//--------------------------------------------------------------------------------
42// wxZipFSHandler
43//--------------------------------------------------------------------------------
44
45
46
47wxZipFSHandler::wxZipFSHandler() : wxFileSystemHandler()
48{
49 m_Archive = NULL;
50 m_ZipFile = m_Pattern = m_BaseDir = wxEmptyString;
51 m_AllowDirs = m_AllowFiles = TRUE;
52 m_DirsFound = NULL;
53}
54
55
56
57wxZipFSHandler::~wxZipFSHandler()
58{
59 if (m_Archive)
60 unzClose((unzFile)m_Archive);
61 if (m_DirsFound)
62 delete m_DirsFound;
63}
64
65
66
67bool wxZipFSHandler::CanOpen(const wxString& location)
68{
69 wxString p = GetProtocol(location);
70 return (p == wxT("zip")) &&
71 (GetProtocol(GetLeftLocation(location)) == wxT("file"));
72}
73
74
75
76
77wxFSFile* wxZipFSHandler::OpenFile(wxFileSystem& WXUNUSED(fs), const wxString& location)
78{
79 wxString right = GetRightLocation(location);
80 wxString left = GetLeftLocation(location);
81 wxInputStream *s;
82
83 if (GetProtocol(left) != wxT("file"))
84 {
85 wxLogError(_("ZIP handler currently supports only local files!"));
86 return NULL;
87 }
88
89 s = new wxZipInputStream(left, right);
90 if (s && (s->LastError() == wxStream_NOERROR))
91 {
92 return new wxFSFile(s,
93 left + wxT("#zip:") + right,
94 GetMimeTypeFromExt(location),
95 GetAnchor(location),
96 wxDateTime(wxFileModificationTime(left)));
97 }
98
99 delete s;
100 return NULL;
101}
102
103
104
105wxString wxZipFSHandler::FindFirst(const wxString& spec, int flags)
106{
107 wxString right = GetRightLocation(spec);
108 wxString left = GetLeftLocation(spec);
109
110 if (right.Last() == wxT('/')) right.RemoveLast();
111
112 if (m_Archive)
113 {
114 unzClose((unzFile)m_Archive);
115 m_Archive = NULL;
116 }
117
118 if (GetProtocol(left) != wxT("file"))
119 {
120 wxLogError(_("ZIP handler currently supports only local files!"));
121 return wxEmptyString;
122 }
123
124 switch (flags)
125 {
126 case wxFILE:
127 m_AllowDirs = FALSE, m_AllowFiles = TRUE; break;
128 case wxDIR:
129 m_AllowDirs = TRUE, m_AllowFiles = FALSE; break;
130 default:
131 m_AllowDirs = m_AllowFiles = TRUE; break;
132 }
133
134 m_ZipFile = left;
135 m_Archive = (void*) unzOpen(m_ZipFile.mb_str());
136 m_Pattern = right.AfterLast(wxT('/'));
137 m_BaseDir = right.BeforeLast(wxT('/'));
138
139 if (m_Archive)
140 {
141 if (unzGoToFirstFile((unzFile)m_Archive) != UNZ_OK)
142 {
143 unzClose((unzFile)m_Archive);
144 m_Archive = NULL;
145 }
146 else
147 {
148 if (m_AllowDirs)
149 {
150 delete m_DirsFound;
151 m_DirsFound = new wxHashTableLong();
152 }
153 return DoFind();
154 }
155 }
156 return wxEmptyString;
157}
158
159
160
161wxString wxZipFSHandler::FindNext()
162{
163 if (!m_Archive) return wxEmptyString;
164 return DoFind();
165}
166
167
168
169wxString wxZipFSHandler::DoFind()
170{
171 static char namebuf[1024]; // char, not wxChar!
172 char *c;
173 wxString namestr, dir, filename;
174 wxString match = wxEmptyString;
175
176 while (match == wxEmptyString)
177 {
178 unzGetCurrentFileInfo((unzFile)m_Archive, NULL, namebuf, 1024, NULL, 0, NULL, 0);
179 for (c = namebuf; *c; c++) if (*c == wxT('\\')) *c = wxT('/');
180 namestr = namebuf;
181
182 if (m_AllowDirs)
183 {
184 dir = namestr.BeforeLast(wxT('/'));
185 while (!dir.IsEmpty())
186 {
187 long key = 0;
188 for (size_t i = 0; i < dir.Length(); i++) key += (wxUChar)dir[i];
189 if (m_DirsFound->Get(key) == wxNOT_FOUND)
190 {
191 m_DirsFound->Put(key, 1);
192 filename = dir.AfterLast(wxT('/'));
193 dir = dir.BeforeLast(wxT('/'));
194 if (!filename.IsEmpty() && m_BaseDir == dir &&
195 wxMatchWild(m_Pattern, filename, FALSE))
196 match = m_ZipFile + wxT("#zip:") + dir + wxT("/") + filename;
197 }
198 else
199 break; // already tranversed
200 }
201 }
202
203 filename = namestr.AfterLast(wxT('/'));
204 dir = namestr.BeforeLast(wxT('/'));
205 if (m_AllowFiles && !filename.IsEmpty() && m_BaseDir == dir &&
206 wxMatchWild(m_Pattern, filename, FALSE))
207 match = m_ZipFile + wxT("#zip:") + namestr;
208
209 if (unzGoToNextFile((unzFile)m_Archive) != UNZ_OK)
210 {
211 unzClose((unzFile)m_Archive);
212 m_Archive = NULL;
213 break;
214 }
215 }
216
217 return match;
218}
219
220
221
222#endif
223 //wxUSE_FILESYSTEM && wxUSE_FS_ZIP && wxUSE_ZIPSTREAM