]> git.saurik.com Git - wxWidgets.git/blob - contrib/src/xrc/xmlbin.cpp
1. added encodings handling to XRC, so that it is possible
[wxWidgets.git] / contrib / src / xrc / xmlbin.cpp
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
31 bool 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
41 wxString 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
56 void 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
71 static 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
100 bool wxXmlIOHandlerBin::Save(wxOutputStream& stream, const wxXmlDocument& doc)
101 {
102 WriteHeader(stream, "XMLBIN ");
103 wxDataOutputStream ds(stream);
104 ds << doc.GetVersion();
105 #if wxUSE_UNICODE
106 ds << wxString(wxT("UTF-8"));
107 #else
108 ds << doc.GetEncoding();
109 #endif
110 SaveBinNode(ds, doc.GetRoot());
111 return stream.LastError() == wxSTREAM_NOERROR;
112 }
113
114
115
116 static wxXmlProperty *LoadBinProp(wxDataInputStream& ds)
117 {
118 wxUint8 dummy;
119 ds >> dummy;
120 if (dummy == 0) return NULL;
121
122 wxString name, value;
123 ds >> name >> value;
124 return new wxXmlProperty(name, value, LoadBinProp(ds));
125 }
126
127
128
129
130 static wxXmlNode *LoadBinNode(wxDataInputStream& ds, wxXmlNode *parent)
131 {
132 wxUint8 type;
133 wxString name, content;
134 wxUint8 dummy;
135
136 ds >> dummy;
137 if (dummy == 0) return NULL;
138 ds >> type >> name >> content;
139
140 wxXmlProperty *prop = LoadBinProp(ds);
141
142 wxXmlNode *nd = new wxXmlNode(parent, (wxXmlNodeType)type, name, content,
143 prop, LoadBinNode(ds, parent));
144 LoadBinNode(ds, nd);
145 return nd;
146 }
147
148
149
150 bool wxXmlIOHandlerBin::Load(wxInputStream& stream, wxXmlDocument& doc,
151 const wxString& encoding)
152 {
153 ReadHeader(stream);
154 wxDataInputStream ds(stream);
155 wxString tmp;
156
157 ds >> tmp;
158 doc.SetVersion(tmp);
159 ds >> tmp;
160 doc.SetFileEncoding(tmp);
161
162 doc.SetRoot(LoadBinNode(ds, NULL));
163
164 return (doc.GetRoot() != NULL);
165 }
166
167