]> git.saurik.com Git - wxWidgets.git/blobdiff - contrib/src/xrc/xmlwrite.cpp
wxDC::SetClippingRegion() in wxMSW works like in wxGTK, i.e. combines the given regio...
[wxWidgets.git] / contrib / src / xrc / xmlwrite.cpp
index 482f976cb45c05267c3a410827fbffb6c7b3603a..3b6e3a1047fb3edd22c0d7b8ae970bfcf61e81e3 100644 (file)
 #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
-    char *buf = str.mb_str(wxMBConvUTF8);
-    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;
-    char c;
+    wxChar c;
     
     len = str.Len();
     last = 0;
     for (i = 0; i < len; i++)
     {
         c = str.GetChar(i);
-        if (c == '<' || c == '>' || 
-            (c == '&' && str.Mid(i+1, 4) != wxT("amp;")))
+        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 '<': OutputString(stream, wxT("&lt;")); break;
-                case '>': OutputString(stream, wxT("&gt;")); break;
-                case '&': 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
+    
+    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);
     
-    OutputNode(stream, doc.GetRoot(), 0);
-    OutputString(stream, wxT("\n"));
+    if ( convFile )
+        delete convFile;
+    if ( convMem )
+        delete convMem;
         
     return TRUE;
 }