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