]>
Commit | Line | Data |
---|---|---|
dcb86da0 VS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: fs_mem.cpp | |
3 | // Purpose: in-memory file system | |
4 | // Author: Vaclav Slavik | |
5 | // Copyright: (c) 2000 Vaclav Slavik | |
6 | // Licence: wxWindows Licence | |
7 | ///////////////////////////////////////////////////////////////////////////// | |
8 | ||
9 | ||
10 | #ifdef __GNUG__ | |
c3f4609e | 11 | #pragma implementation "fs_mem.h" |
dcb86da0 VS |
12 | #endif |
13 | ||
14 | #include "wx/wxprec.h" | |
15 | ||
2b5f62a0 | 16 | #ifdef __BORLANDC__ |
dcb86da0 VS |
17 | #pragma hdrstop |
18 | #endif | |
19 | ||
24528b0c | 20 | #if wxUSE_FILESYSTEM && wxUSE_STREAMS |
dcb86da0 | 21 | |
e2478fde VZ |
22 | #include "wx/image.h" |
23 | #include "wx/bitmap.h" | |
24 | #include "wx/fs_mem.h" | |
25 | ||
e2478fde | 26 | |
dcb86da0 | 27 | #ifndef WXPRECOMP |
04dbb646 VZ |
28 | #include "wx/intl.h" |
29 | #include "wx/log.h" | |
8b70ca26 | 30 | #include "wx/hash.h" |
dcb86da0 VS |
31 | #endif |
32 | ||
dcb86da0 VS |
33 | #include "wx/mstream.h" |
34 | ||
dcb86da0 VS |
35 | class MemFSHashObj : public wxObject |
36 | { | |
37 | public: | |
04dbb646 | 38 | |
dcb86da0 VS |
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; | |
e2b87f38 | 44 | InitTime(); |
dcb86da0 | 45 | } |
04dbb646 | 46 | |
dcb86da0 VS |
47 | MemFSHashObj(wxMemoryOutputStream& stream) |
48 | { | |
49 | m_Len = stream.GetSize(); | |
50 | m_Data = new char[m_Len]; | |
51 | stream.CopyTo(m_Data, m_Len); | |
e2b87f38 | 52 | InitTime(); |
dcb86da0 | 53 | } |
04dbb646 | 54 | |
dcb86da0 VS |
55 | ~MemFSHashObj() |
56 | { | |
57 | delete[] m_Data; | |
58 | } | |
04dbb646 | 59 | |
dcb86da0 VS |
60 | char *m_Data; |
61 | size_t m_Len; | |
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(); | |
73 | #endif // wxUSE_DATETIME | |
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 | { | |
102 | delete m_Hash; | |
103 | m_Hash = NULL; | |
104 | } | |
dcb86da0 VS |
105 | } |
106 | ||
107 | ||
108 | ||
e2478fde | 109 | bool wxMemoryFSHandlerBase::CanOpen(const wxString& location) |
dcb86da0 VS |
110 | { |
111 | wxString p = GetProtocol(location); | |
112 | return (p == wxT("memory")); | |
113 | } | |
114 | ||
115 | ||
116 | ||
117 | ||
e2478fde | 118 | wxFSFile* wxMemoryFSHandlerBase::OpenFile(wxFileSystem& WXUNUSED(fs), const wxString& location) |
dcb86da0 VS |
119 | { |
120 | if (m_Hash) | |
121 | { | |
122 | MemFSHashObj *obj = (MemFSHashObj*) m_Hash -> Get(GetRightLocation(location)); | |
123 | if (obj == NULL) return NULL; | |
124 | else return new wxFSFile(new wxMemoryInputStream(obj -> m_Data, obj -> m_Len), | |
125 | location, | |
126 | GetMimeTypeFromExt(location), | |
e2b87f38 VZ |
127 | GetAnchor(location) |
128 | #if wxUSE_DATETIME | |
129 | , obj -> m_Time | |
130 | #endif // wxUSE_DATETIME | |
131 | ); | |
dcb86da0 VS |
132 | } |
133 | else return NULL; | |
134 | } | |
135 | ||
136 | ||
137 | ||
e2478fde | 138 | wxString wxMemoryFSHandlerBase::FindFirst(const wxString& WXUNUSED(spec), |
bc1dcfc1 | 139 | int WXUNUSED(flags)) |
dcb86da0 | 140 | { |
e2478fde | 141 | wxFAIL_MSG(wxT("wxMemoryFSHandlerBase::FindFirst not implemented")); |
bc1dcfc1 | 142 | |
dcb86da0 VS |
143 | return wxEmptyString; |
144 | } | |
145 | ||
146 | ||
147 | ||
e2478fde | 148 | wxString wxMemoryFSHandlerBase::FindNext() |
dcb86da0 | 149 | { |
e2478fde | 150 | wxFAIL_MSG(wxT("wxMemoryFSHandlerBase::FindNext not implemented")); |
bc1dcfc1 | 151 | |
dcb86da0 VS |
152 | return wxEmptyString; |
153 | } | |
154 | ||
155 | ||
e2478fde | 156 | bool wxMemoryFSHandlerBase::CheckHash(const wxString& filename) |
dcb86da0 | 157 | { |
04dbb646 | 158 | if (m_Hash == NULL) |
dcb86da0 VS |
159 | { |
160 | m_Hash = new wxHashTable(wxKEY_STRING); | |
161 | m_Hash -> DeleteContents(TRUE); | |
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); | |
169 | return FALSE; | |
170 | } | |
171 | else | |
172 | return TRUE; | |
173 | } | |
174 | ||
175 | ||
e2478fde | 176 | /*static*/ void wxMemoryFSHandlerBase::AddFile(const wxString& filename, const wxString& textdata) |
dcb86da0 VS |
177 | { |
178 | AddFile(filename, (const void*) textdata.mb_str(), textdata.Length()); | |
179 | } | |
180 | ||
181 | ||
e2478fde | 182 | /*static*/ void wxMemoryFSHandlerBase::AddFile(const wxString& filename, const void *binarydata, size_t size) |
dcb86da0 VS |
183 | { |
184 | if (!CheckHash(filename)) return; | |
185 | m_Hash -> Put(filename, new MemFSHashObj(binarydata, size)); | |
186 | } | |
187 | ||
188 | ||
189 | ||
e2478fde | 190 | /*static*/ void wxMemoryFSHandlerBase::RemoveFile(const wxString& filename) |
dcb86da0 VS |
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 | ||
ec67cff1 | 204 | #endif // wxUSE_BASE |
e2478fde VZ |
205 | |
206 | #if wxUSE_GUI | |
207 | ||
2de5a6ee VS |
208 | #if wxUSE_IMAGE |
209 | /*static*/ void | |
60c0a8db | 210 | wxMemoryFSHandler::AddFile(const wxString& filename, wxImage& image, long type) |
2de5a6ee VS |
211 | { |
212 | if (!CheckHash(filename)) return; | |
213 | ||
2de5a6ee VS |
214 | wxMemoryOutputStream mems; |
215 | if (image.Ok() && image.SaveFile(mems, (int)type)) | |
216 | m_Hash -> Put(filename, new MemFSHashObj(mems)); | |
217 | else | |
218 | { | |
219 | wxString s; | |
220 | s.Printf(_("Failed to store image '%s' to memory VFS!"), filename.c_str()); | |
221 | wxPrintf(wxT("'%s'\n"), s.c_str()); | |
222 | wxLogError(s); | |
223 | } | |
224 | } | |
225 | #endif // wxUSE_IMAGE | |
226 | ||
e2478fde VZ |
227 | /*static*/ void wxMemoryFSHandler::AddFile(const wxString& filename, const wxBitmap& bitmap, long type) |
228 | { | |
229 | wxImage img = bitmap.ConvertToImage(); | |
230 | AddFile(filename, img, type); | |
231 | } | |
232 | ||
233 | #endif | |
dcb86da0 VS |
234 | |
235 | ||
24528b0c | 236 | #endif // wxUSE_FILESYSTEM && wxUSE_FS_ZIP |
e2478fde | 237 |