]> git.saurik.com Git - wxWidgets.git/blob - src/common/fs_mem.cpp
__WXBASE__ -> wxUSE_BASE
[wxWidgets.git] / src / common / fs_mem.cpp
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/image.h"
23 #include "wx/bitmap.h"
24 #include "wx/fs_mem.h"
25
26
27 #ifndef WXPRECOMP
28 #include "wx/intl.h"
29 #include "wx/log.h"
30 #include "wx/hash.h"
31 #endif
32
33 #include "wx/mstream.h"
34
35 class MemFSHashObj : public wxObject
36 {
37 public:
38
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;
44 InitTime();
45 }
46
47 MemFSHashObj(wxMemoryOutputStream& stream)
48 {
49 m_Len = stream.GetSize();
50 m_Data = new char[m_Len];
51 stream.CopyTo(m_Data, m_Len);
52 InitTime();
53 }
54
55 ~MemFSHashObj()
56 {
57 delete[] m_Data;
58 }
59
60 char *m_Data;
61 size_t m_Len;
62 #if wxUSE_DATETIME
63 wxDateTime m_Time;
64 #endif // wxUSE_DATETIME
65
66 DECLARE_NO_COPY_CLASS(MemFSHashObj)
67
68 private:
69 void InitTime()
70 {
71 #if wxUSE_DATETIME
72 m_Time = wxDateTime::Now();
73 #endif // wxUSE_DATETIME
74 }
75 };
76
77 #if wxUSE_BASE
78
79
80 //--------------------------------------------------------------------------------
81 // wxMemoryFSHandler
82 //--------------------------------------------------------------------------------
83
84
85 wxHashTable *wxMemoryFSHandlerBase::m_Hash = NULL;
86
87
88 wxMemoryFSHandlerBase::wxMemoryFSHandlerBase() : wxFileSystemHandler()
89 {
90 }
91
92
93
94 wxMemoryFSHandlerBase::~wxMemoryFSHandlerBase()
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.)
99
100 if (m_Hash)
101 {
102 delete m_Hash;
103 m_Hash = NULL;
104 }
105 }
106
107
108
109 bool wxMemoryFSHandlerBase::CanOpen(const wxString& location)
110 {
111 wxString p = GetProtocol(location);
112 return (p == wxT("memory"));
113 }
114
115
116
117
118 wxFSFile* wxMemoryFSHandlerBase::OpenFile(wxFileSystem& WXUNUSED(fs), const wxString& location)
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),
127 GetAnchor(location)
128 #if wxUSE_DATETIME
129 , obj -> m_Time
130 #endif // wxUSE_DATETIME
131 );
132 }
133 else return NULL;
134 }
135
136
137
138 wxString wxMemoryFSHandlerBase::FindFirst(const wxString& WXUNUSED(spec),
139 int WXUNUSED(flags))
140 {
141 wxFAIL_MSG(wxT("wxMemoryFSHandlerBase::FindFirst not implemented"));
142
143 return wxEmptyString;
144 }
145
146
147
148 wxString wxMemoryFSHandlerBase::FindNext()
149 {
150 wxFAIL_MSG(wxT("wxMemoryFSHandlerBase::FindNext not implemented"));
151
152 return wxEmptyString;
153 }
154
155
156 bool wxMemoryFSHandlerBase::CheckHash(const wxString& filename)
157 {
158 if (m_Hash == NULL)
159 {
160 m_Hash = new wxHashTable(wxKEY_STRING);
161 m_Hash -> DeleteContents(TRUE);
162 }
163
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
176 /*static*/ void wxMemoryFSHandlerBase::AddFile(const wxString& filename, const wxString& textdata)
177 {
178 AddFile(filename, (const void*) textdata.mb_str(), textdata.Length());
179 }
180
181
182 /*static*/ void wxMemoryFSHandlerBase::AddFile(const wxString& filename, const void *binarydata, size_t size)
183 {
184 if (!CheckHash(filename)) return;
185 m_Hash -> Put(filename, new MemFSHashObj(binarydata, size));
186 }
187
188
189
190 /*static*/ void wxMemoryFSHandlerBase::RemoveFile(const wxString& filename)
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
204 #endif // wxUSE_BASE
205
206 #if wxUSE_GUI
207
208 #if wxUSE_IMAGE
209 /*static*/ void
210 wxMemoryFSHandler::AddFile(const wxString& filename, wxImage& image, long type)
211 {
212 if (!CheckHash(filename)) return;
213
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
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
234
235
236 #endif // wxUSE_FILESYSTEM && wxUSE_FS_ZIP
237