]> git.saurik.com Git - wxWidgets.git/blob - src/common/fs_mem.cpp
no changes, just some minor cleanup
[wxWidgets.git] / src / common / fs_mem.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/fs_mem.cpp
3 // Purpose: in-memory file system
4 // Author: Vaclav Slavik
5 // RCS-ID: $Id$
6 // Copyright: (c) 2000 Vaclav Slavik
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #include "wx/wxprec.h"
11
12 #ifdef __BORLANDC__
13 #pragma hdrstop
14 #endif
15
16 #if wxUSE_FILESYSTEM && wxUSE_STREAMS
17
18 #include "wx/fs_mem.h"
19
20 #ifndef WX_PRECOMP
21 #include "wx/intl.h"
22 #include "wx/log.h"
23 #include "wx/hash.h"
24 #include "wx/wxcrtvararg.h"
25 #if wxUSE_GUI
26 #include "wx/image.h"
27 #endif // wxUSE_GUI
28 #endif
29
30 #include "wx/mstream.h"
31
32 class MemFSHashObj : public wxObject
33 {
34 public:
35 MemFSHashObj(const void *data, size_t len, const wxString& mime)
36 {
37 m_Data = new char[len];
38 memcpy(m_Data, data, len);
39 m_Len = len;
40 m_MimeType = mime;
41 InitTime();
42 }
43
44 MemFSHashObj(const wxMemoryOutputStream& stream, const wxString& mime)
45 {
46 m_Len = stream.GetSize();
47 m_Data = new char[m_Len];
48 stream.CopyTo(m_Data, m_Len);
49 m_MimeType = mime;
50 InitTime();
51 }
52
53 virtual ~MemFSHashObj()
54 {
55 delete[] m_Data;
56 }
57
58 char *m_Data;
59 size_t m_Len;
60 wxString m_MimeType;
61 #if wxUSE_DATETIME
62 wxDateTime m_Time;
63 #endif // wxUSE_DATETIME
64
65 DECLARE_NO_COPY_CLASS(MemFSHashObj)
66
67 private:
68 void InitTime()
69 {
70 #if wxUSE_DATETIME
71 m_Time = wxDateTime::Now();
72 #endif // wxUSE_DATETIME
73 }
74 };
75
76 #if wxUSE_BASE
77
78
79 //--------------------------------------------------------------------------------
80 // wxMemoryFSHandler
81 //--------------------------------------------------------------------------------
82
83
84 wxHashTable *wxMemoryFSHandlerBase::m_Hash = NULL;
85
86
87 wxMemoryFSHandlerBase::wxMemoryFSHandlerBase() : wxFileSystemHandler()
88 {
89 }
90
91 wxMemoryFSHandlerBase::~wxMemoryFSHandlerBase()
92 {
93 // as only one copy of FS handler is supposed to exist, we may silently
94 // delete static data here. (There is no way how to remove FS handler from
95 // wxFileSystem other than releasing _all_ handlers.)
96
97 if (m_Hash)
98 {
99 WX_CLEAR_HASH_TABLE(*m_Hash);
100 delete m_Hash;
101 m_Hash = NULL;
102 }
103 }
104
105 bool wxMemoryFSHandlerBase::CanOpen(const wxString& location)
106 {
107 return GetProtocol(location) == "memory";
108 }
109
110 wxFSFile * wxMemoryFSHandlerBase::OpenFile(wxFileSystem& WXUNUSED(fs),
111 const wxString& location)
112 {
113 if ( !m_Hash )
114 return NULL;
115
116 MemFSHashObj *obj = (MemFSHashObj*) m_Hash->Get(GetRightLocation(location));
117 if ( !obj )
118 return NULL;
119
120 else return new wxFSFile(new wxMemoryInputStream(obj->m_Data, obj->m_Len),
121 location,
122 obj->m_MimeType,
123 GetAnchor(location)
124 #if wxUSE_DATETIME
125 , obj->m_Time
126 #endif // wxUSE_DATETIME
127 );
128 }
129
130 wxString wxMemoryFSHandlerBase::FindFirst(const wxString& WXUNUSED(spec),
131 int WXUNUSED(flags))
132 {
133 wxFAIL_MSG(wxT("wxMemoryFSHandlerBase::FindFirst not implemented"));
134
135 return wxEmptyString;
136 }
137
138 wxString wxMemoryFSHandlerBase::FindNext()
139 {
140 wxFAIL_MSG(wxT("wxMemoryFSHandlerBase::FindNext not implemented"));
141
142 return wxEmptyString;
143 }
144
145 bool wxMemoryFSHandlerBase::CheckHash(const wxString& filename)
146 {
147 if ( !m_Hash )
148 {
149 m_Hash = new wxHashTable(wxKEY_STRING);
150 }
151
152 if ( m_Hash->Get(filename) )
153 {
154 wxLogError(_("Memory VFS already contains file '%s'!"), filename);
155 return false;
156 }
157
158 return true;
159 }
160
161
162 /*static*/
163 void wxMemoryFSHandlerBase::AddFileWithMimeType(const wxString& filename,
164 const wxString& textdata,
165 const wxString& mimetype)
166 {
167 AddFileWithMimeType(filename,
168 (const void*) textdata.mb_str(), textdata.length(),
169 mimetype);
170 }
171
172
173 /*static*/
174 void wxMemoryFSHandlerBase::AddFileWithMimeType(const wxString& filename,
175 const void *binarydata, size_t size,
176 const wxString& mimetype)
177 {
178 if ( !CheckHash(filename) )
179 return;
180
181 m_Hash->Put(filename, new MemFSHashObj(binarydata, size, mimetype));
182 }
183
184 /*static*/
185 void wxMemoryFSHandlerBase::AddFile(const wxString& filename,
186 const wxString& textdata)
187 {
188 AddFileWithMimeType(filename, textdata, wxEmptyString);
189 }
190
191
192 /*static*/
193 void wxMemoryFSHandlerBase::AddFile(const wxString& filename,
194 const void *binarydata, size_t size)
195 {
196 AddFileWithMimeType(filename, binarydata, size, wxEmptyString);
197 }
198
199
200
201 /*static*/ void wxMemoryFSHandlerBase::RemoveFile(const wxString& filename)
202 {
203 if ( !m_Hash || !m_Hash->Get(filename) )
204 {
205 wxLogError(_("Trying to remove file '%s' from memory VFS, "
206 "but it is not loaded!"),
207 filename);
208 }
209 else
210 {
211 delete m_Hash->Delete(filename);
212 }
213 }
214
215 #endif // wxUSE_BASE
216
217 #if wxUSE_GUI
218
219 #if wxUSE_IMAGE
220 /*static*/ void
221 wxMemoryFSHandler::AddFile(const wxString& filename,
222 const wxImage& image,
223 wxBitmapType type)
224 {
225 if ( !CheckHash(filename) )
226 return;
227
228 wxMemoryOutputStream mems;
229 if (image.Ok() && image.SaveFile(mems, type))
230 {
231 m_Hash->Put
232 (
233 filename,
234 new MemFSHashObj
235 (
236 mems,
237 wxImage::FindHandler(type)->GetMimeType()
238 )
239 );
240 }
241 else
242 {
243 wxLogError(_("Failed to store image '%s' to memory VFS!"), filename);
244 }
245 }
246
247 /*static*/ void
248 wxMemoryFSHandler::AddFile(const wxString& filename,
249 const wxBitmap& bitmap,
250 wxBitmapType type)
251 {
252 #if !defined(__WXMSW__) || wxUSE_WXDIB
253 wxImage img = bitmap.ConvertToImage();
254 AddFile(filename, img, type);
255 #endif
256 }
257
258 #endif // wxUSE_IMAGE
259
260 #endif // wxUSE_GUI
261
262
263 #endif // wxUSE_FILESYSTEM && wxUSE_FS_ZIP