From: Václav Slavík Date: Sun, 27 Jan 2002 23:40:43 +0000 (+0000) Subject: implemented writing in original encoding X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/1419ea47dd1a8e8e11ad254026141a8777038c41 implemented writing in original encoding git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@13865 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/contrib/src/xrc/xmlwrite.cpp b/contrib/src/xrc/xmlwrite.cpp index 3b634c7cc3..3b6e3a1047 100644 --- a/contrib/src/xrc/xmlwrite.cpp +++ b/contrib/src/xrc/xmlwrite.cpp @@ -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 "<", '>' to ">" and '&' to "&" -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("<")); break; - case wxT('>'): OutputString(stream, wxT(">")); break; - case wxT('&'): OutputString(stream, wxT("&")); break; + case wxT('<'): + OutputString(stream, wxT("<"), NULL, NULL); + break; + case wxT('>'): + OutputString(stream, wxT(">"), NULL, NULL); + break; + case wxT('&'): + OutputString(stream, wxT("&"), 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("GetName()); - OutputString(stream, wxT(">")); + OutputString(stream, wxT("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, wxT(""), NULL, NULL); break; default: @@ -141,12 +157,28 @@ bool wxXmlIOHandlerWriter::Save(wxOutputStream& stream, const wxXmlDocument& doc wxString s; - s = wxT("\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 + + s.Printf(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); - OutputNode(stream, doc.GetRoot(), 0); - OutputString(stream, wxT("\n")); + if ( convFile ) + delete convFile; + if ( convMem ) + delete convMem; return TRUE; } diff --git a/src/xrc/xmlwrite.cpp b/src/xrc/xmlwrite.cpp index 3b634c7cc3..3b6e3a1047 100644 --- a/src/xrc/xmlwrite.cpp +++ b/src/xrc/xmlwrite.cpp @@ -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 "<", '>' to ">" and '&' to "&" -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("<")); break; - case wxT('>'): OutputString(stream, wxT(">")); break; - case wxT('&'): OutputString(stream, wxT("&")); break; + case wxT('<'): + OutputString(stream, wxT("<"), NULL, NULL); + break; + case wxT('>'): + OutputString(stream, wxT(">"), NULL, NULL); + break; + case wxT('&'): + OutputString(stream, wxT("&"), 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("GetName()); - OutputString(stream, wxT(">")); + OutputString(stream, wxT("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, wxT(""), NULL, NULL); break; default: @@ -141,12 +157,28 @@ bool wxXmlIOHandlerWriter::Save(wxOutputStream& stream, const wxXmlDocument& doc wxString s; - s = wxT("\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 + + s.Printf(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); - OutputNode(stream, doc.GetRoot(), 0); - OutputString(stream, wxT("\n")); + if ( convFile ) + delete convFile; + if ( convMem ) + delete convMem; return TRUE; }