]> git.saurik.com Git - wxWidgets.git/blob - src/common/objstrm.cpp
* Ooops, I didn't copy the files in the right directory of my repository.
[wxWidgets.git] / src / common / objstrm.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: objstrm.cpp
3 // Purpose: wxObjectStream classes
4 // Author: Guilhem Lavaux
5 // Modified by:
6 // Created: 16/07/98
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Guilhem Lavaux
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11 #ifdef __GNUG__
12 #pragma implementation "objstrm.h"
13 #endif
14
15 #include "wx/object.h"
16 #include "wx/objstrm.h"
17 #include "wx/datstrm.h"
18
19 #define WXOBJ_BEGIN "OBEGIN"
20 #define WXOBJ_BEG_LEN 6
21
22 // ----------------------------------------------------------------------------
23 // wxObjectOutputStream
24 // ----------------------------------------------------------------------------
25
26 wxObjectOutputStream::wxObjectOutputStream(wxOutputStream& s)
27 : wxFilterOutputStream(s)
28 {
29 m_saving = FALSE;
30 }
31
32 wxString wxObjectOutputStream::GetObjectName(wxObject *obj)
33 {
34 wxString name;
35
36 name.Printf("%x", (unsigned long)obj);
37 return name;
38 }
39
40 void wxObjectOutputStream::WriteObjectDef(wxObjectStreamInfo& info)
41 {
42 wxDataOutputStream data_s(*this);
43
44 Write(WXOBJ_BEGIN, WXOBJ_BEG_LEN);
45 data_s.WriteString(info.object->GetClassInfo()->GetClassName());
46 data_s.WriteString(GetObjectName(info.object));
47 // I assume an object will not have millions of children
48 data_s.Write8(info.children.Number());
49 }
50
51 void wxObjectOutputStream::AddChildren(wxObject *obj)
52 {
53 wxObjectStreamInfo *info;
54
55 if (!FirstStage())
56 return;
57
58 info = new wxObjectStreamInfo;
59 info->n_children = 0;
60 info->object = obj;
61 info->parent = m_current_info->object; // Not useful here.
62 m_current_info->n_children++;
63 m_current_info->children.Append(info);
64 }
65
66 void wxObjectOutputStream::ProcessObjectDef(wxObjectStreamInfo *info)
67 {
68 wxNode *node;
69
70 m_current_info = info;
71 // First stage: get children of obj
72 info->object->StoreObject(*this);
73
74 // Prepare and write the sub-entry about the child obj.
75 WriteObjectDef(*info);
76
77 node = info->children.First();
78
79 while (node) {
80 ProcessObjectDef((wxObjectStreamInfo *)node->Data());
81 node = node->Next();
82 }
83 }
84
85 void wxObjectOutputStream::ProcessObjectData(wxObjectStreamInfo *info)
86 {
87 wxNode *node = info->children.First();
88
89 m_current_info = info;
90
91 info->object->StoreObject(*this);
92
93 while (node) {
94 ProcessObjectData((wxObjectStreamInfo *)node->Data());
95 node = node->Next();
96 }
97 }
98
99 bool wxObjectOutputStream::SaveObject(wxObject& obj)
100 {
101 wxObjectStreamInfo info;
102
103 if (m_saving)
104 return FALSE;
105
106 m_saving = TRUE;
107
108 // First stage
109 m_stage = 0;
110 info.object = &obj;
111 info.n_children = 0;
112 ProcessObjectDef(&info);
113
114 m_stage = 1;
115 ProcessObjectData(&info);
116
117 info.children.Clear();
118
119 m_saving = FALSE;
120
121 return TRUE;
122 }
123
124 // ----------------------------------------------------------------------------
125 // wxObjectInputStream
126 // ----------------------------------------------------------------------------
127
128 wxObjectInputStream::wxObjectInputStream(wxInputStream& s)
129 : wxFilterInputStream(s)
130 {
131 }
132
133 wxObject *wxObjectInputStream::SolveName(const wxString& name) const
134 {
135 wxNode *node = m_solver.First();
136 wxObjectStreamInfo *info;
137
138 while (node) {
139 info = (wxObjectStreamInfo *)node->Data();
140 if (info->object_name == name)
141 return info->object;
142
143 node = node->Next();
144 }
145 return NULL;
146 }
147
148 wxObject *wxObjectInputStream::GetChild(int no) const
149 {
150 return m_current_info->children.Nth(no);
151 }
152
153 bool wxObjectInputStream::ReadObjectDef(wxObjectStreamInfo *info)
154 {
155 wxDataInputStream data_s(*this);
156 char sig[WXOBJ_BEG_LEN+1];
157
158 Read(sig, WXOBJ_BEG_LEN);
159 sig[WXOBJ_BEG_LEN] = 0;
160 if (wxString(sig) != WXOBJ_BEGIN)
161 return FALSE;
162 info->object = wxCreateDynamicObject((char *)data_s.ReadString());
163 info->object_name = data_s.ReadString();
164 info->n_children = data_s.Read8();
165 info->children = wxList();
166
167 return TRUE;
168 }
169
170 wxObjectStreamInfo *wxObjectInputStream::ProcessObjectDef(wxObjectStreamInfo *parent)
171 {
172 wxObjectStreamInfo *info, *c_info;
173 int c;
174
175 info = new wxObjectStreamInfo;
176 info->parent = parent;
177 info->children.DeleteContents(TRUE);
178
179 m_solver.Append(info);
180
181 if (!ReadObjectDef(info))
182 return NULL;
183
184 for (c=0;c<info->n_children;c++) {
185 c_info = ProcessObjectDef(info);
186 if (!c_info)
187 return NULL;
188 info->children.Append(c_info);
189 }
190
191 return info;
192 }
193
194 void wxObjectInputStream::ProcessObjectData(wxObjectStreamInfo *info)
195 {
196 wxNode *node = info->children.First();
197 wxObjectStreamInfo *c_info;
198
199 m_current_info = info;
200
201 info->object->LoadObject(*this);
202 while (node) {
203 c_info = (wxObjectStreamInfo *)node->Data();
204 c_info->object->LoadObject(*this);
205 node = node->Next();
206 }
207 }
208
209 wxObject *wxObjectInputStream::LoadObject()
210 {
211 wxObjectStreamInfo *info;
212 wxObject *object;
213
214 info = ProcessObjectDef(NULL);
215 if (!info)
216 return NULL;
217 ProcessObjectData(info);
218
219 object = info->object;
220
221 delete info; // It's magic ! The whole tree is destroyed.
222
223 return object;
224 }