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
);
44 MemFSHashObj(wxMemoryOutputStream
& stream
)
46 m_Len
= stream
.GetSize();
47 m_Data
= new char[m_Len
];
48 stream
.CopyTo(m_Data
, m_Len
);
61 #endif // wxUSE_DATETIME
63 DECLARE_NO_COPY_CLASS(MemFSHashObj
)
69 m_Time
= wxDateTime::Now();
70 #endif // wxUSE_DATETIME
75 //--------------------------------------------------------------------------------
77 //--------------------------------------------------------------------------------
80 wxHashTable
*wxMemoryFSHandler::m_Hash
= NULL
;
83 wxMemoryFSHandler::wxMemoryFSHandler() : wxFileSystemHandler()
89 wxMemoryFSHandler::~wxMemoryFSHandler()
91 // as only one copy of FS handler is supposed to exist, we may silently
92 // delete static data here. (There is no way how to remove FS handler from
93 // wxFileSystem other than releasing _all_ handlers.)
95 if (m_Hash
) delete m_Hash
;
101 bool wxMemoryFSHandler::CanOpen(const wxString
& location
)
103 wxString p
= GetProtocol(location
);
104 return (p
== wxT("memory"));
110 wxFSFile
* wxMemoryFSHandler::OpenFile(wxFileSystem
& WXUNUSED(fs
), const wxString
& location
)
114 MemFSHashObj
*obj
= (MemFSHashObj
*) m_Hash
-> Get(GetRightLocation(location
));
115 if (obj
== NULL
) return NULL
;
116 else return new wxFSFile(new wxMemoryInputStream(obj
-> m_Data
, obj
-> m_Len
),
118 GetMimeTypeFromExt(location
),
122 #endif // wxUSE_DATETIME
130 wxString
wxMemoryFSHandler::FindFirst(const wxString
& WXUNUSED(spec
),
133 wxFAIL_MSG(wxT("wxMemoryFSHandler::FindFirst not implemented"));
135 return wxEmptyString
;
140 wxString
wxMemoryFSHandler::FindNext()
142 wxFAIL_MSG(wxT("wxMemoryFSHandler::FindNext not implemented"));
144 return wxEmptyString
;
148 bool wxMemoryFSHandler::CheckHash(const wxString
& filename
)
152 m_Hash
= new wxHashTable(wxKEY_STRING
);
153 m_Hash
-> DeleteContents(TRUE
);
156 if (m_Hash
-> Get(filename
) != NULL
)
159 s
.Printf(_("Memory VFS already contains file '%s'!"), filename
.c_str());
171 /*static*/ void wxMemoryFSHandler::AddFile(const wxString
& filename
, wxImage
& image
, long type
)
173 if (!CheckHash(filename
)) return;
176 wxMemoryOutputStream mems
;
177 if (image
.Ok() && image
.SaveFile(mems
, (int)type
))
178 m_Hash
-> Put(filename
, new MemFSHashObj(mems
));
182 s
.Printf(_("Failed to store image '%s' to memory VFS!"), filename
.c_str());
183 wxPrintf(wxT("'%s'\n"), s
.c_str());
189 /*static*/ void wxMemoryFSHandler::AddFile(const wxString
& filename
, const wxBitmap
& bitmap
, long type
)
191 wxImage img
= bitmap
.ConvertToImage();
192 AddFile(filename
, img
, type
);
197 /*static*/ void wxMemoryFSHandler::AddFile(const wxString
& filename
, const wxString
& textdata
)
199 AddFile(filename
, (const void*) textdata
.mb_str(), textdata
.Length());
203 /*static*/ void wxMemoryFSHandler::AddFile(const wxString
& filename
, const void *binarydata
, size_t size
)
205 if (!CheckHash(filename
)) return;
206 m_Hash
-> Put(filename
, new MemFSHashObj(binarydata
, size
));
211 /*static*/ void wxMemoryFSHandler::RemoveFile(const wxString
& filename
)
213 if (m_Hash
== NULL
||
214 m_Hash
-> Get(filename
) == NULL
)
217 s
.Printf(_("Trying to remove file '%s' from memory VFS, but it is not loaded!"), filename
.c_str());
222 delete m_Hash
-> Delete(filename
);
227 #endif // wxUSE_FILESYSTEM && wxUSE_FS_ZIP