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