1 ///////////////////////////////////////////////////////////////////////////// 
   3 // Purpose:     in-memory file system 
   4 // Author:      Vaclav Slavik 
   5 // Copyright:   (c) 2000 Vaclav Slavik 
   6 // Licence:     wxWindows licence 
   7 ///////////////////////////////////////////////////////////////////////////// 
  10 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) && !defined(__EMX__) 
  11 // Some older compilers (such as EMX) cannot handle 
  12 // #pragma interface/implementation correctly, iff 
  13 // #pragma implementation is used in _two_ translation 
  14 // units (as created by e.g. event.cpp compiled for 
  15 // libwx_base and event.cpp compiled for libwx_gui_core). 
  16 // So we must not use those pragmas for those compilers in 
  18 #pragma implementation "fs_mem.h" 
  21 #include "wx/wxprec.h" 
  27 #if wxUSE_FILESYSTEM && wxUSE_STREAMS 
  29 #include "wx/fs_mem.h" 
  33     #include "wx/bitmap.h" 
  42 #include "wx/mstream.h" 
  44 class MemFSHashObj 
: public wxObject
 
  48         MemFSHashObj(const void *data
, size_t len
) 
  50             m_Data 
= new char[len
]; 
  51             memcpy(m_Data
, data
, len
); 
  56         MemFSHashObj(wxMemoryOutputStream
& stream
) 
  58             m_Len 
= stream
.GetSize(); 
  59             m_Data 
= new char[m_Len
]; 
  60             stream
.CopyTo(m_Data
, m_Len
); 
  73 #endif // wxUSE_DATETIME 
  75     DECLARE_NO_COPY_CLASS(MemFSHashObj
) 
  81             m_Time 
= wxDateTime::Now(); 
  82 #endif // wxUSE_DATETIME 
  89 //-------------------------------------------------------------------------------- 
  91 //-------------------------------------------------------------------------------- 
  94 wxHashTable 
*wxMemoryFSHandlerBase::m_Hash 
= NULL
; 
  97 wxMemoryFSHandlerBase::wxMemoryFSHandlerBase() : wxFileSystemHandler() 
 103 wxMemoryFSHandlerBase::~wxMemoryFSHandlerBase() 
 105     // as only one copy of FS handler is supposed to exist, we may silently 
 106     // delete static data here. (There is no way how to remove FS handler from 
 107     // wxFileSystem other than releasing _all_ handlers.) 
 111         WX_CLEAR_HASH_TABLE(*m_Hash
); 
 119 bool wxMemoryFSHandlerBase::CanOpen(const wxString
& location
) 
 121     wxString p 
= GetProtocol(location
); 
 122     return (p 
== wxT("memory")); 
 128 wxFSFile
* wxMemoryFSHandlerBase::OpenFile(wxFileSystem
& WXUNUSED(fs
), const wxString
& location
) 
 132         MemFSHashObj 
*obj 
= (MemFSHashObj
*) m_Hash 
-> Get(GetRightLocation(location
)); 
 133         if (obj 
== NULL
)  return NULL
; 
 134         else return new wxFSFile(new wxMemoryInputStream(obj 
-> m_Data
, obj 
-> m_Len
), 
 136                             GetMimeTypeFromExt(location
), 
 140 #endif // wxUSE_DATETIME 
 148 wxString 
wxMemoryFSHandlerBase::FindFirst(const wxString
& WXUNUSED(spec
), 
 151     wxFAIL_MSG(wxT("wxMemoryFSHandlerBase::FindFirst not implemented")); 
 153     return wxEmptyString
; 
 158 wxString 
wxMemoryFSHandlerBase::FindNext() 
 160     wxFAIL_MSG(wxT("wxMemoryFSHandlerBase::FindNext not implemented")); 
 162     return wxEmptyString
; 
 166 bool wxMemoryFSHandlerBase::CheckHash(const wxString
& filename
) 
 170         m_Hash 
= new wxHashTable(wxKEY_STRING
); 
 173     if (m_Hash 
-> Get(filename
) != NULL
) 
 176         s
.Printf(_("Memory VFS already contains file '%s'!"), filename
.c_str()); 
 185 /*static*/ void wxMemoryFSHandlerBase::AddFile(const wxString
& filename
, const wxString
& textdata
) 
 187     AddFile(filename
, (const void*) textdata
.mb_str(), textdata
.Length()); 
 191 /*static*/ void wxMemoryFSHandlerBase::AddFile(const wxString
& filename
, const void *binarydata
, size_t size
) 
 193     if (!CheckHash(filename
)) return; 
 194     m_Hash 
-> Put(filename
, new MemFSHashObj(binarydata
, size
)); 
 199 /*static*/ void wxMemoryFSHandlerBase::RemoveFile(const wxString
& filename
) 
 201     if (m_Hash 
== NULL 
|| 
 202         m_Hash 
-> Get(filename
) == NULL
) 
 205         s
.Printf(_("Trying to remove file '%s' from memory VFS, but it is not loaded!"), filename
.c_str()); 
 210         delete m_Hash 
-> Delete(filename
); 
 219 wxMemoryFSHandler::AddFile(const wxString
& filename
, 
 220                            const wxImage
& image
, 
 223     if (!CheckHash(filename
)) return; 
 225     wxMemoryOutputStream mems
; 
 226     if (image
.Ok() && image
.SaveFile(mems
, (int)type
)) 
 227         m_Hash 
-> Put(filename
, new MemFSHashObj(mems
)); 
 231         s
.Printf(_("Failed to store image '%s' to memory VFS!"), filename
.c_str()); 
 232         wxPrintf(wxT("'%s'\n"), s
.c_str()); 
 238 wxMemoryFSHandler::AddFile(const wxString
& filename
, 
 239                            const wxBitmap
& bitmap
, 
 242     wxImage img 
= bitmap
.ConvertToImage(); 
 243     AddFile(filename
, img
, type
); 
 246 #endif // wxUSE_IMAGE 
 251 #endif // wxUSE_FILESYSTEM && wxUSE_FS_ZIP