]> git.saurik.com Git - wxWidgets.git/blob - src/common/fs_mem.cpp
fix wxFilesystem compilation if wxUSE_FFILE is 0 but wxUSE_FILE is 1; disable wxFileS...
[wxWidgets.git] / src / common / fs_mem.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/fs_mem.cpp
3 // Purpose: in-memory file system
4 // Author: Vaclav Slavik
5 // RCS-ID: $Id$
6 // Copyright: (c) 2000 Vaclav Slavik
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #include "wx/wxprec.h"
11
12 #ifdef __BORLANDC__
13 #pragma hdrstop
14 #endif
15
16 #if wxUSE_FILESYSTEM && wxUSE_STREAMS
17
18 #include "wx/fs_mem.h"
19
20 #ifndef WXPRECOMP
21 #include "wx/intl.h"
22 #include "wx/log.h"
23 #include "wx/hash.h"
24 #if wxUSE_GUI
25 #include "wx/bitmap.h"
26 #include "wx/image.h"
27 #endif // wxUSE_GUI
28 #endif
29
30 #include "wx/mstream.h"
31
32 class MemFSHashObj : public wxObject
33 {
34 public:
35
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;
41 InitTime();
42 }
43
44 MemFSHashObj(const wxMemoryOutputStream& stream)
45 {
46 m_Len = stream.GetSize();
47 m_Data = new char[m_Len];
48 stream.CopyTo(m_Data, m_Len);
49 InitTime();
50 }
51
52 virtual ~MemFSHashObj()
53 {
54 delete[] m_Data;
55 }
56
57 char *m_Data;
58 size_t m_Len;
59 #if wxUSE_DATETIME
60 wxDateTime m_Time;
61 #endif // wxUSE_DATETIME
62
63 DECLARE_NO_COPY_CLASS(MemFSHashObj)
64
65 private:
66 void InitTime()
67 {
68 #if wxUSE_DATETIME
69 m_Time = wxDateTime::Now();
70 #endif // wxUSE_DATETIME
71 }
72 };
73
74 #if wxUSE_BASE
75
76
77 //--------------------------------------------------------------------------------
78 // wxMemoryFSHandler
79 //--------------------------------------------------------------------------------
80
81
82 wxHashTable *wxMemoryFSHandlerBase::m_Hash = NULL;
83
84
85 wxMemoryFSHandlerBase::wxMemoryFSHandlerBase() : wxFileSystemHandler()
86 {
87 }
88
89
90
91 wxMemoryFSHandlerBase::~wxMemoryFSHandlerBase()
92 {
93 // as only one copy of FS handler is supposed to exist, we may silently
94 // delete static data here. (There is no way how to remove FS handler from
95 // wxFileSystem other than releasing _all_ handlers.)
96
97 if (m_Hash)
98 {
99 WX_CLEAR_HASH_TABLE(*m_Hash);
100 delete m_Hash;
101 m_Hash = NULL;
102 }
103 }
104
105
106
107 bool wxMemoryFSHandlerBase::CanOpen(const wxString& location)
108 {
109 wxString p = GetProtocol(location);
110 return (p == wxT("memory"));
111 }
112
113
114
115
116 wxFSFile* wxMemoryFSHandlerBase::OpenFile(wxFileSystem& WXUNUSED(fs), const wxString& location)
117 {
118 if (m_Hash)
119 {
120 MemFSHashObj *obj = (MemFSHashObj*) m_Hash -> Get(GetRightLocation(location));
121 if (obj == NULL) return NULL;
122 else return new wxFSFile(new wxMemoryInputStream(obj -> m_Data, obj -> m_Len),
123 location,
124 GetMimeTypeFromExt(location),
125 GetAnchor(location)
126 #if wxUSE_DATETIME
127 , obj -> m_Time
128 #endif // wxUSE_DATETIME
129 );
130 }
131 else return NULL;
132 }
133
134
135
136 wxString wxMemoryFSHandlerBase::FindFirst(const wxString& WXUNUSED(spec),
137 int WXUNUSED(flags))
138 {
139 wxFAIL_MSG(wxT("wxMemoryFSHandlerBase::FindFirst not implemented"));
140
141 return wxEmptyString;
142 }
143
144
145
146 wxString wxMemoryFSHandlerBase::FindNext()
147 {
148 wxFAIL_MSG(wxT("wxMemoryFSHandlerBase::FindNext not implemented"));
149
150 return wxEmptyString;
151 }
152
153
154 bool wxMemoryFSHandlerBase::CheckHash(const wxString& filename)
155 {
156 if (m_Hash == NULL)
157 {
158 m_Hash = new wxHashTable(wxKEY_STRING);
159 }
160
161 if (m_Hash -> Get(filename) != NULL)
162 {
163 wxString s;
164 s.Printf(_("Memory VFS already contains file '%s'!"), filename.c_str());
165 wxLogError(s);
166 return false;
167 }
168 else
169 return true;
170 }
171
172
173 /*static*/ void wxMemoryFSHandlerBase::AddFile(const wxString& filename, const wxString& textdata)
174 {
175 AddFile(filename, (const void*) textdata.mb_str(), textdata.length());
176 }
177
178
179 /*static*/ void wxMemoryFSHandlerBase::AddFile(const wxString& filename, const void *binarydata, size_t size)
180 {
181 if (!CheckHash(filename)) return;
182 m_Hash -> Put(filename, new MemFSHashObj(binarydata, size));
183 }
184
185
186
187 /*static*/ void wxMemoryFSHandlerBase::RemoveFile(const wxString& filename)
188 {
189 if (m_Hash == NULL ||
190 m_Hash -> Get(filename) == NULL)
191 {
192 wxString s;
193 s.Printf(_("Trying to remove file '%s' from memory VFS, but it is not loaded!"), filename.c_str());
194 wxLogError(s);
195 }
196
197 else
198 delete m_Hash -> Delete(filename);
199 }
200
201 #endif // wxUSE_BASE
202
203 #if wxUSE_GUI
204
205 #if wxUSE_IMAGE
206 /*static*/ void
207 wxMemoryFSHandler::AddFile(const wxString& filename,
208 const wxImage& image,
209 long type)
210 {
211 if (!CheckHash(filename)) return;
212
213 wxMemoryOutputStream mems;
214 if (image.Ok() && image.SaveFile(mems, (int)type))
215 m_Hash -> Put(filename, new MemFSHashObj(mems));
216 else
217 {
218 wxString s;
219 s.Printf(_("Failed to store image '%s' to memory VFS!"), filename.c_str());
220 wxPrintf(wxT("'%s'\n"), s.c_str());
221 wxLogError(s);
222 }
223 }
224
225 /*static*/ void
226 wxMemoryFSHandler::AddFile(const wxString& filename,
227 const wxBitmap& bitmap,
228 long type)
229 {
230 #if !defined(__WXMSW__) || wxUSE_WXDIB
231 wxImage img = bitmap.ConvertToImage();
232 AddFile(filename, img, type);
233 #endif
234 }
235
236 #endif // wxUSE_IMAGE
237
238 #endif // wxUSE_GUI
239
240
241 #endif // wxUSE_FILESYSTEM && wxUSE_FS_ZIP