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