1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: in-memory file system
4 // Author: Vaclav Slavik
5 // Copyright: (c) 2000 Vaclav Slavik
6 // Licence: wxWindows Licence
7 /////////////////////////////////////////////////////////////////////////////
10 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "fs_mem.h"
14 #include "wx/wxprec.h"
20 #if wxUSE_FILESYSTEM && wxUSE_STREAMS
22 #include "wx/fs_mem.h"
26 #include "wx/bitmap.h"
35 #include "wx/mstream.h"
37 class MemFSHashObj
: public wxObject
41 MemFSHashObj(const void *data
, size_t len
)
43 m_Data
= new char[len
];
44 memcpy(m_Data
, data
, len
);
49 MemFSHashObj(wxMemoryOutputStream
& stream
)
51 m_Len
= stream
.GetSize();
52 m_Data
= new char[m_Len
];
53 stream
.CopyTo(m_Data
, m_Len
);
66 #endif // wxUSE_DATETIME
68 DECLARE_NO_COPY_CLASS(MemFSHashObj
)
74 m_Time
= wxDateTime::Now();
75 #endif // wxUSE_DATETIME
82 //--------------------------------------------------------------------------------
84 //--------------------------------------------------------------------------------
87 wxHashTable
*wxMemoryFSHandlerBase::m_Hash
= NULL
;
90 wxMemoryFSHandlerBase::wxMemoryFSHandlerBase() : wxFileSystemHandler()
96 wxMemoryFSHandlerBase::~wxMemoryFSHandlerBase()
98 // as only one copy of FS handler is supposed to exist, we may silently
99 // delete static data here. (There is no way how to remove FS handler from
100 // wxFileSystem other than releasing _all_ handlers.)
104 WX_CLEAR_HASH_TABLE(*m_Hash
);
112 bool wxMemoryFSHandlerBase::CanOpen(const wxString
& location
)
114 wxString p
= GetProtocol(location
);
115 return (p
== wxT("memory"));
121 wxFSFile
* wxMemoryFSHandlerBase::OpenFile(wxFileSystem
& WXUNUSED(fs
), const wxString
& location
)
125 MemFSHashObj
*obj
= (MemFSHashObj
*) m_Hash
-> Get(GetRightLocation(location
));
126 if (obj
== NULL
) return NULL
;
127 else return new wxFSFile(new wxMemoryInputStream(obj
-> m_Data
, obj
-> m_Len
),
129 GetMimeTypeFromExt(location
),
133 #endif // wxUSE_DATETIME
141 wxString
wxMemoryFSHandlerBase::FindFirst(const wxString
& WXUNUSED(spec
),
144 wxFAIL_MSG(wxT("wxMemoryFSHandlerBase::FindFirst not implemented"));
146 return wxEmptyString
;
151 wxString
wxMemoryFSHandlerBase::FindNext()
153 wxFAIL_MSG(wxT("wxMemoryFSHandlerBase::FindNext not implemented"));
155 return wxEmptyString
;
159 bool wxMemoryFSHandlerBase::CheckHash(const wxString
& filename
)
163 m_Hash
= new wxHashTable(wxKEY_STRING
);
166 if (m_Hash
-> Get(filename
) != NULL
)
169 s
.Printf(_("Memory VFS already contains file '%s'!"), filename
.c_str());
178 /*static*/ void wxMemoryFSHandlerBase::AddFile(const wxString
& filename
, const wxString
& textdata
)
180 AddFile(filename
, (const void*) textdata
.mb_str(), textdata
.Length());
184 /*static*/ void wxMemoryFSHandlerBase::AddFile(const wxString
& filename
, const void *binarydata
, size_t size
)
186 if (!CheckHash(filename
)) return;
187 m_Hash
-> Put(filename
, new MemFSHashObj(binarydata
, size
));
192 /*static*/ void wxMemoryFSHandlerBase::RemoveFile(const wxString
& filename
)
194 if (m_Hash
== NULL
||
195 m_Hash
-> Get(filename
) == NULL
)
198 s
.Printf(_("Trying to remove file '%s' from memory VFS, but it is not loaded!"), filename
.c_str());
203 delete m_Hash
-> Delete(filename
);
212 wxMemoryFSHandler::AddFile(const wxString
& filename
, wxImage
& image
, long type
)
214 if (!CheckHash(filename
)) return;
216 wxMemoryOutputStream mems
;
217 if (image
.Ok() && image
.SaveFile(mems
, (int)type
))
218 m_Hash
-> Put(filename
, new MemFSHashObj(mems
));
222 s
.Printf(_("Failed to store image '%s' to memory VFS!"), filename
.c_str());
223 wxPrintf(wxT("'%s'\n"), s
.c_str());
227 #endif // wxUSE_IMAGE
229 /*static*/ void wxMemoryFSHandler::AddFile(const wxString
& filename
, const wxBitmap
& bitmap
, long type
)
231 wxImage img
= bitmap
.ConvertToImage();
232 AddFile(filename
, img
, type
);
238 #endif // wxUSE_FILESYSTEM && wxUSE_FS_ZIP