]> git.saurik.com Git - wxWidgets.git/blob - src/common/fs_mem.cpp
wxBase/GUI separation: 1st step, wxMSW should build, all the rest is broken
[wxWidgets.git] / src / common / fs_mem.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: fs_mem.cpp
3 // Purpose: in-memory file system
4 // Author: Vaclav Slavik
5 // Copyright: (c) 2000 Vaclav Slavik
6 // Licence: wxWindows Licence
7 /////////////////////////////////////////////////////////////////////////////
8
9
10 #ifdef __GNUG__
11 #pragma implementation "fs_mem.h"
12 #endif
13
14 #include "wx/wxprec.h"
15
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19
20 #if wxUSE_FILESYSTEM && wxUSE_STREAMS
21
22 #include "wx/image.h"
23 #include "wx/bitmap.h"
24 #include "wx/fs_mem.h"
25
26 #ifdef __WXBASE__
27
28 #ifndef WXPRECOMP
29 #include "wx/intl.h"
30 #include "wx/log.h"
31 #include "wx/hash.h"
32 #endif
33
34 #include "wx/mstream.h"
35
36 class MemFSHashObj : public wxObject
37 {
38 public:
39
40 MemFSHashObj(const void *data, size_t len)
41 {
42 m_Data = new char[len];
43 memcpy(m_Data, data, len);
44 m_Len = len;
45 InitTime();
46 }
47
48 MemFSHashObj(wxMemoryOutputStream& stream)
49 {
50 m_Len = stream.GetSize();
51 m_Data = new char[m_Len];
52 stream.CopyTo(m_Data, m_Len);
53 InitTime();
54 }
55
56 ~MemFSHashObj()
57 {
58 delete[] m_Data;
59 }
60
61 char *m_Data;
62 size_t m_Len;
63 #if wxUSE_DATETIME
64 wxDateTime m_Time;
65 #endif // wxUSE_DATETIME
66
67 DECLARE_NO_COPY_CLASS(MemFSHashObj)
68
69 private:
70 void InitTime()
71 {
72 #if wxUSE_DATETIME
73 m_Time = wxDateTime::Now();
74 #endif // wxUSE_DATETIME
75 }
76 };
77
78
79 //--------------------------------------------------------------------------------
80 // wxMemoryFSHandler
81 //--------------------------------------------------------------------------------
82
83
84 wxHashTable *wxMemoryFSHandlerBase::m_Hash = NULL;
85
86
87 wxMemoryFSHandlerBase::wxMemoryFSHandlerBase() : wxFileSystemHandler()
88 {
89 }
90
91
92
93 wxMemoryFSHandlerBase::~wxMemoryFSHandlerBase()
94 {
95 // as only one copy of FS handler is supposed to exist, we may silently
96 // delete static data here. (There is no way how to remove FS handler from
97 // wxFileSystem other than releasing _all_ handlers.)
98
99 if (m_Hash)
100 {
101 delete m_Hash;
102 m_Hash = NULL;
103 }
104 }
105
106
107
108 bool wxMemoryFSHandlerBase::CanOpen(const wxString& location)
109 {
110 wxString p = GetProtocol(location);
111 return (p == wxT("memory"));
112 }
113
114
115
116
117 wxFSFile* wxMemoryFSHandlerBase::OpenFile(wxFileSystem& WXUNUSED(fs), const wxString& location)
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,
125 GetMimeTypeFromExt(location),
126 GetAnchor(location)
127 #if wxUSE_DATETIME
128 , obj -> m_Time
129 #endif // wxUSE_DATETIME
130 );
131 }
132 else return NULL;
133 }
134
135
136
137 wxString wxMemoryFSHandlerBase::FindFirst(const wxString& WXUNUSED(spec),
138 int WXUNUSED(flags))
139 {
140 wxFAIL_MSG(wxT("wxMemoryFSHandlerBase::FindFirst not implemented"));
141
142 return wxEmptyString;
143 }
144
145
146
147 wxString wxMemoryFSHandlerBase::FindNext()
148 {
149 wxFAIL_MSG(wxT("wxMemoryFSHandlerBase::FindNext not implemented"));
150
151 return wxEmptyString;
152 }
153
154
155 bool wxMemoryFSHandlerBase::CheckHash(const wxString& filename)
156 {
157 if (m_Hash == NULL)
158 {
159 m_Hash = new wxHashTable(wxKEY_STRING);
160 m_Hash -> DeleteContents(TRUE);
161 }
162
163 if (m_Hash -> Get(filename) != NULL)
164 {
165 wxString s;
166 s.Printf(_("Memory VFS already contains file '%s'!"), filename.c_str());
167 wxLogError(s);
168 return FALSE;
169 }
170 else
171 return TRUE;
172 }
173
174
175
176 /*static*/ void
177 wxMemoryFSHandlerBase::AddFile(const wxString& filename, wxImage& image, long type)
178 {
179 if (!CheckHash(filename)) return;
180
181
182 wxMemoryOutputStream mems;
183 if (image.Ok() && image.SaveFile(mems, (int)type))
184 m_Hash -> Put(filename, new MemFSHashObj(mems));
185 else
186 {
187 wxString s;
188 s.Printf(_("Failed to store image '%s' to memory VFS!"), filename.c_str());
189 wxPrintf(wxT("'%s'\n"), s.c_str());
190 wxLogError(s);
191 }
192 }
193
194
195 /*static*/ void wxMemoryFSHandlerBase::AddFile(const wxString& filename, const wxString& textdata)
196 {
197 AddFile(filename, (const void*) textdata.mb_str(), textdata.Length());
198 }
199
200
201 /*static*/ void wxMemoryFSHandlerBase::AddFile(const wxString& filename, const void *binarydata, size_t size)
202 {
203 if (!CheckHash(filename)) return;
204 m_Hash -> Put(filename, new MemFSHashObj(binarydata, size));
205 }
206
207
208
209 /*static*/ void wxMemoryFSHandlerBase::RemoveFile(const wxString& filename)
210 {
211 if (m_Hash == NULL ||
212 m_Hash -> Get(filename) == NULL)
213 {
214 wxString s;
215 s.Printf(_("Trying to remove file '%s' from memory VFS, but it is not loaded!"), filename.c_str());
216 wxLogError(s);
217 }
218
219 else
220 delete m_Hash -> Delete(filename);
221 }
222
223 #endif // __WXBASE__
224
225 #if wxUSE_GUI
226
227 /*static*/ void wxMemoryFSHandler::AddFile(const wxString& filename, const wxBitmap& bitmap, long type)
228 {
229 wxImage img = bitmap.ConvertToImage();
230 AddFile(filename, img, type);
231 }
232
233 #endif
234
235
236 #endif // wxUSE_FILESYSTEM && wxUSE_FS_ZIP
237