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