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();
63 //--------------------------------------------------------------------------------
65 //--------------------------------------------------------------------------------
68 wxHashTable
*wxMemoryFSHandler::m_Hash
= NULL
;
71 wxMemoryFSHandler::wxMemoryFSHandler() : wxFileSystemHandler()
78 wxMemoryFSHandler::~wxMemoryFSHandler()
80 // as only one copy of FS handler is supposed to exist, we may silently
81 // delete static data here. (There is no way how to remove FS handler from
82 // wxFileSystem other than releasing _all_ handlers.)
84 if (m_Hash
) delete m_Hash
;
90 bool wxMemoryFSHandler::CanOpen(const wxString
& location
)
92 wxString p
= GetProtocol(location
);
93 return (p
== wxT("memory"));
99 wxFSFile
* wxMemoryFSHandler::OpenFile(wxFileSystem
& WXUNUSED(fs
), const wxString
& location
)
103 MemFSHashObj
*obj
= (MemFSHashObj
*) m_Hash
-> Get(GetRightLocation(location
));
104 if (obj
== NULL
) return NULL
;
105 else return new wxFSFile(new wxMemoryInputStream(obj
-> m_Data
, obj
-> m_Len
),
107 GetMimeTypeFromExt(location
),
116 wxString
wxMemoryFSHandler::FindFirst(const wxString
& WXUNUSED(spec
),
119 wxFAIL_MSG(wxT("wxMemoryFSHandler::FindFirst not implemented"));
121 return wxEmptyString
;
126 wxString
wxMemoryFSHandler::FindNext()
128 wxFAIL_MSG(wxT("wxMemoryFSHandler::FindNext not implemented"));
130 return wxEmptyString
;
134 bool wxMemoryFSHandler::CheckHash(const wxString
& filename
)
138 m_Hash
= new wxHashTable(wxKEY_STRING
);
139 m_Hash
-> DeleteContents(TRUE
);
142 if (m_Hash
-> Get(filename
) != NULL
)
145 s
.Printf(_("Memory VFS already contains file '%s'!"), filename
.c_str());
157 /*static*/ void wxMemoryFSHandler::AddFile(const wxString
& filename
, wxImage
& image
, long type
)
159 if (!CheckHash(filename
)) return;
162 wxMemoryOutputStream mems
;
163 if (image
.Ok() && image
.SaveFile(mems
, (int)type
))
164 m_Hash
-> Put(filename
, new MemFSHashObj(mems
));
168 s
.Printf(_("Failed to store image '%s' to memory VFS!"), filename
.c_str());
169 wxPrintf(wxT("'%s'\n"), s
.c_str());
175 /*static*/ void wxMemoryFSHandler::AddFile(const wxString
& filename
, const wxBitmap
& bitmap
, long type
)
177 wxImage img
= bitmap
.ConvertToImage();
178 AddFile(filename
, img
, type
);
183 /*static*/ void wxMemoryFSHandler::AddFile(const wxString
& filename
, const wxString
& textdata
)
185 AddFile(filename
, (const void*) textdata
.mb_str(), textdata
.Length());
189 /*static*/ void wxMemoryFSHandler::AddFile(const wxString
& filename
, const void *binarydata
, size_t size
)
191 if (!CheckHash(filename
)) return;
192 m_Hash
-> Put(filename
, new MemFSHashObj(binarydata
, size
));
197 /*static*/ void wxMemoryFSHandler::RemoveFile(const wxString
& filename
)
199 if (m_Hash
== NULL
||
200 m_Hash
-> Get(filename
) == NULL
)
203 s
.Printf(_("Trying to remove file '%s' from memory VFS, but it is not loaded!"), filename
.c_str());
208 delete m_Hash
-> Delete(filename
);
213 #endif // wxUSE_FILESYSTEM && wxUSE_FS_ZIP