From 59049d64dbf2a052a6fa58b3340386846d85b9d9 Mon Sep 17 00:00:00 2001
From: Julien Michel <julien.michel@orfeo-toolbox.org>
Date: Wed, 20 Jan 2010 18:00:44 +0100
Subject: [PATCH] BUG: Fixing bug in the readTextContent. Call to putback was
 invalidating the input stream

---
 .../otbossim/src/ossim/base/ossimXmlNode.cpp  | 113 +++++++-----------
 1 file changed, 42 insertions(+), 71 deletions(-)

diff --git a/Utilities/otbossim/src/ossim/base/ossimXmlNode.cpp b/Utilities/otbossim/src/ossim/base/ossimXmlNode.cpp
index 65d1a30cfb..0dd758c546 100755
--- a/Utilities/otbossim/src/ossim/base/ossimXmlNode.cpp
+++ b/Utilities/otbossim/src/ossim/base/ossimXmlNode.cpp
@@ -1083,80 +1083,51 @@ bool ossimXmlNode::readTextContent(std::istream& in)
 
    theText = "";
    theCDataFlag = false;
-   char c = in.peek();
 
-   do
-   {
-      if(c == '<')
-      {
-         in.ignore();
+   char buf[9];
+   buf[8]='\0';
 
-         // we will check for comments or CDATA
-         if(in.peek()=='!')
-         {
-            char buf1[4];
-            buf1[3] = '\0';
-            in.read(buf1, 3);
-            if(ossimString(buf1) == "!--")
-            {
-               // special text read
-               theText += buf1;
-               bool done = false;
-               do
-               {
-                  if(in.peek() != '-')
-                  {
-                     in.ignore();
-                  }
-                  else
-                  {
-                     in.ignore();
-                     if(in.peek() == '-')
-                     {
-                        in.ignore();
-                        if(in.peek() == '>')
-                        {
-                           in.ignore();
-                           done = true;
-                           c = in.peek();
-                        }
-                     }
-                  }
-               }while(!done&&!in.fail());
-            }
-            else
-            {
-               
-               char buf2[6];
-               buf2[5] = '\0';
-               in.read(buf2, 5);
-               if(in.fail())
-               {
-                  return false;
-               }
-               if(ossimString(buf1)+ossimString(buf2) == "![CDATA[")
-               {
-                  if(readCDataContent(in))
-                  {
-                     theCDataFlag = true;
-                     return true;
-                  }
-               }
-            }
-         }
-         else
-         {
-            in.putback(c);
-            return true;
-         }
-      }
-      else
-      {
-         theText += (char)in.get();
-         c = in.peek();
-      }
-   }while(!in.fail());
+   std::streampos initialPos = in.tellg();
+
+   in.read(buf,9);
+   ossimString ostrBuf(buf);
 
+   if(ostrBuf == "<![CDATA[")
+   {
+	   if(readCDataContent(in))
+		 {
+           theCDataFlag = true;
+           return true;
+	     }
+	   else
+	   {
+		 return false;
+	   }
+   }
+   else if(ostrBuf.substr(0,4) == "<!--")
+   {
+	   in.seekg(initialPos);
+	   char c = in.get();
+	   // Strip comment
+	   while(!in.fail() && c!='>')
+	   {
+		c = in.get();
+	   }
+   }
+   else if(ostrBuf.substr(0,1) ==  "<")
+   {
+	   in.seekg(initialPos);
+   }
+   else
+   {
+       in.seekg(initialPos);
+	   char c = in.peek();
+	   while(!in.fail() && c != '<')
+	   {
+		theText += (char)in.get();
+	    c = in.peek();
+	   }
+   }
    return !in.fail();
 }
 
-- 
GitLab