1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: in-memory file system
4 // Author: Vaclav Slavik
5 // Copyright: (c) 2000 Vaclav Slavik
6 // Licence: wxWindows Licence
7 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "fs_mem.h"
14 #include "wx/wxprec.h"
20 #if wxUSE_FILESYSTEM && wxUSE_STREAMS
23 #include "wx/bitmap.h"
24 #include "wx/fs_mem.h"
34 #include "wx/mstream.h"
36 class MemFSHashObj
: public wxObject
40 MemFSHashObj(const void *data
, size_t len
)
42 m_Data
= new char[len
];
43 memcpy(m_Data
, data
, len
);
48 MemFSHashObj(wxMemoryOutputStream
& stream
)
50 m_Len
= stream
.GetSize();
51 m_Data
= new char[m_Len
];
52 stream
.CopyTo(m_Data
, m_Len
);
65 #endif // wxUSE_DATETIME
67 DECLARE_NO_COPY_CLASS(MemFSHashObj
)
73 m_Time
= wxDateTime::Now();
74 #endif // wxUSE_DATETIME
79 //--------------------------------------------------------------------------------
81 //--------------------------------------------------------------------------------
84 wxHashTable
*wxMemoryFSHandlerBase::m_Hash
= NULL
;
87 wxMemoryFSHandlerBase::wxMemoryFSHandlerBase() : wxFileSystemHandler()
93 wxMemoryFSHandlerBase::~wxMemoryFSHandlerBase()
95 // as only one copy of FS handler is supposed to exist, we may silently
96 // delete static data here. (There is no way how to remove FS handler from
97 // wxFileSystem other than releasing _all_ handlers.)
108 bool wxMemoryFSHandlerBase::CanOpen(const wxString
& location
)
110 wxString p
= GetProtocol(location
);
111 return (p
== wxT("memory"));
117 wxFSFile
* wxMemoryFSHandlerBase::OpenFile(wxFileSystem
& WXUNUSED(fs
), const wxString
& location
)
121 MemFSHashObj
*obj
= (MemFSHashObj
*) m_Hash
-> Get(GetRightLocation(location
));
122 if (obj
== NULL
) return NULL
;
123 else return new wxFSFile(new wxMemoryInputStream(obj
-> m_Data
, obj
-> m_Len
),
125 GetMimeTypeFromExt(location
),
129 #endif // wxUSE_DATETIME
137 wxString
wxMemoryFSHandlerBase::FindFirst(const wxString
& WXUNUSED(spec
),
140 wxFAIL_MSG(wxT("wxMemoryFSHandlerBase::FindFirst not implemented"));
142 return wxEmptyString
;
147 wxString
wxMemoryFSHandlerBase::FindNext()
149 wxFAIL_MSG(wxT("wxMemoryFSHandlerBase::FindNext not implemented"));
151 return wxEmptyString
;
155 bool wxMemoryFSHandlerBase::CheckHash(const wxString
& filename
)
159 m_Hash
= new wxHashTable(wxKEY_STRING
);
160 m_Hash
-> DeleteContents(TRUE
);
163 if (m_Hash
-> Get(filename
) != NULL
)
166 s
.Printf(_("Memory VFS already contains file '%s'!"), filename
.c_str());
177 wxMemoryFSHandlerBase::AddFile(const wxString
& filename
, wxImage
& image
, long type
)
179 if (!CheckHash(filename
)) return;
182 wxMemoryOutputStream mems
;
183 if (image
.Ok() && image
.SaveFile(mems
, (int)type
))
184 m_Hash
-> Put(filename
, new MemFSHashObj(mems
));
188 s
.Printf(_("Failed to store image '%s' to memory VFS!"), filename
.c_str());
189 wxPrintf(wxT("'%s'\n"), s
.c_str());
195 /*static*/ void wxMemoryFSHandlerBase::AddFile(const wxString
& filename
, const wxString
& textdata
)
197 AddFile(filename
, (const void*) textdata
.mb_str(), textdata
.Length());
201 /*static*/ void wxMemoryFSHandlerBase::AddFile(const wxString
& filename
, const void *binarydata
, size_t size
)
203 if (!CheckHash(filename
)) return;
204 m_Hash
-> Put(filename
, new MemFSHashObj(binarydata
, size
));
209 /*static*/ void wxMemoryFSHandlerBase::RemoveFile(const wxString
& filename
)
211 if (m_Hash
== NULL
||
212 m_Hash
-> Get(filename
) == NULL
)
215 s
.Printf(_("Trying to remove file '%s' from memory VFS, but it is not loaded!"), filename
.c_str());
220 delete m_Hash
-> Delete(filename
);
227 /*static*/ void wxMemoryFSHandler::AddFile(const wxString
& filename
, const wxBitmap
& bitmap
, long type
)
229 wxImage img
= bitmap
.ConvertToImage();
230 AddFile(filename
, img
, type
);
236 #endif // wxUSE_FILESYSTEM && wxUSE_FS_ZIP