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