]> git.saurik.com Git - wxWidgets.git/blame - src/common/fs_mem.cpp
make our vsnprintf() implementation work for ANSI version too
[wxWidgets.git] / src / common / fs_mem.cpp
CommitLineData
dcb86da0 1/////////////////////////////////////////////////////////////////////////////
0bca0373 2// Name: src/common/fs_mem.cpp
dcb86da0
VS
3// Purpose: in-memory file system
4// Author: Vaclav Slavik
0bca0373 5// RCS-ID: $Id$
dcb86da0 6// Copyright: (c) 2000 Vaclav Slavik
65571936 7// Licence: wxWindows licence
dcb86da0
VS
8/////////////////////////////////////////////////////////////////////////////
9
dcb86da0
VS
10#include "wx/wxprec.h"
11
2b5f62a0 12#ifdef __BORLANDC__
0bca0373 13 #pragma hdrstop
dcb86da0
VS
14#endif
15
24528b0c 16#if wxUSE_FILESYSTEM && wxUSE_STREAMS
dcb86da0 17
e2478fde
VZ
18#include "wx/fs_mem.h"
19
b4f4d3dd 20#ifndef WX_PRECOMP
04dbb646
VZ
21 #include "wx/intl.h"
22 #include "wx/log.h"
8b70ca26 23 #include "wx/hash.h"
6b24b421 24 #include "wx/wxcrtvararg.h"
0bca0373
WS
25 #if wxUSE_GUI
26 #include "wx/bitmap.h"
155ecd4c 27 #include "wx/image.h"
0bca0373 28 #endif // wxUSE_GUI
dcb86da0
VS
29#endif
30
dcb86da0
VS
31#include "wx/mstream.h"
32
dcb86da0
VS
33class MemFSHashObj : public wxObject
34{
35 public:
04dbb646 36
dcb86da0
VS
37 MemFSHashObj(const void *data, size_t len)
38 {
39 m_Data = new char[len];
40 memcpy(m_Data, data, len);
41 m_Len = len;
e2b87f38 42 InitTime();
dcb86da0 43 }
04dbb646 44
fbfb8bcc 45 MemFSHashObj(const wxMemoryOutputStream& stream)
dcb86da0
VS
46 {
47 m_Len = stream.GetSize();
48 m_Data = new char[m_Len];
49 stream.CopyTo(m_Data, m_Len);
e2b87f38 50 InitTime();
dcb86da0 51 }
04dbb646 52
d3c7fc99 53 virtual ~MemFSHashObj()
dcb86da0
VS
54 {
55 delete[] m_Data;
56 }
04dbb646 57
dcb86da0
VS
58 char *m_Data;
59 size_t m_Len;
e2b87f38 60#if wxUSE_DATETIME
dcb86da0 61 wxDateTime m_Time;
e2b87f38 62#endif // wxUSE_DATETIME
22f3361e
VZ
63
64 DECLARE_NO_COPY_CLASS(MemFSHashObj)
e2b87f38
VZ
65
66 private:
67 void InitTime()
68 {
69#if wxUSE_DATETIME
70 m_Time = wxDateTime::Now();
a62848fd 71#endif // wxUSE_DATETIME
e2b87f38 72 }
dcb86da0
VS
73};
74
ec67cff1 75#if wxUSE_BASE
60c0a8db 76
dcb86da0
VS
77
78//--------------------------------------------------------------------------------
79// wxMemoryFSHandler
80//--------------------------------------------------------------------------------
81
82
e2478fde 83wxHashTable *wxMemoryFSHandlerBase::m_Hash = NULL;
dcb86da0
VS
84
85
e2478fde 86wxMemoryFSHandlerBase::wxMemoryFSHandlerBase() : wxFileSystemHandler()
dcb86da0 87{
dcb86da0
VS
88}
89
90
91
e2478fde 92wxMemoryFSHandlerBase::~wxMemoryFSHandlerBase()
dcb86da0
VS
93{
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.)
04dbb646 97
e2478fde
VZ
98 if (m_Hash)
99 {
df5168c4 100 WX_CLEAR_HASH_TABLE(*m_Hash);
e2478fde
VZ
101 delete m_Hash;
102 m_Hash = NULL;
103 }
dcb86da0
VS
104}
105
106
107
e2478fde 108bool wxMemoryFSHandlerBase::CanOpen(const wxString& location)
dcb86da0
VS
109{
110 wxString p = GetProtocol(location);
111 return (p == wxT("memory"));
112}
113
114
115
116
e2478fde 117wxFSFile* wxMemoryFSHandlerBase::OpenFile(wxFileSystem& WXUNUSED(fs), const wxString& location)
dcb86da0
VS
118{
119 if (m_Hash)
120 {
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),
124 location,
69cce151 125 wxEmptyString,
e2b87f38
VZ
126 GetAnchor(location)
127#if wxUSE_DATETIME
128 , obj -> m_Time
a62848fd 129#endif // wxUSE_DATETIME
e2b87f38 130 );
dcb86da0
VS
131 }
132 else return NULL;
133}
134
135
136
e2478fde 137wxString wxMemoryFSHandlerBase::FindFirst(const wxString& WXUNUSED(spec),
bc1dcfc1 138 int WXUNUSED(flags))
dcb86da0 139{
e2478fde 140 wxFAIL_MSG(wxT("wxMemoryFSHandlerBase::FindFirst not implemented"));
bc1dcfc1 141
dcb86da0
VS
142 return wxEmptyString;
143}
144
145
146
e2478fde 147wxString wxMemoryFSHandlerBase::FindNext()
dcb86da0 148{
e2478fde 149 wxFAIL_MSG(wxT("wxMemoryFSHandlerBase::FindNext not implemented"));
bc1dcfc1 150
dcb86da0
VS
151 return wxEmptyString;
152}
153
154
e2478fde 155bool wxMemoryFSHandlerBase::CheckHash(const wxString& filename)
dcb86da0 156{
04dbb646 157 if (m_Hash == NULL)
dcb86da0
VS
158 {
159 m_Hash = new wxHashTable(wxKEY_STRING);
dcb86da0 160 }
04dbb646 161
dcb86da0
VS
162 if (m_Hash -> Get(filename) != NULL)
163 {
164 wxString s;
165 s.Printf(_("Memory VFS already contains file '%s'!"), filename.c_str());
166 wxLogError(s);
a62848fd 167 return false;
dcb86da0
VS
168 }
169 else
a62848fd 170 return true;
dcb86da0
VS
171}
172
173
e2478fde 174/*static*/ void wxMemoryFSHandlerBase::AddFile(const wxString& filename, const wxString& textdata)
dcb86da0 175{
0bca0373 176 AddFile(filename, (const void*) textdata.mb_str(), textdata.length());
dcb86da0
VS
177}
178
179
e2478fde 180/*static*/ void wxMemoryFSHandlerBase::AddFile(const wxString& filename, const void *binarydata, size_t size)
dcb86da0
VS
181{
182 if (!CheckHash(filename)) return;
183 m_Hash -> Put(filename, new MemFSHashObj(binarydata, size));
184}
185
186
187
e2478fde 188/*static*/ void wxMemoryFSHandlerBase::RemoveFile(const wxString& filename)
dcb86da0
VS
189{
190 if (m_Hash == NULL ||
191 m_Hash -> Get(filename) == NULL)
192 {
193 wxString s;
194 s.Printf(_("Trying to remove file '%s' from memory VFS, but it is not loaded!"), filename.c_str());
195 wxLogError(s);
196 }
197
198 else
199 delete m_Hash -> Delete(filename);
200}
201
ec67cff1 202#endif // wxUSE_BASE
e2478fde
VZ
203
204#if wxUSE_GUI
205
2de5a6ee
VS
206#if wxUSE_IMAGE
207/*static*/ void
989a2421
VZ
208wxMemoryFSHandler::AddFile(const wxString& filename,
209 const wxImage& image,
210 long type)
2de5a6ee
VS
211{
212 if (!CheckHash(filename)) return;
213
2de5a6ee
VS
214 wxMemoryOutputStream mems;
215 if (image.Ok() && image.SaveFile(mems, (int)type))
216 m_Hash -> Put(filename, new MemFSHashObj(mems));
217 else
218 {
219 wxString s;
220 s.Printf(_("Failed to store image '%s' to memory VFS!"), filename.c_str());
221 wxPrintf(wxT("'%s'\n"), s.c_str());
222 wxLogError(s);
223 }
224}
2de5a6ee 225
989a2421
VZ
226/*static*/ void
227wxMemoryFSHandler::AddFile(const wxString& filename,
228 const wxBitmap& bitmap,
229 long type)
e2478fde 230{
64c288fa 231#if !defined(__WXMSW__) || wxUSE_WXDIB
e2478fde
VZ
232 wxImage img = bitmap.ConvertToImage();
233 AddFile(filename, img, type);
64c288fa 234#endif
e2478fde
VZ
235}
236
1904aa72
DS
237#endif // wxUSE_IMAGE
238
239#endif // wxUSE_GUI
dcb86da0
VS
240
241
24528b0c 242#endif // wxUSE_FILESYSTEM && wxUSE_FS_ZIP