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