[Cmake-commits] [cmake-commits] king committed CMakeLists.txt 1.405 1.406 cmCTest.cxx 1.352 1.353 cmCTest.h 1.112 1.113 cmSystemTools.cxx 1.388 1.389 cmSystemTools.h 1.156 1.157 cmXMLSafe.cxx NONE 1.1 cmXMLSafe.h NONE 1.1

cmake-commits at cmake.org cmake-commits at cmake.org
Thu Feb 5 16:31:39 EST 2009


Update of /cvsroot/CMake/CMake/Source
In directory public:/mounts/ram/cvs-serv3208/Source

Modified Files:
	CMakeLists.txt cmCTest.cxx cmCTest.h cmSystemTools.cxx 
	cmSystemTools.h 
Added Files:
	cmXMLSafe.cxx cmXMLSafe.h 
Log Message:
ENH: Create cmXMLSafe to help escapes in XML

This class provides easy syntax to efficiently insert blocks of data
into XML documents with proper escapes.  It replaces the old
cmCTest::MakeXMLSafe and cmSystemTools::MakeXMLSafe methods which
allocated extra memory instead of directly streaming the data.


Index: cmSystemTools.h
===================================================================
RCS file: /cvsroot/CMake/CMake/Source/cmSystemTools.h,v
retrieving revision 1.156
retrieving revision 1.157
diff -C 2 -d -r1.156 -r1.157
*** cmSystemTools.h	26 Nov 2008 19:38:43 -0000	1.156
--- cmSystemTools.h	5 Feb 2009 21:31:36 -0000	1.157
***************
*** 352,358 ****
    static void EnableVSConsoleOutput();
  
