]> git.saurik.com Git - wxWidgets.git/blob - src/common/fs_mem.cpp
don't check for connected state of UDP sockets which are never connected (closes...
[wxWidgets.git] / src / common / fs_mem.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/fs_mem.cpp
3 // Purpose: in-memory file system
4 // Author: Vaclav Slavik
5 // RCS-ID: $Id$
6 // Copyright: (c) 2000 Vaclav Slavik
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #include "wx/wxprec.h"
11
12 #ifdef __BORLANDC__
13 #pragma hdrstop
14 #endif
15
16 #if wxUSE_FILESYSTEM && wxUSE_STREAMS
17
18 #include "wx/fs_mem.h"
19
20 #ifndef WX_PRECOMP
21 #include "wx/intl.h"
22 #include "wx/log.h"
23 #include "wx/wxcrtvararg.h"
24 #if wxUSE_GUI
25 #include "wx/image.h"
26 #endif // wxUSE_GUI
27 #endif
28
29 #include "wx/mstream.h"
30
31 // represents a file entry in wxMemoryFS
32 class wxMemoryFSFile
33 {
34 public:
35 wxMemoryFSFile(const void *data, size_t len, const wxString& mime)
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
44 wxMemoryFSFile(const wxMemoryOutputStream& stream, const wxString& mime)
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
53 virtual ~wxMemoryFSFile()
54 {
55 delete[] m_Data;
56 }
57
58 char *m_Data;
59 size_t m_Len;
60 wxString m_MimeType;
61 #if wxUSE_DATETIME
62 wxDateTime m_Time;
63 #endif // wxUSE_DATETIME
64
65 private:
66 void InitTime()
67 {
68 #if wxUSE_DATETIME
69 m_Time = wxDateTime::Now();
70 #endif // wxUSE_DATETIME
71 }
72
73 wxDECLARE_NO_COPY_CLASS(wxMemoryFSFile);
74 };
75
76 #if wxUSE_BASE
77
78
79 //--------------------------------------------------------------------------------
80 // wxMemoryFSHandler
81 //--------------------------------------------------------------------------------
82
83
84 wxMemoryFSHash wxMemoryFSHandlerBase::m_Hash;
85
86
87 wxMemoryFSHandlerBase::wxMemoryFSHandlerBase() : wxFileSystemHandler()
88 {
89 }
90
91 wxMemoryFSHandlerBase::~wxMemoryFSHandlerBase()
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.)
96 WX_CLEAR_HASH_MAP(wxMemoryFSHash, m_Hash);
97 }
98
99 bool wxMemoryFSHandlerBase::CanOpen(const wxString& location)
100 {
101 return GetProtocol(location) == "memory";
102 }
103
104 wxFSFile * wxMemoryFSHandlerBase::OpenFile(wxFileSystem& WXUNUSED(fs),
105 const wxString& location)
106 {
107 wxMemoryFSHash::const_iterator i = m_Hash.find(GetRightLocation(location));
108 if ( i == m_Hash.end() )
109 return NULL;
110
111 const wxMemoryFSFile * const obj = i->second;
112
113 return new wxFSFile
114 (
115 new wxMemoryInputStream(obj->m_Data, obj->m_Len),
116 location,
117 obj->m_MimeType,
118 GetAnchor(location)
119 #if wxUSE_DATETIME
120 , obj->m_Time
121 #endif // wxUSE_DATETIME
122 );
123 }
124
125 wxString wxMemoryFSHandlerBase::FindFirst(const wxString& url, int flags)
126 {
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
134 const wxString spec = GetRightLocation(url);
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
139 return m_Hash.count(spec) ? url : wxString();
140 }
141 //else: deal with wildcards in FindNext()
142
143 m_findArgument = spec;
144 m_findIter = m_Hash.begin();
145
146 return FindNext();
147 }
148
149 wxString wxMemoryFSHandlerBase::FindNext()
150 {
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 {
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
160 const wxMemoryFSHash::const_iterator current = m_findIter;
161
162 if ( ++m_findIter == m_Hash.end() )
163 m_findArgument.clear();
164
165 if ( found )
166 return "memory:" + current->first;
167 }
168
169 return wxString();
170 }
171
172 bool wxMemoryFSHandlerBase::CheckDoesntExist(const wxString& filename)
173 {
174 if ( m_Hash.count(filename) )
175 {
176 wxLogError(_("Memory VFS already contains file '%s'!"), filename);
177 return false;
178 }
179
180 return true;
181 }
182
183
184 /*static*/
185 void wxMemoryFSHandlerBase::AddFileWithMimeType(const wxString& filename,
186 const wxString& textdata,
187 const wxString& mimetype)
188 {
189 #if wxUSE_UNICODE
190 const wxScopedCharBuffer data(textdata.To8BitData());
191 #else
192 const wxString& data = textdata;
193 #endif
194
195 AddFileWithMimeType(filename,
196 static_cast<const char *>(data), data.length(),
197 mimetype);
198 }
199
200
201 /*static*/
202 void wxMemoryFSHandlerBase::AddFileWithMimeType(const wxString& filename,
203 const void *binarydata, size_t size,
204 const wxString& mimetype)
205 {
206 if ( !CheckDoesntExist(filename) )
207 return;
208
209 m_Hash[filename] = new wxMemoryFSFile(binarydata, size, mimetype);
210 }
211
212 /*static*/
213 void wxMemoryFSHandlerBase::AddFile(const wxString& filename,
214 const wxString& textdata)
215 {
216 AddFileWithMimeType(filename, textdata, wxEmptyString);
217 }
218
219
220 /*static*/
221 void wxMemoryFSHandlerBase::AddFile(const wxString& filename,
222 const void *binarydata, size_t size)
223 {
224 AddFileWithMimeType(filename, binarydata, size, wxEmptyString);
225 }
226
227
228
229 /*static*/ void wxMemoryFSHandlerBase::RemoveFile(const wxString& filename)
230 {
231 wxMemoryFSHash::iterator i = m_Hash.find(filename);
232 if ( i == m_Hash.end() )
233 {
234 wxLogError(_("Trying to remove file '%s' from memory VFS, "
235 "but it is not loaded!"),
236 filename);
237 return;
238 }
239
240 delete i->second;
241 m_Hash.erase(i);
242 }
243
244 #endif // wxUSE_BASE
245
246 #if wxUSE_GUI
247
248 #if wxUSE_IMAGE
249 /*static*/ void
250 wxMemoryFSHandler::AddFile(const wxString& filename,
251 const wxImage& image,
252 wxBitmapType type)
253 {
254 if ( !CheckDoesntExist(filename) )
255 return;
256
257 wxMemoryOutputStream mems;
258 if ( image.Ok() && image.SaveFile(mems, type) )
259 {
260 m_Hash[filename] = new wxMemoryFSFile
261 (
262 mems,
263 wxImage::FindHandler(type)->GetMimeType()
264 );
265 }
266 else
267 {
268 wxLogError(_("Failed to store image '%s' to memory VFS!"), filename);
269 }
270 }
271
272 /*static*/ void
273 wxMemoryFSHandler::AddFile(const wxString& filename,
274 const wxBitmap& bitmap,
275 wxBitmapType type)
276 {
277 wxImage img = bitmap.ConvertToImage();
278 AddFile(filename, img, type);
279 }
280
281 #endif // wxUSE_IMAGE
282
283 #endif // wxUSE_GUI
284
285
286 #endif // wxUSE_FILESYSTEM && wxUSE_FS_ZIP