1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: in-memory file system
4 // Author: Vaclav Slavik
5 // Copyright: (c) 2000 Vaclav Slavik
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
15 #if wxUSE_FILESYSTEM && wxUSE_STREAMS
17 #include "wx/fs_mem.h"
21 #include "wx/bitmap.h"
30 #include "wx/mstream.h"
32 class MemFSHashObj
: public wxObject
36 MemFSHashObj(const void *data
, size_t len
)
38 m_Data
= new char[len
];
39 memcpy(m_Data
, data
, len
);
44 MemFSHashObj(const wxMemoryOutputStream
& stream
)
46 m_Len
= stream
.GetSize();
47 m_Data
= new char[m_Len
];
48 stream
.CopyTo(m_Data
, m_Len
);
61 #endif // wxUSE_DATETIME
63 DECLARE_NO_COPY_CLASS(MemFSHashObj
)
69 m_Time
= wxDateTime::Now();
70 #endif // wxUSE_DATETIME
77 //--------------------------------------------------------------------------------
79 //--------------------------------------------------------------------------------
82 wxHashTable
*wxMemoryFSHandlerBase::m_Hash
= NULL
;
85 wxMemoryFSHandlerBase::wxMemoryFSHandlerBase() : wxFileSystemHandler()
91 wxMemoryFSHandlerBase::~wxMemoryFSHandlerBase()
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.)
99 WX_CLEAR_HASH_TABLE(*m_Hash
);
107 bool wxMemoryFSHandlerBase::CanOpen(const wxString
& location
)
109 wxString p
= GetProtocol(location
);
110 return (p
== wxT("memory"));
116 wxFSFile
* wxMemoryFSHandlerBase::OpenFile(wxFileSystem
& WXUNUSED(fs
), const wxString
& location
)
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
),
124 GetMimeTypeFromExt(location
),
128 #endif // wxUSE_DATETIME
136 wxString
wxMemoryFSHandlerBase::FindFirst(const wxString
& WXUNUSED(spec
),
139 wxFAIL_MSG(wxT("wxMemoryFSHandlerBase::FindFirst not implemented"));
141 return wxEmptyString
;
146 wxString
wxMemoryFSHandlerBase::FindNext()
148 wxFAIL_MSG(wxT("wxMemoryFSHandlerBase::FindNext not implemented"));
150 return wxEmptyString
;
154 bool wxMemoryFSHandlerBase::CheckHash(const wxString
& filename
)
158 m_Hash
= new wxHashTable(wxKEY_STRING
);
161 if (m_Hash
-> Get(filename
) != NULL
)
164 s
.Printf(_("Memory VFS already contains file '%s'!"), filename
.c_str());
173 /*static*/ void wxMemoryFSHandlerBase::AddFile(const wxString
& filename
, const wxString
& textdata
)
175 AddFile(filename
, (const void*) textdata
.mb_str(), textdata
.Length());
179 /*static*/ void wxMemoryFSHandlerBase::AddFile(const wxString
& filename
, const void *binarydata
, size_t size
)
181 if (!CheckHash(filename
)) return;
182 m_Hash
-> Put(filename
, new MemFSHashObj(binarydata
, size
));
187 /*static*/ void wxMemoryFSHandlerBase::RemoveFile(const wxString
& filename
)
189 if (m_Hash
== NULL
||
190 m_Hash
-> Get(filename
) == NULL
)
193 s
.Printf(_("Trying to remove file '%s' from memory VFS, but it is not loaded!"), filename
.c_str());
198 delete m_Hash
-> Delete(filename
);
207 wxMemoryFSHandler::AddFile(const wxString
& filename
,
208 const wxImage
& image
,
211 if (!CheckHash(filename
)) return;
213 wxMemoryOutputStream mems
;
214 if (image
.Ok() && image
.SaveFile(mems
, (int)type
))
215 m_Hash
-> Put(filename
, new MemFSHashObj(mems
));
219 s
.Printf(_("Failed to store image '%s' to memory VFS!"), filename
.c_str());
220 wxPrintf(wxT("'%s'\n"), s
.c_str());
226 wxMemoryFSHandler::AddFile(const wxString
& filename
,
227 const wxBitmap
& bitmap
,
230 #if !defined(__WXMSW__) || wxUSE_WXDIB
231 wxImage img
= bitmap
.ConvertToImage();
232 AddFile(filename
, img
, type
);
236 #endif // wxUSE_IMAGE
241 #endif // wxUSE_FILESYSTEM && wxUSE_FS_ZIP