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

View File

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