From 4007562c97510e6730ec40d1906aff814749a78e Mon Sep 17 00:00:00 2001
From: Tinny Ng <tng@apache.org>
Date: Fri, 19 Oct 2001 18:27:34 +0000
Subject: [PATCH] These samples use the C++ "endl" method which puts out a
 newline in the local code page, not necessarily the requested output
 encoding. For example, if you request UTF-16 on an ASCII machine, these two
 sample programs put out x'0a' instead of x'000a' in two spots of the output.
 The fix was minor and involved transcoding the newline and then manually
 flushing the output instead of using the endl method to do the same thing.
 The change was only made for the two "endl" calls that generate standard
 output, not calls that output errors messages since they are shown in the
 local code page.  Patches from David McCreedy. And other modification for
 consistent help display and return code across samples.

git-svn-id: https://svn.apache.org/repos/asf/xerces/c/trunk@173135 13f79535-47bb-0310-9956-ffa450edef68
---
 samples/IDOMPrint/IDOMPrint.cpp | 54 +++++++++++++++++++--------------
 1 file changed, 32 insertions(+), 22 deletions(-)

diff --git a/samples/IDOMPrint/IDOMPrint.cpp b/samples/IDOMPrint/IDOMPrint.cpp
index 25d693721..b677672c1 100644
--- a/samples/IDOMPrint/IDOMPrint.cpp
+++ b/samples/IDOMPrint/IDOMPrint.cpp
@@ -267,25 +267,26 @@ ostream& operator<<(ostream& target, IDOM_Node *toWrite);
 // ---------------------------------------------------------------------------
 void usage()
 {
-    cout << "\nUsage: IDOMPrint [options] <XML file>\n\n"
-            "This program invokes the Xerces-C IDOM parser and builds the IDOM\n"
-            "tree. It then traverses the DOM tree and prints the contents\n"
-            "of the tree. Options are NOT case sensitive.\n\n"
+    cout << "\nUsage:\n"
+            "    IDOMPrint [options] <XML file>\n\n"
+            "This program invokes the IDOM parser, and builds the DOM tree.\n"
+            "It then traverses the DOM tree and prints the contents of the\n"
+            "tree for the specified XML file.\n\n"
             "Options:\n"
             "    -e          create entity reference nodes. Default is no expansion.\n"
-            "    -u=xxx      Handle unrepresentable chars [fail | rep | ref*]\n"
-            "    -v=xxx      Validation scheme [always | never | auto*]\n"
+            "    -u=xxx      Handle unrepresentable chars [fail | rep | ref*].\n"
+            "    -v=xxx      Validation scheme [always | never | auto*].\n"
             "    -n          Enable namespace processing. Default is off.\n"
             "    -s          Enable schema processing. Default is off.\n"
             "    -f          Enable full schema constraint checking. Defaults is off.\n"
             "    -x=XXX      Use a particular encoding for output. Default is\n"
             "                the same encoding as the input XML file. UTF-8 if\n"
             "                input XML file has not XML declaration.\n"
-            "    -?          Show this help (must be the only parameter)\n\n"
-            "  * = Default if not provided explicitly\n\n"
+            "    -?          Show this help.\n\n"
+            "  * = Default if not provided explicitly.\n\n"
             "The parser has intrinsic support for the following encodings:\n"
             "    UTF-8, USASCII, ISO8859-1, UTF-16[BL]E, UCS-4[BL]E,\n"
-            "    WINDOWS-1252, IBM1140, IBM037\n"
+            "    WINDOWS-1252, IBM1140, IBM037.\n"
           <<  endl;
 }
 
@@ -322,14 +323,6 @@ int main(int argC, char* argV[])
         return 1;
     }
 
-    // Watch for special case help request
-    if (!strcmp(argV[1], "-?"))
-    {
-        usage();
-        XMLPlatformUtils::Terminate();
-        return 2;
-    }
-
     // See if non validating dom parser configuration is requested.
     int parmInd;
     for (parmInd = 1; parmInd < argC; parmInd++)
@@ -338,8 +331,15 @@ int main(int argC, char* argV[])
         if (argV[parmInd][0] != '-')
             break;
 
-        if (!strncmp(argV[parmInd], "-v=", 3)
-        ||  !strncmp(argV[parmInd], "-V=", 3))
+        // Watch for special case help request
+        if (!strcmp(argV[parmInd], "-?"))
+        {
+            usage();
+            XMLPlatformUtils::Terminate();
+            return 2;
+        }
+         else if (!strncmp(argV[parmInd], "-v=", 3)
+              ||  !strncmp(argV[parmInd], "-V=", 3))
         {
             const char* const parm = &argV[parmInd][3];
 
@@ -352,6 +352,7 @@ int main(int argC, char* argV[])
             else
             {
                 cerr << "Unknown -v= value: " << parm << endl;
+                XMLPlatformUtils::Terminate();
                 return 2;
             }
         }
@@ -396,6 +397,7 @@ int main(int argC, char* argV[])
             else
             {
                 cerr << "Unknown -u= value: " << parm << endl;
+                XMLPlatformUtils::Terminate();
                 return 2;
             }
         }
@@ -503,19 +505,23 @@ int main(int argC, char* argV[])
 
             *gFormatter << gXMLDecl3;
 
-            cout << doc << endl;
+            cout << doc;
+            *gFormatter << chLF; // add linefeed in requested output encoding
+            cout << flush;
         }
         catch (XMLException& e)
         {
             cerr << "An error occurred during creation of output transcoder. Msg is:"
                 << endl
                 << StrX(e.getMessage()) << endl;
-            retval = 3;
+            retval = 4;
         }
 
         delete formatTarget;
         delete gFormatter;
     }
+    else
+        retval = 4;
 
     //
     //  Clean up the error handler. The parser does not adopt handlers
@@ -583,7 +589,11 @@ ostream& operator<<(ostream& target, IDOM_Node *toWrite)
             IDOM_Node *child = toWrite->getFirstChild();
             while( child != 0)
             {
-                target << child << endl;
+                target << child;
+                // add linefeed in requested output encoding
+                *gFormatter << chLF;
+                target << flush;
+
                 child = child->getNextSibling();
             }
             break;
-- 
GitLab