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
28 #include "wx/filesys.h"
29 #include "wx/fs_mem.h"
30 #include "wx/mstream.h"
32 class MemFSHashObj
: public wxObject
36 MemFSHashObj(const void *data
, size_t len
)
38 m_Data
= new char[len
];
39 memcpy(m_Data
, data
, len
);
41 m_Time
= wxDateTime::Now();
44 MemFSHashObj(wxMemoryOutputStream
& stream
)
46 m_Len
= stream
.GetSize();
47 m_Data
= new char[m_Len
];
48 stream
.CopyTo(m_Data
, m_Len
);
49 m_Time
= wxDateTime::Now();
61 DECLARE_NO_COPY_CLASS(MemFSHashObj
)
65 //--------------------------------------------------------------------------------
67 //--------------------------------------------------------------------------------
70 wxHashTable
*wxMemoryFSHandler::m_Hash
= NULL
;
73 wxMemoryFSHandler::wxMemoryFSHandler() : wxFileSystemHandler()
79 wxMemoryFSHandler::~wxMemoryFSHandler()
81 // as only one copy of FS handler is supposed to exist, we may silently
82 // delete static data here. (There is no way how to remove FS handler from
83 // wxFileSystem other than releasing _all_ handlers.)
85 if (m_Hash
) delete m_Hash
;
91 bool wxMemoryFSHandler::CanOpen(const wxString
& location
)
93 wxString p
= GetProtocol(location
);
94 return (p
== wxT("memory"));
100 wxFSFile
* wxMemoryFSHandler::OpenFile(wxFileSystem
& WXUNUSED(fs
), const wxString
& location
)
104 MemFSHashObj
*obj
= (MemFSHashObj
*) m_Hash
-> Get(GetRightLocation(location
));
105 if (obj
== NULL
) return NULL
;
106 else return new wxFSFile(new wxMemoryInputStream(obj
-> m_Data
, obj
-> m_Len
),
108 GetMimeTypeFromExt(location
),
117 wxString
wxMemoryFSHandler::FindFirst(const wxString
& WXUNUSED(spec
),
120 wxFAIL_MSG(wxT("wxMemoryFSHandler::FindFirst not implemented"));
122 return wxEmptyString
;
127 wxString
wxMemoryFSHandler::FindNext()
129 wxFAIL_MSG(wxT("wxMemoryFSHandler::FindNext not implemented"));
131 return wxEmptyString
;
135 bool wxMemoryFSHandler::CheckHash(const wxString
& filename
)
139 m_Hash
= new wxHashTable(wxKEY_STRING
);
140 m_Hash
-> DeleteContents(TRUE
);
143 if (m_Hash
-> Get(filename
) != NULL
)
146 s
.Printf(_("Memory VFS already contains file '%s'!"), filename
.c_str());
158 /*static*/ void wxMemoryFSHandler::AddFile(const wxString
& filename
, wxImage
& image
, long type
)
160 if (!CheckHash(filename
)) return;
163 wxMemoryOutputStream mems
;
164 if (image
.Ok() && image
.SaveFile(mems
, (int)type
))
165 m_Hash
-> Put(filename
, new MemFSHashObj(mems
));
169 s
.Printf(_("Failed to store image '%s' to memory VFS!"), filename
.c_str());
170 wxPrintf(wxT("'%s'\n"), s
.c_str());
176 /*static*/ void wxMemoryFSHandler::AddFile(const wxString
& filename
, const wxBitmap
& bitmap
, long type
)
178 wxImage img
= bitmap
.ConvertToImage();
179 AddFile(filename
, img
, type
);
184 /*static*/ void wxMemoryFSHandler::AddFile(const wxString
& filename
, const wxString
& textdata
)
186 AddFile(filename
, (const void*) textdata
.mb_str(), textdata
.Length());
190 /*static*/ void wxMemoryFSHandler::AddFile(const wxString
& filename
, const void *binarydata
, size_t size
)
192 if (!CheckHash(filename
)) return;
193 m_Hash
-> Put(filename
, new MemFSHashObj(binarydata
, size
));
198 /*static*/ void wxMemoryFSHandler::RemoveFile(const wxString
& filename
)
200 if (m_Hash
== NULL
||
201 m_Hash
-> Get(filename
) == NULL
)
204 s
.Printf(_("Trying to remove file '%s' from memory VFS, but it is not loaded!"), filename
.c_str());
209 delete m_Hash
-> Delete(filename
);
214 #endif // wxUSE_FILESYSTEM && wxUSE_FS_ZIP