]> git.saurik.com Git - wxWidgets.git/blame - contrib/src/xrc/xmlbin.cpp
fix for stupid segfault
[wxWidgets.git] / contrib / src / xrc / xmlbin.cpp
CommitLineData
78d14f80
VS
1/////////////////////////////////////////////////////////////////////////////
2// Name: xmlbin.cpp
3// Purpose: wxXmlIOHandlerBin
4// Author: Vaclav Slavik
5// Created: 2000/07/24
6// RCS-ID: $Id$
7// Copyright: (c) 2000 Vaclav Slavik
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
11#ifdef __GNUG__
12// nothing, already in xml.cpp
13#endif
14
15// For compilers that support precompilation, includes "wx.h".
16#include "wx/wxprec.h"
17
18#ifdef __BORLANDC__
19 #pragma hdrstop
20#endif
21
22#include "wx/datstrm.h"
23#include "wx/log.h"
24#include "wx/intl.h"
25
26#include "wx/xrc/xmlio.h"
27
28
29
30
31bool wxXmlIOHandlerBin::CanLoad(wxInputStream& stream)
32{
33 bool canread;
34 canread = (ReadHeader(stream) == wxT("XMLBIN "));
35 stream.SeekI(-9, wxFromCurrent);
36 return canread;
37}
38
39
40
41wxString wxXmlIOHandlerBin::ReadHeader(wxInputStream& stream)
42{
43 wxUint8 version;
44 char cheader[8];
45
46 stream.Read(cheader, 8);
47 cheader[7] = 0;
48 stream.Read(&version, 1);
49
50 if (version != 1) return wxEmptyString;
51 else return wxString(cheader);
52}
53
54
55
56void wxXmlIOHandlerBin::WriteHeader(wxOutputStream& stream, const wxString& header)
57{
58 char cheader[8];
59 size_t i;
60 wxUint8 version = 1;
61
62 for (i = 0; i < header.Length(); i++) cheader[i] = header[i];
63 for (; i < 7; i++) cheader[i] = ' ';
64 cheader[7] = 0;
65 stream.Write(cheader, 8);
66 stream.Write(&version, 1);
67}
68
69
70
71static bool SaveBinNode(wxDataOutputStream& ds, wxXmlNode *node)
72{
73 if (node)
74 {
75 ds << (wxUint8)1 <<
76 (wxUint8)node->GetType() <<
77 node->GetName() << node->GetContent();
78
79 wxXmlProperty *prop = node->GetProperties();
80 while (prop)
81 {
82 ds << (wxUint8)1;
83 ds << prop->GetName() << prop->GetValue();
84 prop = prop->GetNext();
85
86 }
87 ds << (wxUint8)0;
88
89 SaveBinNode(ds, node->GetNext());
90 SaveBinNode(ds, node->GetChildren());
91 }
92 else
93 ds << (wxUint8)0;
94
95 return TRUE;
96}
97
98
99
100bool wxXmlIOHandlerBin::Save(wxOutputStream& stream, const wxXmlDocument& doc)
101{
102 WriteHeader(stream, "XMLBIN ");
103 wxDataOutputStream ds(stream);
104 ds << doc.GetVersion() << doc.GetEncoding();
105 SaveBinNode(ds, doc.GetRoot());
106 return stream.LastError() == wxSTREAM_NOERROR;
107}
108
109
110
111static wxXmlProperty *LoadBinProp(wxDataInputStream& ds)
112{
113 wxUint8 dummy;
114 ds >> dummy;
115 if (dummy == 0) return NULL;
116
117 wxString name, value;
118 ds >> name >> value;
119 return new wxXmlProperty(name, value, LoadBinProp(ds));
120}
121
122
123
124
125static wxXmlNode *LoadBinNode(wxDataInputStream& ds, wxXmlNode *parent)
126{
127 wxUint8 type;
128 wxString name, content;
129 wxUint8 dummy;
130
131 ds >> dummy;
132 if (dummy == 0) return NULL;
133 ds >> type >> name >> content;
134
135 wxXmlProperty *prop = LoadBinProp(ds);
136
137 wxXmlNode *nd = new wxXmlNode(parent, (wxXmlNodeType)type, name, content,
138 prop, LoadBinNode(ds, parent));
139 LoadBinNode(ds, nd);
140 return nd;
141}
142
143
144
145bool wxXmlIOHandlerBin::Load(wxInputStream& stream, wxXmlDocument& doc)
146{
147 ReadHeader(stream);
148 wxDataInputStream ds(stream);
149 wxString tmp;
150
151 ds >> tmp;
152 doc.SetVersion(tmp);
153 ds >> tmp;
154 doc.SetEncoding(tmp);
155
156 doc.SetRoot(LoadBinNode(ds, NULL));
157
158 return (doc.GetRoot() != NULL);
159}
160
161