]>
Commit | Line | Data |
---|---|---|
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__ | |
11 | #pragma implementation "fs_mem.h" | |
12 | #endif | |
13 | ||
14 | #include "wx/wxprec.h" | |
15 | ||
16 | #ifdef __BORLANDC__ | |
17 | #pragma hdrstop | |
18 | #endif | |
19 | ||
20 | #if wxUSE_FILESYSTEM && wxUSE_STREAMS | |
21 | ||
22 | #include "wx/fs_mem.h" | |
23 | ||
24 | #if wxUSE_GUI | |
25 | #include "wx/image.h" | |
26 | #include "wx/bitmap.h" | |
27 | #endif // wxUSE_GUI | |
28 | ||
29 | #ifndef WXPRECOMP | |
30 | #include "wx/intl.h" | |
31 | #include "wx/log.h" | |
32 | #include "wx/hash.h" | |
33 | #endif | |
34 | ||
35 | #include "wx/mstream.h" | |
36 | ||
37 | class MemFSHashObj : public wxObject | |
38 | { | |
39 | public: | |
40 | ||
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; | |
46 | InitTime(); | |
47 | } | |
48 | ||
49 | MemFSHashObj(wxMemoryOutputStream& stream) | |
50 | { | |
51 | m_Len = stream.GetSize(); | |
52 | m_Data = new char[m_Len]; | |
53 | stream.CopyTo(m_Data, m_Len); | |
54 | InitTime(); | |
55 | } | |
56 | ||
57 | ~MemFSHashObj() | |
58 | { | |
59 | delete[] m_Data; | |
60 | } | |
61 | ||
62 | char *m_Data; | |
63 | size_t m_Len; | |
64 | #if wxUSE_DATETIME | |
65 | wxDateTime m_Time; | |
66 | #endif // wxUSE_DATETIME | |
67 | ||
68 | DECLARE_NO_COPY_CLASS(MemFSHashObj) | |
69 | ||
70 | private: | |
71 | void InitTime() | |
72 | { | |
73 | #if wxUSE_DATETIME | |
74 | m_Time = wxDateTime::Now(); | |
75 | #endif // wxUSE_DATETIME | |
76 | } | |
77 | }; | |
78 | ||
79 | #if wxUSE_BASE | |
80 | ||
81 | ||
82 | //-------------------------------------------------------------------------------- | |
83 | // wxMemoryFSHandler | |
84 | //-------------------------------------------------------------------------------- | |
85 | ||
86 | ||
87 | wxHashTable *wxMemoryFSHandlerBase::m_Hash = NULL; | |
88 | ||
89 | ||
90 | wxMemoryFSHandlerBase::wxMemoryFSHandlerBase() : wxFileSystemHandler() | |
91 | { | |
92 | } | |
93 | ||
94 | ||
95 | ||
96 | wxMemoryFSHandlerBase::~wxMemoryFSHandlerBase() | |
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.) | |
101 | ||
102 | if (m_Hash) | |
103 | { | |
104 | WX_CLEAR_HASH_TABLE(*m_Hash); | |
105 | delete m_Hash; | |
106 | m_Hash = NULL; | |
107 | } | |
108 | } | |
109 | ||
110 | ||
111 | ||
112 | bool wxMemoryFSHandlerBase::CanOpen(const wxString& location) | |
113 | { | |
114 | wxString p = GetProtocol(location); | |
115 | return (p == wxT("memory")); | |
116 | } | |
117 | ||
118 | ||
119 | ||
120 | ||
121 | wxFSFile* wxMemoryFSHandlerBase::OpenFile(wxFileSystem& WXUNUSED(fs), const wxString& location) | |
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), | |
130 | GetAnchor(location) | |
131 | #if wxUSE_DATETIME | |
132 | , obj -> m_Time | |
133 | #endif // wxUSE_DATETIME | |
134 | ); | |
135 | } | |
136 | else return NULL; | |
137 | } | |
138 | ||
139 | ||
140 | ||
141 | wxString wxMemoryFSHandlerBase::FindFirst(const wxString& WXUNUSED(spec), | |
142 | int WXUNUSED(flags)) | |
143 | { | |
144 | wxFAIL_MSG(wxT("wxMemoryFSHandlerBase::FindFirst not implemented")); | |
145 | ||
146 | return wxEmptyString; | |
147 | } | |
148 | ||
149 | ||
150 | ||
151 | wxString wxMemoryFSHandlerBase::FindNext() | |
152 | { | |
153 | wxFAIL_MSG(wxT("wxMemoryFSHandlerBase::FindNext not implemented")); | |
154 | ||
155 | return wxEmptyString; | |
156 | } | |
157 | ||
158 | ||
159 | bool wxMemoryFSHandlerBase::CheckHash(const wxString& filename) | |
160 | { | |
161 | if (m_Hash == NULL) | |
162 | { | |
163 | m_Hash = new wxHashTable(wxKEY_STRING); | |
164 | } | |
165 | ||
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 | ||
178 | /*static*/ void wxMemoryFSHandlerBase::AddFile(const wxString& filename, const wxString& textdata) | |
179 | { | |
180 | AddFile(filename, (const void*) textdata.mb_str(), textdata.Length()); | |
181 | } | |
182 | ||
183 | ||
184 | /*static*/ void wxMemoryFSHandlerBase::AddFile(const wxString& filename, const void *binarydata, size_t size) | |
185 | { | |
186 | if (!CheckHash(filename)) return; | |
187 | m_Hash -> Put(filename, new MemFSHashObj(binarydata, size)); | |
188 | } | |
189 | ||
190 | ||
191 | ||
192 | /*static*/ void wxMemoryFSHandlerBase::RemoveFile(const wxString& filename) | |
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 | ||
206 | #endif // wxUSE_BASE | |
207 | ||
208 | #if wxUSE_GUI | |
209 | ||
210 | #if wxUSE_IMAGE | |
211 | /*static*/ void | |
212 | wxMemoryFSHandler::AddFile(const wxString& filename, wxImage& image, 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 | #endif // wxUSE_IMAGE | |
228 | ||
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 | |
236 | ||
237 | ||
238 | #endif // wxUSE_FILESYSTEM && wxUSE_FS_ZIP | |
239 |