]> git.saurik.com Git - wxWidgets.git/blame - src/common/fs_mem.cpp
Committing in .
[wxWidgets.git] / src / common / fs_mem.cpp
CommitLineData
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__
c3f4609e 11#pragma implementation "fs_mem.h"
dcb86da0
VS
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
dcb86da0
VS
30class 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;
5f8525bf 39 m_Time = wxDateTime::Now();
dcb86da0
VS
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);
5f8525bf 47 m_Time = wxDateTime::Now();
dcb86da0
VS
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
66wxHashTable *wxMemoryFSHandler::m_Hash = NULL;
67
68
69wxMemoryFSHandler::wxMemoryFSHandler() : wxFileSystemHandler()
70{
71 m_Hash = NULL;
72}
73
74
75
76wxMemoryFSHandler::~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
88bool wxMemoryFSHandler::CanOpen(const wxString& location)
89{
90 wxString p = GetProtocol(location);
91 return (p == wxT("memory"));
92}
93
94
95
96
97wxFSFile* 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
bc1dcfc1
VZ
114wxString wxMemoryFSHandler::FindFirst(const wxString& WXUNUSED(spec),
115 int WXUNUSED(flags))
dcb86da0 116{
bc1dcfc1
VZ
117 wxFAIL_MSG(wxT("wxMemoryFSHandler::FindFirst not implemented"));
118
dcb86da0
VS
119 return wxEmptyString;
120}
121
122
123
124wxString wxMemoryFSHandler::FindNext()
125{
bc1dcfc1
VZ
126 wxFAIL_MSG(wxT("wxMemoryFSHandler::FindNext not implemented"));
127
dcb86da0
VS
128 return wxEmptyString;
129}
130
131
dcb86da0
VS
132bool 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
11eaa981
VS
153#if wxUSE_GUI
154
dcb86da0
VS
155/*static*/ void wxMemoryFSHandler::AddFile(const wxString& filename, wxImage& image, long type)
156{
157 if (!CheckHash(filename)) return;
158
159
160 wxMemoryOutputStream mems;
f6bcfd97 161 if (image.Ok() && image.SaveFile(mems, (int)type))
dcb86da0
VS
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());
f6bcfd97 167 wxPrintf(wxT("'%s'\n"), s.c_str());
dcb86da0
VS
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
11eaa981 179#endif
dcb86da0
VS
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
24528b0c 211#endif // wxUSE_FILESYSTEM && wxUSE_FS_ZIP