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