]> git.saurik.com Git - wxWidgets.git/blob - src/common/fs_zip.cpp
minor fixes to wxLocale::GetSystemLanguage(), test code for it added to the console...
[wxWidgets.git] / src / common / fs_zip.cpp
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
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
47 wxZipFSHandler::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
57 wxZipFSHandler::~wxZipFSHandler()
58 {
59 if (m_Archive)
60 unzClose((unzFile)m_Archive);
61 if (m_DirsFound)
62 delete m_DirsFound;
63 }
64
65
66
67 bool wxZipFSHandler::CanOpen(const wxString& location)
68 {
69 wxString p = GetProtocol(location);
70 return (p == wxT("zip"));
71 }
72
73
74
75
76 wxFSFile* wxZipFSHandler::OpenFile(wxFileSystem& WXUNUSED(fs), const wxString& location)
77 {
78 wxString right = GetRightLocation(location);
79 wxString left = GetLeftLocation(location);
80 wxInputStream *s;
81
82 if (GetProtocol(left) != wxT("file"))
83 {
84 wxLogError(_("ZIP handler currently supports only local files!"));
85 return NULL;
86 }
87
88 s = new wxZipInputStream(left, right);
89 if (s && (s->LastError() == wxStream_NOERROR))
90 {
91 return new wxFSFile(s,
92 left + wxT("#zip:") + right,
93 GetMimeTypeFromExt(location),
94 GetAnchor(location),
95 wxDateTime(wxFileModificationTime(left)));
96 }
97
98 delete s;
99 return NULL;
100 }
101
102
103
104 wxString wxZipFSHandler::FindFirst(const wxString& spec, int flags)
105 {
106 wxString right = GetRightLocation(spec);
107 wxString left = GetLeftLocation(spec);
108
109 if (right.Last() == wxT('/')) right.RemoveLast();
110
111 if (m_Archive)
112 {
113 unzClose((unzFile)m_Archive);
114 m_Archive = NULL;
115 }
116
117 if (GetProtocol(left) != wxT("file"))
118 {
119 wxLogError(_("ZIP handler currently supports only local files!"));
120 return wxEmptyString;
121 }
122
123 switch (flags)
124 {
125 case wxFILE:
126 m_AllowDirs = FALSE, m_AllowFiles = TRUE; break;
127 case wxDIR:
128 m_AllowDirs = TRUE, m_AllowFiles = FALSE; break;
129 default:
130 m_AllowDirs = m_AllowFiles = TRUE; break;
131 }
132
133 m_ZipFile = left;
134 m_Archive = (void*) unzOpen(m_ZipFile.mb_str());
135 m_Pattern = right.AfterLast(wxT('/'));
136 m_BaseDir = right.BeforeLast(wxT('/'));
137
138 if (m_Archive)
139 {
140 if (unzGoToFirstFile((unzFile)m_Archive) != UNZ_OK)
141 {
142 unzClose((unzFile)m_Archive);
143 m_Archive = NULL;
144 }
145 else
146 {
147 if (m_AllowDirs)
148 {
149 delete m_DirsFound;
150 m_DirsFound = new wxHashTableLong();
151 }
152 return DoFind();
153 }
154 }
155 return wxEmptyString;
156 }
157
158
159
160 wxString wxZipFSHandler::FindNext()
161 {
162 if (!m_Archive) return wxEmptyString;
163 return DoFind();
164 }
165
166
167
168 wxString wxZipFSHandler::DoFind()
169 {
170 static char namebuf[1024]; // char, not wxChar!
171 char *c;
172 wxString namestr, dir, filename;
173 wxString match = wxEmptyString;
174
175 while (match == wxEmptyString)
176 {
177 unzGetCurrentFileInfo((unzFile)m_Archive, NULL, namebuf, 1024, NULL, 0, NULL, 0);
178 for (c = namebuf; *c; c++) if (*c == wxT('\\')) *c = wxT('/');
179 namestr = namebuf;
180
181 if (m_AllowDirs)
182 {
183 dir = namestr.BeforeLast(wxT('/'));
184 while (!dir.IsEmpty())
185 {
186 long key = 0;
187 for (size_t i = 0; i < dir.Length(); i++) key += (wxUChar)dir[i];
188 if (m_DirsFound->Get(key) == wxNOT_FOUND)
189 {
190 m_DirsFound->Put(key, 1);
191 filename = dir.AfterLast(wxT('/'));
192 dir = dir.BeforeLast(wxT('/'));
193 if (!filename.IsEmpty() && m_BaseDir == dir &&
194 wxMatchWild(m_Pattern, filename, FALSE))
195 match = m_ZipFile + wxT("#zip:") + dir + wxT("/") + filename;
196 }
197 else
198 break; // already tranversed
199 }
200 }
201
202 filename = namestr.AfterLast(wxT('/'));
203 dir = namestr.BeforeLast(wxT('/'));
204 if (m_AllowFiles && !filename.IsEmpty() && m_BaseDir == dir &&
205 wxMatchWild(m_Pattern, filename, FALSE))
206 match = m_ZipFile + wxT("#zip:") + namestr;
207
208 if (unzGoToNextFile((unzFile)m_Archive) != UNZ_OK)
209 {
210 unzClose((unzFile)m_Archive);
211 m_Archive = NULL;
212 break;
213 }
214 }
215
216 return match;
217 }
218
219
220
221 #endif
222 //wxUSE_FILESYSTEM && wxUSE_FS_ZIP && wxUSE_ZIPSTREAM