1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/fs_mem.cpp
3 // Purpose: in-memory file system
4 // Author: Vaclav Slavik
6 // Copyright: (c) 2000 Vaclav Slavik
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #include "wx/wxprec.h"
16 #if wxUSE_FILESYSTEM && wxUSE_STREAMS
18 #include "wx/fs_mem.h"
24 #include "wx/wxcrtvararg.h"
26 #include "wx/bitmap.h"
31 #include "wx/mstream.h"
33 class MemFSHashObj
: public wxObject
37 MemFSHashObj(const void *data
, size_t len
, const wxString
& mime
)
39 m_Data
= new char[len
];
40 memcpy(m_Data
, data
, len
);
46 MemFSHashObj(const wxMemoryOutputStream
& stream
, const wxString
& mime
)
48 m_Len
= stream
.GetSize();
49 m_Data
= new char[m_Len
];
50 stream
.CopyTo(m_Data
, m_Len
);
55 virtual ~MemFSHashObj()
65 #endif // wxUSE_DATETIME
67 DECLARE_NO_COPY_CLASS(MemFSHashObj
)
73 m_Time
= wxDateTime::Now();
74 #endif // wxUSE_DATETIME
81 //--------------------------------------------------------------------------------
83 //--------------------------------------------------------------------------------
86 wxHashTable
*wxMemoryFSHandlerBase::m_Hash
= NULL
;
89 wxMemoryFSHandlerBase::wxMemoryFSHandlerBase() : wxFileSystemHandler()
95 wxMemoryFSHandlerBase::~wxMemoryFSHandlerBase()
97 // as only one copy of FS handler is supposed to exist, we may silently
98 // delete static data here. (There is no way how to remove FS handler from
99 // wxFileSystem other than releasing _all_ handlers.)
103 WX_CLEAR_HASH_TABLE(*m_Hash
);
111 bool wxMemoryFSHandlerBase::CanOpen(const wxString
& location
)
113 wxString p
= GetProtocol(location
);
114 return (p
== wxT("memory"));
120 wxFSFile
* wxMemoryFSHandlerBase::OpenFile(wxFileSystem
& WXUNUSED(fs
), const wxString
& location
)
124 MemFSHashObj
*obj
= (MemFSHashObj
*) m_Hash
-> Get(GetRightLocation(location
));
125 if (obj
== NULL
) return NULL
;
126 else return new wxFSFile(new wxMemoryInputStream(obj
-> m_Data
, obj
-> m_Len
),
132 #endif // wxUSE_DATETIME
140 wxString
wxMemoryFSHandlerBase::FindFirst(const wxString
& WXUNUSED(spec
),
143 wxFAIL_MSG(wxT("wxMemoryFSHandlerBase::FindFirst not implemented"));
145 return wxEmptyString
;
150 wxString
wxMemoryFSHandlerBase::FindNext()
152 wxFAIL_MSG(wxT("wxMemoryFSHandlerBase::FindNext not implemented"));
154 return wxEmptyString
;
158 bool wxMemoryFSHandlerBase::CheckHash(const wxString
& filename
)
162 m_Hash
= new wxHashTable(wxKEY_STRING
);
165 if (m_Hash
-> Get(filename
) != NULL
)
168 s
.Printf(_("Memory VFS already contains file '%s'!"), filename
.c_str());
178 void wxMemoryFSHandlerBase::AddFileWithMimeType(const wxString
& filename
,
179 const wxString
& textdata
,
180 const wxString
& mimetype
)
182 AddFileWithMimeType(filename
,
183 (const void*) textdata
.mb_str(), textdata
.length(),
189 void wxMemoryFSHandlerBase::AddFileWithMimeType(const wxString
& filename
,
190 const void *binarydata
, size_t size
,
191 const wxString
& mimetype
)
193 if (!CheckHash(filename
)) return;
194 m_Hash
-> Put(filename
, new MemFSHashObj(binarydata
, size
, mimetype
));
198 void wxMemoryFSHandlerBase::AddFile(const wxString
& filename
,
199 const wxString
& textdata
)
201 AddFileWithMimeType(filename
, textdata
, wxEmptyString
);
206 void wxMemoryFSHandlerBase::AddFile(const wxString
& filename
,
207 const void *binarydata
, size_t size
)
209 AddFileWithMimeType(filename
, binarydata
, size
, wxEmptyString
);
214 /*static*/ void wxMemoryFSHandlerBase::RemoveFile(const wxString
& filename
)
216 if (m_Hash
== NULL
||
217 m_Hash
-> Get(filename
) == NULL
)
220 s
.Printf(_("Trying to remove file '%s' from memory VFS, but it is not loaded!"), filename
.c_str());
225 delete m_Hash
-> Delete(filename
);
234 wxMemoryFSHandler::AddFile(const wxString
& filename
,
235 const wxImage
& image
,
238 if (!CheckHash(filename
)) return;
240 wxMemoryOutputStream mems
;
241 if (image
.Ok() && image
.SaveFile(mems
, (int)type
))
249 wxImage::FindHandler(type
)->GetMimeType()
256 s
.Printf(_("Failed to store image '%s' to memory VFS!"), filename
.c_str());
257 wxPrintf(wxT("'%s'\n"), s
.c_str());
263 wxMemoryFSHandler::AddFile(const wxString
& filename
,
264 const wxBitmap
& bitmap
,
267 #if !defined(__WXMSW__) || wxUSE_WXDIB
268 wxImage img
= bitmap
.ConvertToImage();
269 AddFile(filename
, img
, type
);
273 #endif // wxUSE_IMAGE
278 #endif // wxUSE_FILESYSTEM && wxUSE_FS_ZIP