1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/fs_mem.cpp
3 // Purpose: in-memory file system
4 // Author: Vaclav Slavik
6 // Copyright: (c) 2000 Vaclav Slavik
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #include "wx/wxprec.h"
16 #if wxUSE_FILESYSTEM && wxUSE_STREAMS
18 #include "wx/fs_mem.h"
23 #include "wx/hashmap.h"
24 #include "wx/wxcrtvararg.h"
30 #include "wx/mstream.h"
35 MemFSHashObj(const void *data
, size_t len
, const wxString
& mime
)
37 m_Data
= new char[len
];
38 memcpy(m_Data
, data
, len
);
44 MemFSHashObj(const wxMemoryOutputStream
& stream
, const wxString
& mime
)
46 m_Len
= stream
.GetSize();
47 m_Data
= new char[m_Len
];
48 stream
.CopyTo(m_Data
, m_Len
);
53 virtual ~MemFSHashObj()
63 #endif // wxUSE_DATETIME
65 DECLARE_NO_COPY_CLASS(MemFSHashObj
)
71 m_Time
= wxDateTime::Now();
72 #endif // wxUSE_DATETIME
76 WX_DECLARE_STRING_HASH_MAP(MemFSHashObj
*, wxMemoryFSHash
);
81 //--------------------------------------------------------------------------------
83 //--------------------------------------------------------------------------------
86 wxMemoryFSHash
*wxMemoryFSHandlerBase::m_Hash
= NULL
;
89 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.)
101 WX_CLEAR_HASH_MAP(wxMemoryFSHash
, *m_Hash
);
107 bool wxMemoryFSHandlerBase::CanOpen(const wxString
& location
)
109 return GetProtocol(location
) == "memory";
112 wxFSFile
* wxMemoryFSHandlerBase::OpenFile(wxFileSystem
& WXUNUSED(fs
),
113 const wxString
& location
)
118 wxMemoryFSHash::const_iterator i
= m_Hash
->find(GetRightLocation(location
));
119 if ( i
== m_Hash
->end() )
122 const MemFSHashObj
* const obj
= i
->second
;
126 new wxMemoryInputStream(obj
->m_Data
, obj
->m_Len
),
132 #endif // wxUSE_DATETIME
136 wxString
wxMemoryFSHandlerBase::FindFirst(const wxString
& WXUNUSED(spec
),
139 wxFAIL_MSG(wxT("wxMemoryFSHandlerBase::FindFirst not implemented"));
141 return wxEmptyString
;
144 wxString
wxMemoryFSHandlerBase::FindNext()
146 wxFAIL_MSG(wxT("wxMemoryFSHandlerBase::FindNext not implemented"));
148 return wxEmptyString
;
151 bool wxMemoryFSHandlerBase::CheckHash(const wxString
& filename
)
154 m_Hash
= new wxMemoryFSHash
;
156 if ( m_Hash
->count(filename
) )
158 wxLogError(_("Memory VFS already contains file '%s'!"), filename
);
167 void wxMemoryFSHandlerBase::AddFileWithMimeType(const wxString
& filename
,
168 const wxString
& textdata
,
169 const wxString
& mimetype
)
171 AddFileWithMimeType(filename
,
172 (const void*) textdata
.mb_str(), textdata
.length(),
178 void wxMemoryFSHandlerBase::AddFileWithMimeType(const wxString
& filename
,
179 const void *binarydata
, size_t size
,
180 const wxString
& mimetype
)
182 if ( !CheckHash(filename
) )
185 (*m_Hash
)[filename
] = new MemFSHashObj(binarydata
, size
, mimetype
);
189 void wxMemoryFSHandlerBase::AddFile(const wxString
& filename
,
190 const wxString
& textdata
)
192 AddFileWithMimeType(filename
, textdata
, wxEmptyString
);
197 void wxMemoryFSHandlerBase::AddFile(const wxString
& filename
,
198 const void *binarydata
, size_t size
)
200 AddFileWithMimeType(filename
, binarydata
, size
, wxEmptyString
);
205 /*static*/ void wxMemoryFSHandlerBase::RemoveFile(const wxString
& filename
)
209 wxMemoryFSHash::iterator i
= m_Hash
->find(filename
);
210 if ( i
!= m_Hash
->end() )
217 wxLogError(_("Trying to remove file '%s' from memory VFS, "
218 "but it is not loaded!"),
228 wxMemoryFSHandler::AddFile(const wxString
& filename
,
229 const wxImage
& image
,
232 if ( !CheckHash(filename
) )
235 wxMemoryOutputStream mems
;
236 if ( image
.Ok() && image
.SaveFile(mems
, type
) )
238 (*m_Hash
)[filename
] = new MemFSHashObj
241 wxImage::FindHandler(type
)->GetMimeType()
246 wxLogError(_("Failed to store image '%s' to memory VFS!"), filename
);
251 wxMemoryFSHandler::AddFile(const wxString
& filename
,
252 const wxBitmap
& bitmap
,
255 wxImage img
= bitmap
.ConvertToImage();
256 AddFile(filename
, img
, type
);
259 #endif // wxUSE_IMAGE
264 #endif // wxUSE_FILESYSTEM && wxUSE_FS_ZIP