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"
30 #include "wx/mstream.h"
32 class MemFSHashObj
: public wxObject
36 MemFSHashObj(const void *data
, size_t len
, const wxString
& mime
)
38 m_Data
= new char[len
];
39 memcpy(m_Data
, data
, len
);
45 MemFSHashObj(const wxMemoryOutputStream
& stream
, const wxString
& mime
)
47 m_Len
= stream
.GetSize();
48 m_Data
= new char[m_Len
];
49 stream
.CopyTo(m_Data
, m_Len
);
54 virtual ~MemFSHashObj()
64 #endif // wxUSE_DATETIME
66 DECLARE_NO_COPY_CLASS(MemFSHashObj
)
72 m_Time
= wxDateTime::Now();
73 #endif // wxUSE_DATETIME
80 //--------------------------------------------------------------------------------
82 //--------------------------------------------------------------------------------
85 wxHashTable
*wxMemoryFSHandlerBase::m_Hash
= NULL
;
88 wxMemoryFSHandlerBase::wxMemoryFSHandlerBase() : wxFileSystemHandler()
94 wxMemoryFSHandlerBase::~wxMemoryFSHandlerBase()
96 // as only one copy of FS handler is supposed to exist, we may silently
97 // delete static data here. (There is no way how to remove FS handler from
98 // wxFileSystem other than releasing _all_ handlers.)
102 WX_CLEAR_HASH_TABLE(*m_Hash
);
110 bool wxMemoryFSHandlerBase::CanOpen(const wxString
& location
)
112 wxString p
= GetProtocol(location
);
113 return (p
== wxT("memory"));
119 wxFSFile
* wxMemoryFSHandlerBase::OpenFile(wxFileSystem
& WXUNUSED(fs
), const wxString
& location
)
123 MemFSHashObj
*obj
= (MemFSHashObj
*) m_Hash
-> Get(GetRightLocation(location
));
124 if (obj
== NULL
) return NULL
;
125 else return new wxFSFile(new wxMemoryInputStream(obj
-> m_Data
, obj
-> m_Len
),
131 #endif // wxUSE_DATETIME
139 wxString
wxMemoryFSHandlerBase::FindFirst(const wxString
& WXUNUSED(spec
),
142 wxFAIL_MSG(wxT("wxMemoryFSHandlerBase::FindFirst not implemented"));
144 return wxEmptyString
;
149 wxString
wxMemoryFSHandlerBase::FindNext()
151 wxFAIL_MSG(wxT("wxMemoryFSHandlerBase::FindNext not implemented"));
153 return wxEmptyString
;
157 bool wxMemoryFSHandlerBase::CheckHash(const wxString
& filename
)
161 m_Hash
= new wxHashTable(wxKEY_STRING
);
164 if (m_Hash
-> Get(filename
) != NULL
)
167 s
.Printf(_("Memory VFS already contains file '%s'!"), filename
.c_str());
177 void wxMemoryFSHandlerBase::AddFileWithMimeType(const wxString
& filename
,
178 const wxString
& textdata
,
179 const wxString
& mimetype
)
181 AddFileWithMimeType(filename
,
182 (const void*) textdata
.mb_str(), textdata
.length(),
188 void wxMemoryFSHandlerBase::AddFileWithMimeType(const wxString
& filename
,
189 const void *binarydata
, size_t size
,
190 const wxString
& mimetype
)
192 if (!CheckHash(filename
)) return;
193 m_Hash
-> Put(filename
, new MemFSHashObj(binarydata
, size
, mimetype
));
197 void wxMemoryFSHandlerBase::AddFile(const wxString
& filename
,
198 const wxString
& textdata
)
200 AddFileWithMimeType(filename
, textdata
, wxEmptyString
);
205 void wxMemoryFSHandlerBase::AddFile(const wxString
& filename
,
206 const void *binarydata
, size_t size
)
208 AddFileWithMimeType(filename
, binarydata
, size
, wxEmptyString
);
213 /*static*/ void wxMemoryFSHandlerBase::RemoveFile(const wxString
& filename
)
215 if (m_Hash
== NULL
||
216 m_Hash
-> Get(filename
) == NULL
)
219 s
.Printf(_("Trying to remove file '%s' from memory VFS, but it is not loaded!"), filename
.c_str());
224 delete m_Hash
-> Delete(filename
);
233 wxMemoryFSHandler::AddFile(const wxString
& filename
,
234 const wxImage
& image
,
237 if (!CheckHash(filename
)) return;
239 wxMemoryOutputStream mems
;
240 if (image
.Ok() && image
.SaveFile(mems
, type
))
248 wxImage::FindHandler(type
)->GetMimeType()
255 s
.Printf(_("Failed to store image '%s' to memory VFS!"), filename
.c_str());
256 wxPrintf(wxT("'%s'\n"), s
.c_str());
262 wxMemoryFSHandler::AddFile(const wxString
& filename
,
263 const wxBitmap
& bitmap
,
266 #if !defined(__WXMSW__) || wxUSE_WXDIB
267 wxImage img
= bitmap
.ConvertToImage();
268 AddFile(filename
, img
, type
);
272 #endif // wxUSE_IMAGE
277 #endif // wxUSE_FILESYSTEM && wxUSE_FS_ZIP