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