]>
Commit | Line | Data |
---|---|---|
dcb86da0 | 1 | ///////////////////////////////////////////////////////////////////////////// |
0bca0373 | 2 | // Name: src/common/fs_mem.cpp |
dcb86da0 VS |
3 | // Purpose: in-memory file system |
4 | // Author: Vaclav Slavik | |
0bca0373 | 5 | // RCS-ID: $Id$ |
dcb86da0 | 6 | // Copyright: (c) 2000 Vaclav Slavik |
65571936 | 7 | // Licence: wxWindows licence |
dcb86da0 VS |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
dcb86da0 VS |
10 | #include "wx/wxprec.h" |
11 | ||
2b5f62a0 | 12 | #ifdef __BORLANDC__ |
0bca0373 | 13 | #pragma hdrstop |
dcb86da0 VS |
14 | #endif |
15 | ||
24528b0c | 16 | #if wxUSE_FILESYSTEM && wxUSE_STREAMS |
dcb86da0 | 17 | |
e2478fde VZ |
18 | #include "wx/fs_mem.h" |
19 | ||
b4f4d3dd | 20 | #ifndef WX_PRECOMP |
04dbb646 VZ |
21 | #include "wx/intl.h" |
22 | #include "wx/log.h" | |
8b70ca26 | 23 | #include "wx/hash.h" |
6b24b421 | 24 | #include "wx/wxcrtvararg.h" |
0bca0373 | 25 | #if wxUSE_GUI |
155ecd4c | 26 | #include "wx/image.h" |
0bca0373 | 27 | #endif // wxUSE_GUI |
dcb86da0 VS |
28 | #endif |
29 | ||
dcb86da0 VS |
30 | #include "wx/mstream.h" |
31 | ||
dcb86da0 VS |
32 | class MemFSHashObj : public wxObject |
33 | { | |
34 | public: | |
04dbb646 | 35 | |
c5d7b81e | 36 | MemFSHashObj(const void *data, size_t len, const wxString& mime) |
dcb86da0 VS |
37 | { |
38 | m_Data = new char[len]; | |
39 | memcpy(m_Data, data, len); | |
40 | m_Len = len; | |
c5d7b81e | 41 | m_MimeType = mime; |
e2b87f38 | 42 | InitTime(); |
dcb86da0 | 43 | } |
04dbb646 | 44 | |
c5d7b81e | 45 | MemFSHashObj(const wxMemoryOutputStream& stream, const wxString& mime) |
dcb86da0 VS |
46 | { |
47 | m_Len = stream.GetSize(); | |
48 | m_Data = new char[m_Len]; | |
49 | stream.CopyTo(m_Data, m_Len); | |
c5d7b81e | 50 | m_MimeType = mime; |
e2b87f38 | 51 | InitTime(); |
dcb86da0 | 52 | } |
04dbb646 | 53 | |
d3c7fc99 | 54 | virtual ~MemFSHashObj() |
dcb86da0 VS |
55 | { |
56 | delete[] m_Data; | |
57 | } | |
04dbb646 | 58 | |
dcb86da0 VS |
59 | char *m_Data; |
60 | size_t m_Len; | |
c5d7b81e | 61 | wxString m_MimeType; |
e2b87f38 | 62 | #if wxUSE_DATETIME |
dcb86da0 | 63 | wxDateTime m_Time; |
e2b87f38 | 64 | #endif // wxUSE_DATETIME |
22f3361e VZ |
65 | |
66 | DECLARE_NO_COPY_CLASS(MemFSHashObj) | |
e2b87f38 VZ |
67 | |
68 | private: | |
69 | void InitTime() | |
70 | { | |
71 | #if wxUSE_DATETIME | |
72 | m_Time = wxDateTime::Now(); | |
a62848fd | 73 | #endif // wxUSE_DATETIME |
e2b87f38 | 74 | } |
dcb86da0 VS |
75 | }; |
76 | ||
ec67cff1 | 77 | #if wxUSE_BASE |
60c0a8db | 78 | |
dcb86da0 VS |
79 | |
80 | //-------------------------------------------------------------------------------- | |
81 | // wxMemoryFSHandler | |
82 | //-------------------------------------------------------------------------------- | |
83 | ||
84 | ||
e2478fde | 85 | wxHashTable *wxMemoryFSHandlerBase::m_Hash = NULL; |
dcb86da0 VS |
86 | |
87 | ||
e2478fde | 88 | wxMemoryFSHandlerBase::wxMemoryFSHandlerBase() : wxFileSystemHandler() |
dcb86da0 | 89 | { |
dcb86da0 VS |
90 | } |
91 | ||
92 | ||
93 | ||
e2478fde | 94 | wxMemoryFSHandlerBase::~wxMemoryFSHandlerBase() |
dcb86da0 VS |
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.) | |
04dbb646 | 99 | |
e2478fde VZ |
100 | if (m_Hash) |
101 | { | |
df5168c4 | 102 | WX_CLEAR_HASH_TABLE(*m_Hash); |
e2478fde VZ |
103 | delete m_Hash; |
104 | m_Hash = NULL; | |
105 | } | |
dcb86da0 VS |
106 | } |
107 | ||
108 | ||
109 | ||
e2478fde | 110 | bool wxMemoryFSHandlerBase::CanOpen(const wxString& location) |
dcb86da0 VS |
111 | { |
112 | wxString p = GetProtocol(location); | |
113 | return (p == wxT("memory")); | |
114 | } | |
115 | ||
116 | ||
117 | ||
118 | ||
e2478fde | 119 | wxFSFile* wxMemoryFSHandlerBase::OpenFile(wxFileSystem& WXUNUSED(fs), const wxString& location) |
dcb86da0 VS |
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, | |
c5d7b81e | 127 | obj->m_MimeType, |
e2b87f38 VZ |
128 | GetAnchor(location) |
129 | #if wxUSE_DATETIME | |
130 | , obj -> m_Time | |
a62848fd | 131 | #endif // wxUSE_DATETIME |
e2b87f38 | 132 | ); |
dcb86da0 VS |
133 | } |
134 | else return NULL; | |
135 | } | |
136 | ||
137 | ||
138 | ||
e2478fde | 139 | wxString wxMemoryFSHandlerBase::FindFirst(const wxString& WXUNUSED(spec), |
bc1dcfc1 | 140 | int WXUNUSED(flags)) |
dcb86da0 | 141 | { |
e2478fde | 142 | wxFAIL_MSG(wxT("wxMemoryFSHandlerBase::FindFirst not implemented")); |
bc1dcfc1 | 143 | |
dcb86da0 VS |
144 | return wxEmptyString; |
145 | } | |
146 | ||
147 | ||
148 | ||
e2478fde | 149 | wxString wxMemoryFSHandlerBase::FindNext() |
dcb86da0 | 150 | { |
e2478fde | 151 | wxFAIL_MSG(wxT("wxMemoryFSHandlerBase::FindNext not implemented")); |
bc1dcfc1 | 152 | |
dcb86da0 VS |
153 | return wxEmptyString; |
154 | } | |
155 | ||
156 | ||
e2478fde | 157 | bool wxMemoryFSHandlerBase::CheckHash(const wxString& filename) |
dcb86da0 | 158 | { |
04dbb646 | 159 | if (m_Hash == NULL) |
dcb86da0 VS |
160 | { |
161 | m_Hash = new wxHashTable(wxKEY_STRING); | |
dcb86da0 | 162 | } |
04dbb646 | 163 | |
dcb86da0 VS |
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); | |
a62848fd | 169 | return false; |
dcb86da0 VS |
170 | } |
171 | else | |
a62848fd | 172 | return true; |
dcb86da0 VS |
173 | } |
174 | ||
175 | ||
c5d7b81e VS |
176 | /*static*/ |
177 | void wxMemoryFSHandlerBase::AddFileWithMimeType(const wxString& filename, | |
178 | const wxString& textdata, | |
179 | const wxString& mimetype) | |
dcb86da0 | 180 | { |
c5d7b81e VS |
181 | AddFileWithMimeType(filename, |
182 | (const void*) textdata.mb_str(), textdata.length(), | |
183 | mimetype); | |
dcb86da0 VS |
184 | } |
185 | ||
186 | ||
c5d7b81e VS |
187 | /*static*/ |
188 | void wxMemoryFSHandlerBase::AddFileWithMimeType(const wxString& filename, | |
189 | const void *binarydata, size_t size, | |
190 | const wxString& mimetype) | |
dcb86da0 VS |
191 | { |
192 | if (!CheckHash(filename)) return; | |
c5d7b81e VS |
193 | m_Hash -> Put(filename, new MemFSHashObj(binarydata, size, mimetype)); |
194 | } | |
195 | ||
196 | /*static*/ | |
197 | void wxMemoryFSHandlerBase::AddFile(const wxString& filename, | |
198 | const wxString& textdata) | |
199 | { | |
200 | AddFileWithMimeType(filename, textdata, wxEmptyString); | |
201 | } | |
202 | ||
203 | ||
204 | /*static*/ | |
205 | void wxMemoryFSHandlerBase::AddFile(const wxString& filename, | |
206 | const void *binarydata, size_t size) | |
207 | { | |
208 | AddFileWithMimeType(filename, binarydata, size, wxEmptyString); | |
dcb86da0 VS |
209 | } |
210 | ||
211 | ||
212 | ||
e2478fde | 213 | /*static*/ void wxMemoryFSHandlerBase::RemoveFile(const wxString& filename) |
dcb86da0 VS |
214 | { |
215 | if (m_Hash == NULL || | |
216 | m_Hash -> Get(filename) == NULL) | |
217 | { | |
218 | wxString s; | |
219 | s.Printf(_("Trying to remove file '%s' from memory VFS, but it is not loaded!"), filename.c_str()); | |
220 | wxLogError(s); | |
221 | } | |
222 | ||
223 | else | |
224 | delete m_Hash -> Delete(filename); | |
225 | } | |
226 | ||
ec67cff1 | 227 | #endif // wxUSE_BASE |
e2478fde VZ |
228 | |
229 | #if wxUSE_GUI | |
230 | ||
2de5a6ee VS |
231 | #if wxUSE_IMAGE |
232 | /*static*/ void | |
989a2421 VZ |
233 | wxMemoryFSHandler::AddFile(const wxString& filename, |
234 | const wxImage& image, | |
d75a69e8 | 235 | wxBitmapType type) |
2de5a6ee VS |
236 | { |
237 | if (!CheckHash(filename)) return; | |
238 | ||
2de5a6ee | 239 | wxMemoryOutputStream mems; |
d75a69e8 | 240 | if (image.Ok() && image.SaveFile(mems, type)) |
c5d7b81e VS |
241 | { |
242 | m_Hash->Put | |
243 | ( | |
244 | filename, | |
245 | new MemFSHashObj | |
246 | ( | |
247 | mems, | |
248 | wxImage::FindHandler(type)->GetMimeType() | |
249 | ) | |
250 | ); | |
251 | } | |
2de5a6ee VS |
252 | else |
253 | { | |
254 | wxString s; | |
255 | s.Printf(_("Failed to store image '%s' to memory VFS!"), filename.c_str()); | |
256 | wxPrintf(wxT("'%s'\n"), s.c_str()); | |
257 | wxLogError(s); | |
258 | } | |
259 | } | |
2de5a6ee | 260 | |
989a2421 VZ |
261 | /*static*/ void |
262 | wxMemoryFSHandler::AddFile(const wxString& filename, | |
263 | const wxBitmap& bitmap, | |
d75a69e8 | 264 | wxBitmapType type) |
e2478fde | 265 | { |
64c288fa | 266 | #if !defined(__WXMSW__) || wxUSE_WXDIB |
e2478fde VZ |
267 | wxImage img = bitmap.ConvertToImage(); |
268 | AddFile(filename, img, type); | |
64c288fa | 269 | #endif |
e2478fde VZ |
270 | } |
271 | ||
1904aa72 DS |
272 | #endif // wxUSE_IMAGE |
273 | ||
274 | #endif // wxUSE_GUI | |
dcb86da0 VS |
275 | |
276 | ||
24528b0c | 277 | #endif // wxUSE_FILESYSTEM && wxUSE_FS_ZIP |