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