]> git.saurik.com Git - wxWidgets.git/blame - src/common/fs_mem.cpp
no changes, just remove #if'd out code
[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"
6b24b421 23 #include "wx/wxcrtvararg.h"
0bca0373 24 #if wxUSE_GUI
155ecd4c 25 #include "wx/image.h"
0bca0373 26 #endif // wxUSE_GUI
dcb86da0
VS
27#endif
28
dcb86da0
VS
29#include "wx/mstream.h"
30
8c9d7602
VZ
31// represents a file entry in wxMemoryFS
32class wxMemoryFSFile
dcb86da0 33{
cb564879 34public:
8c9d7602 35 wxMemoryFSFile(const void *data, size_t len, const wxString& mime)
cb564879
VZ
36 {
37 m_Data = new char[len];
38 memcpy(m_Data, data, len);
39 m_Len = len;
40 m_MimeType = mime;
41 InitTime();
42 }
43
8c9d7602 44 wxMemoryFSFile(const wxMemoryOutputStream& stream, const wxString& mime)
cb564879
VZ
45 {
46 m_Len = stream.GetSize();
47 m_Data = new char[m_Len];
48 stream.CopyTo(m_Data, m_Len);
49 m_MimeType = mime;
50 InitTime();
51 }
52
8c9d7602 53 virtual ~wxMemoryFSFile()
cb564879
VZ
54 {
55 delete[] m_Data;
56 }
57
58 char *m_Data;
59 size_t m_Len;
60 wxString m_MimeType;
e2b87f38 61#if wxUSE_DATETIME
cb564879 62 wxDateTime m_Time;
e2b87f38 63#endif // wxUSE_DATETIME
22f3361e 64
cb564879
VZ
65private:
66 void InitTime()
67 {
e2b87f38 68#if wxUSE_DATETIME
cb564879 69 m_Time = wxDateTime::Now();
a62848fd 70#endif // wxUSE_DATETIME
cb564879 71 }
dcb86da0 72
c0c133e1 73 wxDECLARE_NO_COPY_CLASS(wxMemoryFSFile);
8c9d7602 74};
0eb2e510 75
ec67cff1 76#if wxUSE_BASE
60c0a8db 77
dcb86da0
VS
78
79//--------------------------------------------------------------------------------
80// wxMemoryFSHandler
81//--------------------------------------------------------------------------------
82
83
8c9d7602 84wxMemoryFSHash wxMemoryFSHandlerBase::m_Hash;
dcb86da0
VS
85
86
e2478fde 87wxMemoryFSHandlerBase::wxMemoryFSHandlerBase() : wxFileSystemHandler()
dcb86da0 88{
dcb86da0
VS
89}
90
e2478fde 91wxMemoryFSHandlerBase::~wxMemoryFSHandlerBase()
dcb86da0
VS
92{
93 // as only one copy of FS handler is supposed to exist, we may silently
94 // delete static data here. (There is no way how to remove FS handler from
95 // wxFileSystem other than releasing _all_ handlers.)
8c9d7602 96 WX_CLEAR_HASH_MAP(wxMemoryFSHash, m_Hash);
dcb86da0
VS
97}
98
e2478fde 99bool wxMemoryFSHandlerBase::CanOpen(const wxString& location)
dcb86da0 100{
cb564879 101 return GetProtocol(location) == "memory";
dcb86da0
VS
102}
103
cb564879
VZ
104wxFSFile * wxMemoryFSHandlerBase::OpenFile(wxFileSystem& WXUNUSED(fs),
105 const wxString& location)
106{
8c9d7602
VZ
107 wxMemoryFSHash::const_iterator i = m_Hash.find(GetRightLocation(location));
108 if ( i == m_Hash.end() )
cb564879 109 return NULL;
dcb86da0 110
8c9d7602 111 const wxMemoryFSFile * const obj = i->second;
0eb2e510
VZ
112
113 return new wxFSFile
114 (
115 new wxMemoryInputStream(obj->m_Data, obj->m_Len),
116 location,
117 obj->m_MimeType,
118 GetAnchor(location)
e2b87f38 119#if wxUSE_DATETIME
0eb2e510 120 , obj->m_Time
a62848fd 121#endif // wxUSE_DATETIME
0eb2e510 122 );
dcb86da0
VS
123}
124
c8c86bd0 125wxString wxMemoryFSHandlerBase::FindFirst(const wxString& url, int flags)
dcb86da0 126{
fcc65883
VZ
127 if ( (flags & wxDIR) && !(flags & wxFILE) )
128 {
129 // we only store files, not directories, so we don't risk finding
130 // anything
131 return wxString();
132 }
133
c8c86bd0 134 const wxString spec = GetRightLocation(url);
fcc65883
VZ
135 if ( spec.find_first_of("?*") == wxString::npos )
136 {
137 // simple case: there are no wildcard characters so we can return
138 // either 0 or 1 results and we can find the potential match quickly
c8c86bd0 139 return m_Hash.count(spec) ? url : wxString();
fcc65883
VZ
140 }
141 //else: deal with wildcards in FindNext()
bc1dcfc1 142
fcc65883
VZ
143 m_findArgument = spec;
144 m_findIter = m_Hash.begin();
145
146 return FindNext();
dcb86da0
VS
147}
148
e2478fde 149wxString wxMemoryFSHandlerBase::FindNext()
dcb86da0 150{
fcc65883
VZ
151 // m_findArgument is used to indicate that search is in progress, we reset
152 // it to empty string after iterating over all elements
153 while ( !m_findArgument.empty() )
154 {
c8c86bd0
VZ
155 // test for the match before (possibly) clearing m_findArgument below
156 const bool found = m_findIter->first.Matches(m_findArgument);
157
158 // advance m_findIter first as we need to do it anyhow, whether it
159 // matches or not
fcc65883
VZ
160 const wxMemoryFSHash::const_iterator current = m_findIter;
161
162 if ( ++m_findIter == m_Hash.end() )
163 m_findArgument.clear();
164
c8c86bd0
VZ
165 if ( found )
166 return "memory:" + current->first;
fcc65883 167 }
bc1dcfc1 168
fcc65883 169 return wxString();
dcb86da0
VS
170}
171
8c9d7602 172bool wxMemoryFSHandlerBase::CheckDoesntExist(const wxString& filename)
dcb86da0 173{
8c9d7602 174 if ( m_Hash.count(filename) )
dcb86da0 175 {
cb564879 176 wxLogError(_("Memory VFS already contains file '%s'!"), filename);
a62848fd 177 return false;
dcb86da0 178 }
cb564879
VZ
179
180 return true;
dcb86da0
VS
181}
182
183
c5d7b81e
VS
184/*static*/
185void wxMemoryFSHandlerBase::AddFileWithMimeType(const wxString& filename,
186 const wxString& textdata,
187 const wxString& mimetype)
dcb86da0 188{
8f1a82e6
VZ
189#if wxUSE_UNICODE
190 const wxScopedCharBuffer data(textdata.To8BitData());
191#else
192 const wxString& data = textdata;
193#endif
194
c5d7b81e 195 AddFileWithMimeType(filename,
8f1a82e6 196 static_cast<const char *>(data), data.length(),
c5d7b81e 197 mimetype);
dcb86da0
VS
198}
199
200
c5d7b81e
VS
201/*static*/
202void wxMemoryFSHandlerBase::AddFileWithMimeType(const wxString& filename,
203 const void *binarydata, size_t size,
204 const wxString& mimetype)
dcb86da0 205{
8c9d7602 206 if ( !CheckDoesntExist(filename) )
cb564879
VZ
207 return;
208
8c9d7602 209 m_Hash[filename] = new wxMemoryFSFile(binarydata, size, mimetype);
c5d7b81e
VS
210}
211
212/*static*/
213void wxMemoryFSHandlerBase::AddFile(const wxString& filename,
214 const wxString& textdata)
215{
216 AddFileWithMimeType(filename, textdata, wxEmptyString);
217}
218
219
220/*static*/
221void wxMemoryFSHandlerBase::AddFile(const wxString& filename,
222 const void *binarydata, size_t size)
223{
224 AddFileWithMimeType(filename, binarydata, size, wxEmptyString);
dcb86da0
VS
225}
226
227
228
e2478fde 229/*static*/ void wxMemoryFSHandlerBase::RemoveFile(const wxString& filename)
dcb86da0 230{
8c9d7602
VZ
231 wxMemoryFSHash::iterator i = m_Hash.find(filename);
232 if ( i == m_Hash.end() )
dcb86da0 233 {
8c9d7602
VZ
234 wxLogError(_("Trying to remove file '%s' from memory VFS, "
235 "but it is not loaded!"),
236 filename);
237 return;
cb564879 238 }
0eb2e510 239
8c9d7602
VZ
240 delete i->second;
241 m_Hash.erase(i);
dcb86da0
VS
242}
243
ec67cff1 244#endif // wxUSE_BASE
e2478fde
VZ
245
246#if wxUSE_GUI
247
2de5a6ee
VS
248#if wxUSE_IMAGE
249/*static*/ void
989a2421
VZ
250wxMemoryFSHandler::AddFile(const wxString& filename,
251 const wxImage& image,
d75a69e8 252 wxBitmapType type)
2de5a6ee 253{
8c9d7602 254 if ( !CheckDoesntExist(filename) )
cb564879 255 return;
2de5a6ee 256
2de5a6ee 257 wxMemoryOutputStream mems;
0eb2e510 258 if ( image.Ok() && image.SaveFile(mems, type) )
c5d7b81e 259 {
8c9d7602
VZ
260 m_Hash[filename] = new wxMemoryFSFile
261 (
0eb2e510
VZ
262 mems,
263 wxImage::FindHandler(type)->GetMimeType()
8c9d7602 264 );
c5d7b81e 265 }
2de5a6ee
VS
266 else
267 {
cb564879 268 wxLogError(_("Failed to store image '%s' to memory VFS!"), filename);
2de5a6ee
VS
269 }
270}
2de5a6ee 271
989a2421
VZ
272/*static*/ void
273wxMemoryFSHandler::AddFile(const wxString& filename,
274 const wxBitmap& bitmap,
d75a69e8 275 wxBitmapType type)
e2478fde
VZ
276{
277 wxImage img = bitmap.ConvertToImage();
278 AddFile(filename, img, type);
279}
280
1904aa72
DS
281#endif // wxUSE_IMAGE
282
283#endif // wxUSE_GUI
dcb86da0
VS
284
285
24528b0c 286#endif // wxUSE_FILESYSTEM && wxUSE_FS_ZIP