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 | ||
16 | #ifdef __BORDLANDC__ | |
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; | |
5f8525bf | 41 | m_Time = wxDateTime::Now(); |
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); | |
5f8525bf | 49 | m_Time = wxDateTime::Now(); |
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; | |
59 | wxDateTime m_Time; | |
60 | }; | |
61 | ||
62 | ||
63 | //-------------------------------------------------------------------------------- | |
64 | // wxMemoryFSHandler | |
65 | //-------------------------------------------------------------------------------- | |
66 | ||
67 | ||
68 | wxHashTable *wxMemoryFSHandler::m_Hash = NULL; | |
69 | ||
70 | ||
71 | wxMemoryFSHandler::wxMemoryFSHandler() : wxFileSystemHandler() | |
72 | { | |
73 | m_Hash = NULL; | |
74 | } | |
75 | ||
76 | ||
77 | ||
78 | wxMemoryFSHandler::~wxMemoryFSHandler() | |
79 | { | |
80 | // as only one copy of FS handler is supposed to exist, we may silently | |
81 | // delete static data here. (There is no way how to remove FS handler from | |
82 | // wxFileSystem other than releasing _all_ handlers.) | |
04dbb646 VZ |
83 | |
84 | if (m_Hash) delete m_Hash; | |
dcb86da0 VS |
85 | m_Hash = NULL; |
86 | } | |
87 | ||
88 | ||
89 | ||
90 | bool wxMemoryFSHandler::CanOpen(const wxString& location) | |
91 | { | |
92 | wxString p = GetProtocol(location); | |
93 | return (p == wxT("memory")); | |
94 | } | |
95 | ||
96 | ||
97 | ||
98 | ||
99 | wxFSFile* wxMemoryFSHandler::OpenFile(wxFileSystem& WXUNUSED(fs), const wxString& location) | |
100 | { | |
101 | if (m_Hash) | |
102 | { | |
103 | MemFSHashObj *obj = (MemFSHashObj*) m_Hash -> Get(GetRightLocation(location)); | |
104 | if (obj == NULL) return NULL; | |
105 | else return new wxFSFile(new wxMemoryInputStream(obj -> m_Data, obj -> m_Len), | |
106 | location, | |
107 | GetMimeTypeFromExt(location), | |
108 | GetAnchor(location), | |
109 | obj -> m_Time); | |
110 | } | |
111 | else return NULL; | |
112 | } | |
113 | ||
114 | ||
115 | ||
bc1dcfc1 VZ |
116 | wxString wxMemoryFSHandler::FindFirst(const wxString& WXUNUSED(spec), |
117 | int WXUNUSED(flags)) | |
dcb86da0 | 118 | { |
bc1dcfc1 VZ |
119 | wxFAIL_MSG(wxT("wxMemoryFSHandler::FindFirst not implemented")); |
120 | ||
dcb86da0 VS |
121 | return wxEmptyString; |
122 | } | |
123 | ||
124 | ||
125 | ||
126 | wxString wxMemoryFSHandler::FindNext() | |
127 | { | |
bc1dcfc1 VZ |
128 | wxFAIL_MSG(wxT("wxMemoryFSHandler::FindNext not implemented")); |
129 | ||
dcb86da0 VS |
130 | return wxEmptyString; |
131 | } | |
132 | ||
133 | ||
dcb86da0 VS |
134 | bool wxMemoryFSHandler::CheckHash(const wxString& filename) |
135 | { | |
04dbb646 | 136 | if (m_Hash == NULL) |
dcb86da0 VS |
137 | { |
138 | m_Hash = new wxHashTable(wxKEY_STRING); | |
139 | m_Hash -> DeleteContents(TRUE); | |
140 | } | |
04dbb646 | 141 | |
dcb86da0 VS |
142 | if (m_Hash -> Get(filename) != NULL) |
143 | { | |
144 | wxString s; | |
145 | s.Printf(_("Memory VFS already contains file '%s'!"), filename.c_str()); | |
146 | wxLogError(s); | |
147 | return FALSE; | |
148 | } | |
149 | else | |
150 | return TRUE; | |
151 | } | |
152 | ||
153 | ||
154 | ||
11eaa981 VS |
155 | #if wxUSE_GUI |
156 | ||
dcb86da0 VS |
157 | /*static*/ void wxMemoryFSHandler::AddFile(const wxString& filename, wxImage& image, long type) |
158 | { | |
159 | if (!CheckHash(filename)) return; | |
160 | ||
04dbb646 | 161 | |
dcb86da0 | 162 | wxMemoryOutputStream mems; |
f6bcfd97 | 163 | if (image.Ok() && image.SaveFile(mems, (int)type)) |
dcb86da0 VS |
164 | m_Hash -> Put(filename, new MemFSHashObj(mems)); |
165 | else | |
166 | { | |
167 | wxString s; | |
168 | s.Printf(_("Failed to store image '%s' to memory VFS!"), filename.c_str()); | |
f6bcfd97 | 169 | wxPrintf(wxT("'%s'\n"), s.c_str()); |
dcb86da0 VS |
170 | wxLogError(s); |
171 | } | |
172 | } | |
173 | ||
174 | ||
175 | /*static*/ void wxMemoryFSHandler::AddFile(const wxString& filename, const wxBitmap& bitmap, long type) | |
176 | { | |
177 | wxImage img(bitmap); | |
178 | AddFile(filename, img, type); | |
179 | } | |
180 | ||
11eaa981 | 181 | #endif |
dcb86da0 VS |
182 | |
183 | /*static*/ void wxMemoryFSHandler::AddFile(const wxString& filename, const wxString& textdata) | |
184 | { | |
185 | AddFile(filename, (const void*) textdata.mb_str(), textdata.Length()); | |
186 | } | |
187 | ||
188 | ||
189 | /*static*/ void wxMemoryFSHandler::AddFile(const wxString& filename, const void *binarydata, size_t size) | |
190 | { | |
191 | if (!CheckHash(filename)) return; | |
192 | m_Hash -> Put(filename, new MemFSHashObj(binarydata, size)); | |
193 | } | |
194 | ||
195 | ||
196 | ||
197 | /*static*/ void wxMemoryFSHandler::RemoveFile(const wxString& filename) | |
198 | { | |
199 | if (m_Hash == NULL || | |
200 | m_Hash -> Get(filename) == NULL) | |
201 | { | |
202 | wxString s; | |
203 | s.Printf(_("Trying to remove file '%s' from memory VFS, but it is not loaded!"), filename.c_str()); | |
204 | wxLogError(s); | |
205 | } | |
206 | ||
207 | else | |
208 | delete m_Hash -> Delete(filename); | |
209 | } | |
210 | ||
211 | ||
212 | ||
24528b0c | 213 | #endif // wxUSE_FILESYSTEM && wxUSE_FS_ZIP |