implemented writing in original encoding

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@13865 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Václav Slavík 2002-01-27 23:40:43 +00:00
parent a679e1298b
commit 1419ea47dd
2 changed files with 124 additions and 60 deletions

View File

@ -27,20 +27,28 @@
#include "wx/xrc/xmlio.h"
// write string to output:
inline static void OutputString(wxOutputStream& stream, const wxString& str)
inline static void OutputString(wxOutputStream& stream, const wxString& str,
wxMBConv *convMem, wxMBConv *convFile)
{
if (str.IsEmpty()) return;
#if wxUSE_UNICODE
const char *buf = str.mb_str(wxConvUTF8);
stream.Write(buf, strlen(buf));
const wxW2MBbuf *buf = str.mb_str(convFile ? *convFile : wxConvUTF8);
stream.Write((const char*)buf, strlen((const char*)buf));
#else
stream.Write(str.mb_str(), str.Len());
if ( convFile == NULL )
stream.Write(str.mb_str(), str.Len());
else
{
wxString str2(str.wc_str(*convMem), *convFile);
stream.Write(str2.mb_str(), str2.Len());
}
#endif
}
// Same as above, but create entities first.
// Translates '<' to "&lt;", '>' to "&gt;" and '&' to "&amp;"
static void OutputStringEnt(wxOutputStream& stream, const wxString& str)
static void OutputStringEnt(wxOutputStream& stream, const wxString& str,
wxMBConv *convMem, wxMBConv *convFile)
{
wxString buf;
size_t i, last, len;
@ -54,18 +62,24 @@ static void OutputStringEnt(wxOutputStream& stream, const wxString& str)
if (c == wxT('<') || c == wxT('>') ||
(c == wxT('&') && str.Mid(i+1, 4) != wxT("amp;")))
{
OutputString(stream, str.Mid(last, i - last));
OutputString(stream, str.Mid(last, i - last), convMem, convFile);
switch (c)
{
case wxT('<'): OutputString(stream, wxT("&lt;")); break;
case wxT('>'): OutputString(stream, wxT("&gt;")); break;
case wxT('&'): OutputString(stream, wxT("&amp;")); break;
case wxT('<'):
OutputString(stream, wxT("&lt;"), NULL, NULL);
break;
case wxT('>'):
OutputString(stream, wxT("&gt;"), NULL, NULL);
break;
case wxT('&'):
OutputString(stream, wxT("&amp;"), NULL, NULL);
break;
default: break;
}
last = i + 1;
}
}
OutputString(stream, str.Mid(last, i - last));
OutputString(stream, str.Mid(last, i - last), convMem, convFile);
}
inline static void OutputIndentation(wxOutputStream& stream, int indent)
@ -73,10 +87,11 @@ inline static void OutputIndentation(wxOutputStream& stream, int indent)
wxString str = wxT("\n");
for (int i = 0; i < indent; i++)
str << wxT(' ') << wxT(' ');
OutputString(stream, str);
OutputString(stream, str, NULL, NULL);
}
static void OutputNode(wxOutputStream& stream, wxXmlNode *node, int indent)
static void OutputNode(wxOutputStream& stream, wxXmlNode *node, int indent,
wxMBConv *convMem, wxMBConv *convFile)
{
wxXmlNode *n, *prev;
wxXmlProperty *prop;
@ -84,49 +99,50 @@ static void OutputNode(wxOutputStream& stream, wxXmlNode *node, int indent)
switch (node->GetType())
{
case wxXML_TEXT_NODE:
OutputStringEnt(stream, node->GetContent());
OutputStringEnt(stream, node->GetContent(), convMem, convFile);
break;
case wxXML_ELEMENT_NODE:
OutputString(stream, wxT("<"));
OutputString(stream, node->GetName());
OutputString(stream, wxT("<"), NULL, NULL);
OutputString(stream, node->GetName(), NULL, NULL);
prop = node->GetProperties();
while (prop)
{
OutputString(stream, wxT(" ") + prop->GetName() +
wxT("=\"") + prop->GetValue() + wxT("\""));
wxT("=\"") + prop->GetValue() + wxT("\""),
NULL, NULL);
// FIXME - what if prop contains '"'?
prop = prop->GetNext();
}
if (node->GetChildren())
{
OutputString(stream, wxT(">"));
OutputString(stream, wxT(">"), NULL, NULL);
prev = NULL;
n = node->GetChildren();
while (n)
{
if (n && n->GetType() != wxXML_TEXT_NODE)
OutputIndentation(stream, indent + 1);
OutputNode(stream, n, indent + 1);
OutputNode(stream, n, indent + 1, convMem, convFile);
prev = n;
n = n->GetNext();
}
if (prev && prev->GetType() != wxXML_TEXT_NODE)
OutputIndentation(stream, indent);
OutputString(stream, wxT("</"));
OutputString(stream, node->GetName());
OutputString(stream, wxT(">"));
OutputString(stream, wxT("</"), NULL, NULL);
OutputString(stream, node->GetName(), NULL, NULL);
OutputString(stream, wxT(">"), NULL, NULL);
}
else
OutputString(stream, wxT("/>"));
OutputString(stream, wxT("/>"), NULL, NULL);
break;
case wxXML_COMMENT_NODE:
OutputString(stream, wxT("<!--"));
OutputString(stream, node->GetContent());
OutputString(stream, wxT("-->"));
OutputString(stream, wxT("<!--"), NULL, NULL);
OutputString(stream, node->GetContent(), convMem, convFile);
OutputString(stream, wxT("-->"), NULL, NULL);
break;
default:
@ -141,12 +157,28 @@ bool wxXmlIOHandlerWriter::Save(wxOutputStream& stream, const wxXmlDocument& doc
wxString s;
s = wxT("<?xml version=\"") + doc.GetVersion() +
wxT("\" encoding=\"utf-8\"?>\n");
OutputString(stream, s);
wxMBConv *convMem = NULL, *convFile = NULL;
#if wxUSE_UNICODE
convFile = new wxCSConv(doc.GetFileEncoding());
#else
if ( doc.GetFileEncoding() != doc.GetEncoding() )
{
convFile = new wxCSConv(doc.GetFileEncoding());
convMem = new wxCSConv(doc.GetEncoding());
}
#endif
OutputNode(stream, doc.GetRoot(), 0);
OutputString(stream, wxT("\n"));
s.Printf(wxT("<?xml version=\"%s\" encoding=\"%s\"?>\n"),
doc.GetVersion().c_str(), doc.GetFileEncoding().c_str());
OutputString(stream, s, NULL, NULL);
OutputNode(stream, doc.GetRoot(), 0, convMem, convFile);
OutputString(stream, wxT("\n"), NULL, NULL);
if ( convFile )
delete convFile;
if ( convMem )
delete convMem;
return TRUE;
}

View File

@ -27,20 +27,28 @@
#include "wx/xrc/xmlio.h"
// write string to output:
inline static void OutputString(wxOutputStream& stream, const wxString& str)
inline static void OutputString(wxOutputStream& stream, const wxString& str,
wxMBConv *convMem, wxMBConv *convFile)
{
if (str.IsEmpty()) return;
#if wxUSE_UNICODE
const char *buf = str.mb_str(wxConvUTF8);
stream.Write(buf, strlen(buf));
const wxW2MBbuf *buf = str.mb_str(convFile ? *convFile : wxConvUTF8);
stream.Write((const char*)buf, strlen((const char*)buf));
#else
stream.Write(str.mb_str(), str.Len());
if ( convFile == NULL )
stream.Write(str.mb_str(), str.Len());
else
{
wxString str2(str.wc_str(*convMem), *convFile);
stream.Write(str2.mb_str(), str2.Len());
}
#endif
}
// Same as above, but create entities first.
// Translates '<' to "&lt;", '>' to "&gt;" and '&' to "&amp;"
static void OutputStringEnt(wxOutputStream& stream, const wxString& str)
static void OutputStringEnt(wxOutputStream& stream, const wxString& str,
wxMBConv *convMem, wxMBConv *convFile)
{
wxString buf;
size_t i, last, len;
@ -54,18 +62,24 @@ static void OutputStringEnt(wxOutputStream& stream, const wxString& str)
if (c == wxT('<') || c == wxT('>') ||
(c == wxT('&') && str.Mid(i+1, 4) != wxT("amp;")))
{
OutputString(stream, str.Mid(last, i - last));
OutputString(stream, str.Mid(last, i - last), convMem, convFile);
switch (c)
{
case wxT('<'): OutputString(stream, wxT("&lt;")); break;
case wxT('>'): OutputString(stream, wxT("&gt;")); break;
case wxT('&'): OutputString(stream, wxT("&amp;")); break;
case wxT('<'):
OutputString(stream, wxT("&lt;"), NULL, NULL);
break;
case wxT('>'):
OutputString(stream, wxT("&gt;"), NULL, NULL);
break;
case wxT('&'):
OutputString(stream, wxT("&amp;"), NULL, NULL);
break;
default: break;
}
last = i + 1;
}
}
OutputString(stream, str.Mid(last, i - last));
OutputString(stream, str.Mid(last, i - last), convMem, convFile);
}
inline static void OutputIndentation(wxOutputStream& stream, int indent)
@ -73,10 +87,11 @@ inline static void OutputIndentation(wxOutputStream& stream, int indent)
wxString str = wxT("\n");
for (int i = 0; i < indent; i++)
str << wxT(' ') << wxT(' ');
OutputString(stream, str);
OutputString(stream, str, NULL, NULL);
}
static void OutputNode(wxOutputStream& stream, wxXmlNode *node, int indent)
static void OutputNode(wxOutputStream& stream, wxXmlNode *node, int indent,
wxMBConv *convMem, wxMBConv *convFile)
{
wxXmlNode *n, *prev;
wxXmlProperty *prop;
@ -84,49 +99,50 @@ static void OutputNode(wxOutputStream& stream, wxXmlNode *node, int indent)
switch (node->GetType())
{
case wxXML_TEXT_NODE:
OutputStringEnt(stream, node->GetContent());
OutputStringEnt(stream, node->GetContent(), convMem, convFile);
break;
case wxXML_ELEMENT_NODE:
OutputString(stream, wxT("<"));
OutputString(stream, node->GetName());
OutputString(stream, wxT("<"), NULL, NULL);
OutputString(stream, node->GetName(), NULL, NULL);
prop = node->GetProperties();
while (prop)
{
OutputString(stream, wxT(" ") + prop->GetName() +
wxT("=\"") + prop->GetValue() + wxT("\""));
wxT("=\"") + prop->GetValue() + wxT("\""),
NULL, NULL);
// FIXME - what if prop contains '"'?
prop = prop->GetNext();
}
if (node->GetChildren())
{
OutputString(stream, wxT(">"));
OutputString(stream, wxT(">"), NULL, NULL);
prev = NULL;
n = node->GetChildren();
while (n)
{
if (n && n->GetType() != wxXML_TEXT_NODE)
OutputIndentation(stream, indent + 1);
OutputNode(stream, n, indent + 1);
OutputNode(stream, n, indent + 1, convMem, convFile);
prev = n;
n = n->GetNext();
}
if (prev && prev->GetType() != wxXML_TEXT_NODE)
OutputIndentation(stream, indent);
OutputString(stream, wxT("</"));
OutputString(stream, node->GetName());
OutputString(stream, wxT(">"));
OutputString(stream, wxT("</"), NULL, NULL);
OutputString(stream, node->GetName(), NULL, NULL);
OutputString(stream, wxT(">"), NULL, NULL);
}
else
OutputString(stream, wxT("/>"));
OutputString(stream, wxT("/>"), NULL, NULL);
break;
case wxXML_COMMENT_NODE:
OutputString(stream, wxT("<!--"));
OutputString(stream, node->GetContent());
OutputString(stream, wxT("-->"));
OutputString(stream, wxT("<!--"), NULL, NULL);
OutputString(stream, node->GetContent(), convMem, convFile);
OutputString(stream, wxT("-->"), NULL, NULL);
break;
default:
@ -141,12 +157,28 @@ bool wxXmlIOHandlerWriter::Save(wxOutputStream& stream, const wxXmlDocument& doc
wxString s;
s = wxT("<?xml version=\"") + doc.GetVersion() +
wxT("\" encoding=\"utf-8\"?>\n");
OutputString(stream, s);
wxMBConv *convMem = NULL, *convFile = NULL;
#if wxUSE_UNICODE
convFile = new wxCSConv(doc.GetFileEncoding());
#else
if ( doc.GetFileEncoding() != doc.GetEncoding() )
{
convFile = new wxCSConv(doc.GetFileEncoding());
convMem = new wxCSConv(doc.GetEncoding());
}
#endif
OutputNode(stream, doc.GetRoot(), 0);
OutputString(stream, wxT("\n"));
s.Printf(wxT("<?xml version=\"%s\" encoding=\"%s\"?>\n"),
doc.GetVersion().c_str(), doc.GetFileEncoding().c_str());
OutputString(stream, s, NULL, NULL);
OutputNode(stream, doc.GetRoot(), 0, convMem, convFile);
OutputString(stream, wxT("\n"), NULL, NULL);
if ( convFile )
delete convFile;
if ( convMem )
delete convMem;
return TRUE;
}