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
26 #include "wx/filesys.h"
27 #include "wx/fs_mem.h"
28 #include "wx/mstream.h"
30 class MemFSHashObj
: public wxObject
34 MemFSHashObj(const void *data
, size_t len
)
36 m_Data
= new char[len
];
37 memcpy(m_Data
, data
, len
);
39 m_Time
= wxDateTime::Now();
42 MemFSHashObj(wxMemoryOutputStream
& stream
)
44 m_Len
= stream
.GetSize();
45 m_Data
= new char[m_Len
];
46 stream
.CopyTo(m_Data
, m_Len
);
47 m_Time
= wxDateTime::Now();
61 //--------------------------------------------------------------------------------
63 //--------------------------------------------------------------------------------
66 wxHashTable
*wxMemoryFSHandler::m_Hash
= NULL
;
69 wxMemoryFSHandler::wxMemoryFSHandler() : wxFileSystemHandler()
76 wxMemoryFSHandler::~wxMemoryFSHandler()
78 // as only one copy of FS handler is supposed to exist, we may silently
79 // delete static data here. (There is no way how to remove FS handler from
80 // wxFileSystem other than releasing _all_ handlers.)
82 if (m_Hash
) delete m_Hash
;
88 bool wxMemoryFSHandler::CanOpen(const wxString
& location
)
90 wxString p
= GetProtocol(location
);
91 return (p
== wxT("memory"));
97 wxFSFile
* wxMemoryFSHandler::OpenFile(wxFileSystem
& WXUNUSED(fs
), const wxString
& location
)
101 MemFSHashObj
*obj
= (MemFSHashObj
*) m_Hash
-> Get(GetRightLocation(location
));
102 if (obj
== NULL
) return NULL
;
103 else return new wxFSFile(new wxMemoryInputStream(obj
-> m_Data
, obj
-> m_Len
),
105 GetMimeTypeFromExt(location
),
114 wxString
wxMemoryFSHandler::FindFirst(const wxString
& WXUNUSED(spec
),
117 wxFAIL_MSG(wxT("wxMemoryFSHandler::FindFirst not implemented"));
119 return wxEmptyString
;
124 wxString
wxMemoryFSHandler::FindNext()
126 wxFAIL_MSG(wxT("wxMemoryFSHandler::FindNext not implemented"));
128 return wxEmptyString
;
132 bool wxMemoryFSHandler::CheckHash(const wxString
& filename
)
136 m_Hash
= new wxHashTable(wxKEY_STRING
);
137 m_Hash
-> DeleteContents(TRUE
);
140 if (m_Hash
-> Get(filename
) != NULL
)
143 s
.Printf(_("Memory VFS already contains file '%s'!"), filename
.c_str());
155 /*static*/ void wxMemoryFSHandler::AddFile(const wxString
& filename
, wxImage
& image
, long type
)
157 if (!CheckHash(filename
)) return;
160 wxMemoryOutputStream mems
;
161 if (image
.Ok() && image
.SaveFile(mems
, (int)type
))
162 m_Hash
-> Put(filename
, new MemFSHashObj(mems
));
166 s
.Printf(_("Failed to store image '%s' to memory VFS!"), filename
.c_str());
167 wxPrintf(wxT("'%s'\n"), s
.c_str());
173 /*static*/ void wxMemoryFSHandler::AddFile(const wxString
& filename
, const wxBitmap
& bitmap
, long type
)
176 AddFile(filename
, img
, type
);
181 /*static*/ void wxMemoryFSHandler::AddFile(const wxString
& filename
, const wxString
& textdata
)
183 AddFile(filename
, (const void*) textdata
.mb_str(), textdata
.Length());
187 /*static*/ void wxMemoryFSHandler::AddFile(const wxString
& filename
, const void *binarydata
, size_t size
)
189 if (!CheckHash(filename
)) return;
190 m_Hash
-> Put(filename
, new MemFSHashObj(binarydata
, size
));
195 /*static*/ void wxMemoryFSHandler::RemoveFile(const wxString
& filename
)
197 if (m_Hash
== NULL
||
198 m_Hash
-> Get(filename
) == NULL
)
201 s
.Printf(_("Trying to remove file '%s' from memory VFS, but it is not loaded!"), filename
.c_str());
206 delete m_Hash
-> Delete(filename
);
211 #endif // wxUSE_FILESYSTEM && wxUSE_FS_ZIP