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