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
35 MemFSHashObj(const void *data
, size_t len
, const wxString
& mime
)
37 m_Data
= new char[len
];
38 memcpy(m_Data
, data
, len
);
44 MemFSHashObj(const wxMemoryOutputStream
& stream
, const wxString
& mime
)
46 m_Len
= stream
.GetSize();
47 m_Data
= new char[m_Len
];
48 stream
.CopyTo(m_Data
, m_Len
);
53 virtual ~MemFSHashObj()
63 #endif // wxUSE_DATETIME
65 DECLARE_NO_COPY_CLASS(MemFSHashObj
)
71 m_Time
= wxDateTime::Now();
72 #endif // wxUSE_DATETIME
79 //--------------------------------------------------------------------------------
81 //--------------------------------------------------------------------------------
84 wxHashTable
*wxMemoryFSHandlerBase::m_Hash
= NULL
;
87 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
);
105 bool wxMemoryFSHandlerBase::CanOpen(const wxString
& location
)
107 return GetProtocol(location
) == "memory";
110 wxFSFile
* wxMemoryFSHandlerBase::OpenFile(wxFileSystem
& WXUNUSED(fs
),
111 const wxString
& location
)
116 MemFSHashObj
*obj
= (MemFSHashObj
*) m_Hash
->Get(GetRightLocation(location
));
120 else return new wxFSFile(new wxMemoryInputStream(obj
->m_Data
, obj
->m_Len
),
126 #endif // wxUSE_DATETIME
130 wxString
wxMemoryFSHandlerBase::FindFirst(const wxString
& WXUNUSED(spec
),
133 wxFAIL_MSG(wxT("wxMemoryFSHandlerBase::FindFirst not implemented"));
135 return wxEmptyString
;
138 wxString
wxMemoryFSHandlerBase::FindNext()
140 wxFAIL_MSG(wxT("wxMemoryFSHandlerBase::FindNext not implemented"));
142 return wxEmptyString
;
145 bool wxMemoryFSHandlerBase::CheckHash(const wxString
& filename
)
149 m_Hash
= new wxHashTable(wxKEY_STRING
);
152 if ( m_Hash
->Get(filename
) )
154 wxLogError(_("Memory VFS already contains file '%s'!"), filename
);
163 void wxMemoryFSHandlerBase::AddFileWithMimeType(const wxString
& filename
,
164 const wxString
& textdata
,
165 const wxString
& mimetype
)
167 AddFileWithMimeType(filename
,
168 (const void*) textdata
.mb_str(), textdata
.length(),
174 void wxMemoryFSHandlerBase::AddFileWithMimeType(const wxString
& filename
,
175 const void *binarydata
, size_t size
,
176 const wxString
& mimetype
)
178 if ( !CheckHash(filename
) )
181 m_Hash
->Put(filename
, new MemFSHashObj(binarydata
, size
, mimetype
));
185 void wxMemoryFSHandlerBase::AddFile(const wxString
& filename
,
186 const wxString
& textdata
)
188 AddFileWithMimeType(filename
, textdata
, wxEmptyString
);
193 void wxMemoryFSHandlerBase::AddFile(const wxString
& filename
,
194 const void *binarydata
, size_t size
)
196 AddFileWithMimeType(filename
, binarydata
, size
, wxEmptyString
);
201 /*static*/ void wxMemoryFSHandlerBase::RemoveFile(const wxString
& filename
)
203 if ( !m_Hash
|| !m_Hash
->Get(filename
) )
205 wxLogError(_("Trying to remove file '%s' from memory VFS, "
206 "but it is not loaded!"),
211 delete m_Hash
->Delete(filename
);
221 wxMemoryFSHandler::AddFile(const wxString
& filename
,
222 const wxImage
& image
,
225 if ( !CheckHash(filename
) )
228 wxMemoryOutputStream mems
;
229 if (image
.Ok() && image
.SaveFile(mems
, type
))
237 wxImage::FindHandler(type
)->GetMimeType()
243 wxLogError(_("Failed to store image '%s' to memory VFS!"), filename
);
248 wxMemoryFSHandler::AddFile(const wxString
& filename
,
249 const wxBitmap
& bitmap
,
252 #if !defined(__WXMSW__) || wxUSE_WXDIB
253 wxImage img
= bitmap
.ConvertToImage();
254 AddFile(filename
, img
, type
);
258 #endif // wxUSE_IMAGE
263 #endif // wxUSE_FILESYSTEM && wxUSE_FS_ZIP