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" 
  24     #include "wx/wxcrtvararg.h" 
  26         #include "wx/bitmap.h" 
  31 #include "wx/mstream.h" 
  33 class MemFSHashObj 
: public wxObject
 
  37         MemFSHashObj(const void *data
, size_t len
) 
  39             m_Data 
= new char[len
]; 
  40             memcpy(m_Data
, data
, len
); 
  45         MemFSHashObj(const wxMemoryOutputStream
& stream
) 
  47             m_Len 
= stream
.GetSize(); 
  48             m_Data 
= new char[m_Len
]; 
  49             stream
.CopyTo(m_Data
, m_Len
); 
  53         virtual ~MemFSHashObj() 
  62 #endif // wxUSE_DATETIME 
  64     DECLARE_NO_COPY_CLASS(MemFSHashObj
) 
  70             m_Time 
= wxDateTime::Now(); 
  71 #endif // wxUSE_DATETIME 
  78 //-------------------------------------------------------------------------------- 
  80 //-------------------------------------------------------------------------------- 
  83 wxHashTable 
*wxMemoryFSHandlerBase::m_Hash 
= NULL
; 
  86 wxMemoryFSHandlerBase::wxMemoryFSHandlerBase() : wxFileSystemHandler() 
  92 wxMemoryFSHandlerBase::~wxMemoryFSHandlerBase() 
  94     // as only one copy of FS handler is supposed to exist, we may silently 
  95     // delete static data here. (There is no way how to remove FS handler from 
  96     // wxFileSystem other than releasing _all_ handlers.) 
 100         WX_CLEAR_HASH_TABLE(*m_Hash
); 
 108 bool wxMemoryFSHandlerBase::CanOpen(const wxString
& location
) 
 110     wxString p 
= GetProtocol(location
); 
 111     return (p 
== wxT("memory")); 
 117 wxFSFile
* wxMemoryFSHandlerBase::OpenFile(wxFileSystem
& WXUNUSED(fs
), const wxString
& location
) 
 121         MemFSHashObj 
*obj 
= (MemFSHashObj
*) m_Hash 
-> Get(GetRightLocation(location
)); 
 122         if (obj 
== NULL
)  return NULL
; 
 123         else return new wxFSFile(new wxMemoryInputStream(obj 
-> m_Data
, obj 
-> m_Len
), 
 125                             GetMimeTypeFromExt(location
), 
 129 #endif // wxUSE_DATETIME 
 137 wxString 
wxMemoryFSHandlerBase::FindFirst(const wxString
& WXUNUSED(spec
), 
 140     wxFAIL_MSG(wxT("wxMemoryFSHandlerBase::FindFirst not implemented")); 
 142     return wxEmptyString
; 
 147 wxString 
wxMemoryFSHandlerBase::FindNext() 
 149     wxFAIL_MSG(wxT("wxMemoryFSHandlerBase::FindNext not implemented")); 
 151     return wxEmptyString
; 
 155 bool wxMemoryFSHandlerBase::CheckHash(const wxString
& filename
) 
 159         m_Hash 
= new wxHashTable(wxKEY_STRING
); 
 162     if (m_Hash 
-> Get(filename
) != NULL
) 
 165         s
.Printf(_("Memory VFS already contains file '%s'!"), filename
.c_str()); 
 174 /*static*/ void wxMemoryFSHandlerBase::AddFile(const wxString
& filename
, const wxString
& textdata
) 
 176     AddFile(filename
, (const void*) textdata
.mb_str(), textdata
.length()); 
 180 /*static*/ void wxMemoryFSHandlerBase::AddFile(const wxString
& filename
, const void *binarydata
, size_t size
) 
 182     if (!CheckHash(filename
)) return; 
 183     m_Hash 
-> Put(filename
, new MemFSHashObj(binarydata
, size
)); 
 188 /*static*/ void wxMemoryFSHandlerBase::RemoveFile(const wxString
& filename
) 
 190     if (m_Hash 
== NULL 
|| 
 191         m_Hash 
-> Get(filename
) == NULL
) 
 194         s
.Printf(_("Trying to remove file '%s' from memory VFS, but it is not loaded!"), filename
.c_str()); 
 199         delete m_Hash 
-> Delete(filename
); 
 208 wxMemoryFSHandler::AddFile(const wxString
& filename
, 
 209                            const wxImage
& image
, 
 212     if (!CheckHash(filename
)) return; 
 214     wxMemoryOutputStream mems
; 
 215     if (image
.Ok() && image
.SaveFile(mems
, (int)type
)) 
 216         m_Hash 
-> Put(filename
, new MemFSHashObj(mems
)); 
 220         s
.Printf(_("Failed to store image '%s' to memory VFS!"), filename
.c_str()); 
 221         wxPrintf(wxT("'%s'\n"), s
.c_str()); 
 227 wxMemoryFSHandler::AddFile(const wxString
& filename
, 
 228                            const wxBitmap
& bitmap
, 
 231 #if !defined(__WXMSW__) || wxUSE_WXDIB 
 232     wxImage img 
= bitmap
.ConvertToImage(); 
 233     AddFile(filename
, img
, type
); 
 237 #endif // wxUSE_IMAGE 
 242 #endif // wxUSE_FILESYSTEM && wxUSE_FS_ZIP