-   /** Make string XML safe */
-   static std::string MakeXMLSafe(const char* str);
- 
    /** Create tar */
    static bool ListTar(const char* outFileName,
--- 352,355 ----

--- NEW FILE: cmXMLSafe.cxx ---
/*=========================================================================

  Program:   CMake - Cross-Platform Makefile Generator
  Module:    $RCSfile: cmXMLSafe.cxx,v $
  Language:  C++
  Date:      $Date: 2009-02-05 21:31:37 $
  Version:   $Revision: 1.1 $

  Copyright (c) 2002 Kitware, Inc., Insight Consortium.  All rights reserved.
  See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/
#include "cmXMLSafe.h"

#include <cmsys/ios/iostream>
#include <cmsys/ios/sstream>

#include <string.h>
#include <stdio.h>

//----------------------------------------------------------------------------
cmXMLSafe::cmXMLSafe(const char* s):
  Data(s),
  Size(static_cast<unsigned long>(strlen(s))),
  DoQuotes(true)
{
}

//----------------------------------------------------------------------------
cmXMLSafe::cmXMLSafe(cmsys_stl::string const& str):
    Data(str.c_str()),
    Size(static_cast<unsigned long>(str.length())),
    DoQuotes(true)
{
}

//----------------------------------------------------------------------------
cmXMLSafe& cmXMLSafe::Quotes(bool b)
{
  this->DoQuotes = b;
  return *this;
}

//----------------------------------------------------------------------------
cmsys_stl::string cmXMLSafe::str()
{
  cmsys_ios::ostringstream ss;
  ss << *this;
  return ss.str();
}

//----------------------------------------------------------------------------
cmsys_ios::ostream& operator<<(cmsys_ios::ostream& os, cmXMLSafe const& self)
{
  char const* first = self.Data;
  char const* last = self.Data + self.Size;
  for(char const* ci = first; ci != last; ++ci)
    {
    char c = *ci;
    switch(c)
      {
      case '&': os << "&amp;"; break;
      case '<': os << "&lt;"; break;
      case '>': os << "&gt;"; break;
      case '"': os << (self.DoQuotes? "&quot;" : "\""); break;
      case '\'': os << (self.DoQuotes? "&apos;" : "'"); break;
      case '\t': os << "\t"; break;
      case '\n': os << "\n"; break;
      case '\r': break; // Ignore CR
      default:
        if(c >= 0x20 && c <= 0x7f)
          {
          os.put(c);
          }
        else
          {
          // TODO: More complete treatment of program output character
          // encoding.  Instead of escaping these bytes, we should
          // handle the current locale and its encoding.
          unsigned char uc = static_cast<unsigned char>(c);
          char buf[16];
          sprintf(buf, "&#x%hx;", static_cast<unsigned short>(uc));
          os << buf;
          }
        break;
      }
    }
  return os;
}

Index: cmSystemTools.cxx
===================================================================
RCS file: /cvsroot/CMake/CMake/Source/cmSystemTools.cxx,v
retrieving revision 1.388
retrieving revision 1.389
diff -C 2 -d -r1.388 -r1.389
*** cmSystemTools.cxx	20 Jan 2009 15:06:39 -0000	1.388
--- cmSystemTools.cxx	5 Feb 2009 21:31:34 -0000	1.389
***************
*** 1645,1698 ****
  }
  
- std::string cmSystemTools::MakeXMLSafe(const char* str)
- {
-   std::vector<char> result;
-   result.reserve(500);
-   const char* pos = str;
-   for ( ;*pos; ++pos)
-     {
-     char ch = *pos;
-     if ( (ch > 126 || ch < 32) && ch != 9  && ch != 10 && ch != 13 
-          && ch != '\r' )
-       {
-       char buffer[33];
-       sprintf(buffer, "&lt;%d&gt;", static_cast<int>(ch));
-       //sprintf(buffer, "&#x%0x;", (unsigned int)ch);
-       result.insert(result.end(), buffer, buffer+strlen(buffer));
-       }
-     else
-       {
-       const char* const encodedChars[] = {
-         "&amp;",
-         "&lt;",
-         "&gt;"
-       };
-       switch ( ch )
-         {
-         case '&':
-           result.insert(result.end(), encodedChars[0], encodedChars[0]+5);
-           break;
-         case '<':
-           result.insert(result.end(), encodedChars[1], encodedChars[1]+4);
-           break;
-         case '>':
-           result.insert(result.end(), encodedChars[2], encodedChars[2]+4);
-           break;
-         case '\n':
-           result.push_back('\n');
-           break;
-         case '\r': break; // Ignore \r
-         default:
-           result.push_back(ch);
-         }
-       }
-     }
-   if ( result.size() == 0 )
-     {
-     return "";
-     }
-   return std::string(&*result.begin(), result.size());
- }
- 
  bool cmSystemTools::IsPathToFramework(const char* path)
  {
--- 1645,1648 ----

Index: cmCTest.cxx
===================================================================
RCS file: /cvsroot/CMake/CMake/Source/cmCTest.cxx,v
retrieving revision 1.352
retrieving revision 1.353
diff -C 2 -d -r1.352 -r1.353
*** cmCTest.cxx	27 Jan 2009 15:58:33 -0000	1.352
--- cmCTest.cxx	5 Feb 2009 21:31:33 -0000	1.353
***************
*** 26,29 ****
--- 26,30 ----
  #include "cmDynamicLoader.h"
  #include "cmGeneratedFileStream.h"
+ #include "cmXMLSafe.h"
  #include "cmCTestCommand.h"
  
***************
*** 150,206 ****
      }
    cmCTestLog(this, DEBUG, "   Current_Time: " << current_time << std::endl);
!   return cmCTest::MakeXMLSafe(cmCTest::CleanString(current_time));
! }
! 
! 
! //----------------------------------------------------------------------
! std::string cmCTest::MakeXMLSafe(const std::string& str)
! {
!   std::vector<char> result;
!   result.reserve(500);
!   const char* pos = str.c_str();
!   for ( ;*pos; ++pos)
!     {
!     char ch = *pos;
!     if ( (ch > 126 || ch < 32) && ch != 9  &&
!       ch != 10 && ch != 13 && ch != '\r' )
!       {
!       char buffer[33];
!       sprintf(buffer, "&lt;%d&gt;", (int)ch);
!       //sprintf(buffer, "&#x%0x;", (unsigned int)ch);
!       result.insert(result.end(), buffer, buffer+strlen(buffer));
!       }
!     else
!       {
!       const char* const encodedChars[] = {
!         "&amp;",
!         "&lt;",
!         "&gt;"
!       };
!       switch ( ch )
!         {
!         case '&':
!           result.insert(result.end(), encodedChars[0], encodedChars[0]+5);
!           break;
!         case '<':
!           result.insert(result.end(), encodedChars[1], encodedChars[1]+4);
!           break;
!         case '>':
!           result.insert(result.end(), encodedChars[2], encodedChars[2]+4);
!           break;
!         case '\n':
!           result.push_back('\n');
!           break;
!         case '\r': break; // Ignore \r
!         default:
!           result.push_back(ch);
!         }
!       }
!     }
!   if ( result.size() == 0 )
!     {
!     return "";
!     }
!   return std::string(&*result.begin(), result.size());
  }
  
--- 151,155 ----
      }
    cmCTestLog(this, DEBUG, "   Current_Time: " << current_time << std::endl);
!   return cmXMLSafe(cmCTest::CleanString(current_time)).str();
  }
  
***************
*** 1387,1391 ****
      cmCTestLog(this, OUTPUT, "\tAdd file: " << it->c_str() << std::endl);
      std::string note_time = this->CurrentTime();
!     os << "<Note Name=\"" << this->MakeXMLSafe(it->c_str()) << "\">\n"
        << "<Time>" << cmSystemTools::GetTime() << "</Time>\n"
        << "<DateTime>" << note_time << "</DateTime>\n"
--- 1336,1340 ----
      cmCTestLog(this, OUTPUT, "\tAdd file: " << it->c_str() << std::endl);
      std::string note_time = this->CurrentTime();
!     os << "<Note Name=\"" << cmXMLSafe(*it) << "\">\n"
        << "<Time>" << cmSystemTools::GetTime() << "</Time>\n"
        << "<DateTime>" << note_time << "</DateTime>\n"
***************
*** 1397,1401 ****
        while ( cmSystemTools::GetLineFromStream(ifs, line) )
          {
!         os << this->MakeXMLSafe(line) << std::endl;
          }
        ifs.close();
--- 1346,1350 ----
        while ( cmSystemTools::GetLineFromStream(ifs, line) )
          {
!         os << cmXMLSafe(line) << std::endl;
          }
        ifs.close();

Index: CMakeLists.txt
===================================================================
RCS file: /cvsroot/CMake/CMake/Source/CMakeLists.txt,v
retrieving revision 1.405
retrieving revision 1.406
diff -C 2 -d -r1.405 -r1.406
*** CMakeLists.txt	22 Jan 2009 17:12:44 -0000	1.405
--- CMakeLists.txt	5 Feb 2009 21:31:33 -0000	1.406
***************
*** 214,217 ****
--- 214,219 ----
    cmXMLParser.cxx
    cmXMLParser.h
+   cmXMLSafe.cxx
+   cmXMLSafe.h
    cmake.cxx
    cmake.h

Index: cmCTest.h
===================================================================
RCS file: /cvsroot/CMake/CMake/Source/cmCTest.h,v
retrieving revision 1.112
retrieving revision 1.113
diff -C 2 -d -r1.112 -r1.113
*** cmCTest.h	27 Jan 2009 15:58:33 -0000	1.112
--- cmCTest.h	5 Feb 2009 21:31:34 -0000	1.113
***************
*** 201,207 ****
                        bool compress = false);
  
-   ///! Convert string to something that is XML safe
-   static std::string MakeXMLSafe(const std::string&);
- 
    ///! Should we only show what we would do?
    bool GetShowOnly();
--- 201,204 ----

--- NEW FILE: cmXMLSafe.h ---
/*=========================================================================

  Program:   CMake - Cross-Platform Makefile Generator
  Module:    $RCSfile: cmXMLSafe.h,v $
  Language:  C++
  Date:      $Date: 2009-02-05 21:31:37 $
  Version:   $Revision: 1.1 $

  Copyright (c) 2002 Kitware, Inc., Insight Consortium.  All rights reserved.
  See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/
#ifndef cmXMLSafe_h
#define cmXMLSafe_h

#include <cmsys/stl/string>
#include <cmsys/ios/iosfwd>

/** \class cmXMLSafe
 * \brief Write strings to XML with proper escapes
 */
class cmXMLSafe
{
public:
  /** Construct with the data to be written.  This assumes the data
      will exist for the duration of this object's life.  */
  cmXMLSafe(const char* s);
  cmXMLSafe(cmsys_stl::string const& str);

  /** Specify whether to escape quotes too.  This is needed when
      writing the content of an attribute value.  By default quotes
      are escaped.  */
  cmXMLSafe& Quotes(bool b = true);

  /** Get the escaped data as a string.  */
  cmsys_stl::string str();
private:
  char const* Data;
  unsigned long Size;
  bool DoQuotes;
  friend cmsys_ios::ostream& operator<<(cmsys_ios::ostream&,
                                        cmXMLSafe const&);
};

#endif



More information about the Cmake-commits mailing list