]> git.saurik.com Git - wxWidgets.git/blame - src/common/fs_mem.cpp
fix the run-time behaviour after the last compilation fixing patch
[wxWidgets.git] / src / common / fs_mem.cpp
CommitLineData
dcb86da0 1/////////////////////////////////////////////////////////////////////////////
0bca0373 2// Name: src/common/fs_mem.cpp
dcb86da0
VS
3// Purpose: in-memory file system
4// Author: Vaclav Slavik
0bca0373 5// RCS-ID: $Id$
dcb86da0 6// Copyright: (c) 2000 Vaclav Slavik
65571936 7// Licence: wxWindows licence
dcb86da0
VS
8/////////////////////////////////////////////////////////////////////////////
9
dcb86da0
VS
10#include "wx/wxprec.h"
11
2b5f62a0 12#ifdef __BORLANDC__
0bca0373 13 #pragma hdrstop
dcb86da0
VS
14#endif
15
24528b0c 16#if wxUSE_FILESYSTEM && wxUSE_STREAMS
dcb86da0 17
e2478fde
VZ
18#include "wx/fs_mem.h"
19
dcb86da0 20#ifndef WXPRECOMP
04dbb646
VZ
21 #include "wx/intl.h"
22 #include "wx/log.h"
8b70ca26 23 #include "wx/hash.h"
0bca0373
WS
24 #if wxUSE_GUI
25 #include "wx/bitmap.h"
155ecd4c 26 #include "wx/image.h"
0bca0373 27 #endif // wxUSE_GUI
dcb86da0
VS
28#endif
29
dcb86da0
VS
30#include "wx/mstream.h"
31
dcb86da0
VS
32class 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
fbfb8bcc 44 MemFSHashObj(const wxMemoryOutputStream& stream)
dcb86da0
VS
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
d3c7fc99 52 virtual ~MemFSHashObj()
dcb86da0
VS
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();
a62848fd 70#endif // wxUSE_DATETIME
e2b87f38 71 }
dcb86da0
VS
72};
73
ec67cff1 74#if wxUSE_BASE
60c0a8db 75
dcb86da0
VS
76
77//--------------------------------------------------------------------------------
78// wxMemoryFSHandler
79//--------------------------------------------------------------------------------
80
81
e2478fde 82wxHashTable *wxMemoryFSHandlerBase::m_Hash = NULL;
dcb86da0
VS
83
84
e2478fde 85wxMemoryFSHandlerBase::wxMemoryFSHandlerBase() : wxFileSystemHandler()
dcb86da0 86{
dcb86da0
VS
87}
88
89
90
e2478fde 91wxMemoryFSHandlerBase::~wxMemoryFSHandlerBase()
dcb86da0
VS
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.)
04dbb646 96
e2478fde
VZ
97 if (m_Hash)
98 {
df5168c4 99 WX_CLEAR_HASH_TABLE(*m_Hash);
e2478fde
VZ
100 delete m_Hash;
101 m_Hash = NULL;
102 }
dcb86da0
VS
103}
104
105
106
e2478fde 107bool wxMemoryFSHandlerBase::CanOpen(const wxString& location)
dcb86da0
VS
108{
109 wxString p = GetProtocol(location);
110 return (p == wxT("memory"));
111}
112
113
114
115
e2478fde 116wxFSFile* wxMemoryFSHandlerBase::OpenFile(wxFileSystem& WXUNUSED(fs), const wxString& location)
dcb86da0
VS
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),
e2b87f38
VZ
125 GetAnchor(location)
126#if wxUSE_DATETIME
127 , obj -> m_Time
a62848fd 128#endif // wxUSE_DATETIME
e2b87f38 129 );
dcb86da0
VS
130 }
131 else return NULL;
132}
133
134
135
e2478fde 136wxString wxMemoryFSHandlerBase::FindFirst(const wxString& WXUNUSED(spec),
bc1dcfc1 137 int WXUNUSED(flags))
dcb86da0 138{
e2478fde 139 wxFAIL_MSG(wxT("wxMemoryFSHandlerBase::FindFirst not implemented"));
bc1dcfc1 140
dcb86da0
VS
141 return wxEmptyString;
142}
143
144
145
e2478fde 146wxString wxMemoryFSHandlerBase::FindNext()
dcb86da0 147{
e2478fde 148 wxFAIL_MSG(wxT("wxMemoryFSHandlerBase::FindNext not implemented"));
bc1dcfc1 149
dcb86da0
VS
150 return wxEmptyString;
151}
152
153
e2478fde 154bool wxMemoryFSHandlerBase::CheckHash(const wxString& filename)
dcb86da0 155{
04dbb646 156 if (m_Hash == NULL)
dcb86da0
VS
157 {
158 m_Hash = new wxHashTable(wxKEY_STRING);
dcb86da0 159 }
04dbb646 160
dcb86da0
VS
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);
a62848fd 166 return false;
dcb86da0
VS
167 }
168 else
a62848fd 169 return true;
dcb86da0
VS
170}
171
172
e2478fde 173/*static*/ void wxMemoryFSHandlerBase::AddFile(const wxString& filename, const wxString& textdata)
dcb86da0 174{
0bca0373 175 AddFile(filename, (const void*) textdata.mb_str(), textdata.length());
dcb86da0
VS
176}
177
178
e2478fde 179/*static*/ void wxMemoryFSHandlerBase::AddFile(const wxString& filename, const void *binarydata, size_t size)
dcb86da0
VS
180{
181 if (!CheckHash(filename)) return;
182 m_Hash -> Put(filename, new MemFSHashObj(binarydata, size));
183}
184
185
186
e2478fde 187/*static*/ void wxMemoryFSHandlerBase::RemoveFile(const wxString& filename)
dcb86da0
VS
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
ec67cff1 201#endif // wxUSE_BASE
e2478fde
VZ
202
203#if wxUSE_GUI
204
2de5a6ee
VS
205#if wxUSE_IMAGE
206/*static*/ void
989a2421
VZ
207wxMemoryFSHandler::AddFile(const wxString& filename,
208 const wxImage& image,
209 long type)
2de5a6ee
VS
210{
211 if (!CheckHash(filename)) return;
212
2de5a6ee
VS
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}
2de5a6ee 224
989a2421
VZ
225/*static*/ void
226wxMemoryFSHandler::AddFile(const wxString& filename,
227 const wxBitmap& bitmap,
228 long type)
e2478fde 229{
64c288fa 230#if !defined(__WXMSW__) || wxUSE_WXDIB
e2478fde
VZ
231 wxImage img = bitmap.ConvertToImage();
232 AddFile(filename, img, type);
64c288fa 233#endif
e2478fde
VZ
234}
235
1904aa72
DS
236#endif // wxUSE_IMAGE
237
238#endif // wxUSE_GUI
dcb86da0
VS
239
240
24528b0c 241#endif // wxUSE_FILESYSTEM && wxUSE_FS_ZIP