You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xalan.apache.org by dm...@apache.org on 2005/08/08 17:25:52 UTC

cvs commit: xml-xalan/c/src/xalanc/XalanDOM XalanDOMString.cpp XalanDOMString.hpp

dmitryh     2005/08/08 08:25:52

  Modified:    c/src/xalanc/Include GCCDefinitions.hpp OS390Definitions.hpp
               c/src/xalanc/PlatformSupport DOMStringHelper.cpp
               c/src/xalanc/XalanDOM XalanDOMString.cpp XalanDOMString.hpp
  Log:
  Fix for XALANC-535
  
  Revision  Changes    Path
  1.11      +0 -1      xml-xalan/c/src/xalanc/Include/GCCDefinitions.hpp
  
  Index: GCCDefinitions.hpp
  ===================================================================
  RCS file: /home/cvs/xml-xalan/c/src/xalanc/Include/GCCDefinitions.hpp,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- GCCDefinitions.hpp	11 Apr 2005 02:16:55 -0000	1.10
  +++ GCCDefinitions.hpp	8 Aug 2005 15:25:52 -0000	1.11
  @@ -47,7 +47,6 @@
   #define XALAN_HAS_STD_DISTANCE
   
   #if defined(CYGWIN)
  -#define XALAN_USE_XERCES_LOCAL_CODEPAGE_TRANSCODERS
   #if defined(WIN32)
   #undef WIN32
   #endif
  
  
  
  1.5       +0 -1      xml-xalan/c/src/xalanc/Include/OS390Definitions.hpp
  
  Index: OS390Definitions.hpp
  ===================================================================
  RCS file: /home/cvs/xml-xalan/c/src/xalanc/Include/OS390Definitions.hpp,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- OS390Definitions.hpp	26 Feb 2004 22:30:58 -0000	1.4
  +++ OS390Definitions.hpp	8 Aug 2005 15:25:52 -0000	1.5
  @@ -33,7 +33,6 @@
   
   #define XALAN_XALANDOMCHAR_USHORT_MISMATCH
   #define XALAN_USE_WCHAR_CAST_HACK
  -#define XALAN_USE_XERCES_LOCAL_CODEPAGE_TRANSCODERS
   #define XALAN_NON_ASCII_PLATFORM
   
   #define XALAN_EXPLICIT_SCOPE_IN_TEMPLATE_BUG
  
  
  
  1.12      +1 -1      xml-xalan/c/src/xalanc/PlatformSupport/DOMStringHelper.cpp
  
  Index: DOMStringHelper.cpp
  ===================================================================
  RCS file: /home/cvs/xml-xalan/c/src/xalanc/PlatformSupport/DOMStringHelper.cpp,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- DOMStringHelper.cpp	26 Jul 2005 20:09:17 -0000	1.11
  +++ DOMStringHelper.cpp	8 Aug 2005 15:25:52 -0000	1.12
  @@ -372,7 +372,7 @@
   {
       CharVectorType  theVector(theMemoryManager);
   
  -    TranscodeToLocalCodePage(theString, theVector);
  +    TranscodeToLocalCodePage(theString, theVector, false, '?');
   
   	OutputString(theStream, theVector);
   }
  
  
  
  1.16      +178 -160  xml-xalan/c/src/xalanc/XalanDOM/XalanDOMString.cpp
  
  Index: XalanDOMString.cpp
  ===================================================================
  RCS file: /home/cvs/xml-xalan/c/src/xalanc/XalanDOM/XalanDOMString.cpp,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- XalanDOMString.cpp	12 May 2005 04:46:26 -0000	1.15
  +++ XalanDOMString.cpp	8 Aug 2005 15:25:52 -0000	1.16
  @@ -25,14 +25,13 @@
   #include <xalanc/Include/XalanMemMngArrayAllocate.hpp>
   
   
  -
   #include <cstdlib>
   
   
   
   XALAN_CPP_NAMESPACE_BEGIN
   
  -//#define XALAN_USE_XERCES_LOCAL_CODEPAGE_TRANSCODERS
  +
   
   const XalanDOMChar	XalanDOMString::s_empty = 0;
   
  @@ -808,7 +807,6 @@
   
   
   
  -#if defined(XALAN_USE_XERCES_LOCAL_CODEPAGE_TRANSCODERS)
   
   
   
  @@ -822,7 +820,119 @@
   
   XALAN_CPP_NAMESPACE_BEGIN
   
  +// The template function should never fail.
  +// In case of the unconvertable characters we add the substitution char
  +// and in case of too small result vector we resize it 
  +template <class SourceType, class TargetType>
  +void
  +doXercesTranscode(
  +            const SourceType*           theSourceString,
  +            XalanDOMString::size_type   theSourceStringLength,
  +            bool                        theSourceStringIsNullTerminated,
  +            XalanVector<TargetType>&    theTargetVector,
  +            bool                        terminate,
  +            char                        theSubstitutionChar)
  +{
  +
  +	const SourceType*	theRealSourceString = theSourceString;
  +
  +	XalanDOMString::size_type	theRealSourceStringLength = theSourceStringLength;
  +
  +	XalanMemMgrAutoPtrArray<SourceType>		theGuard;
  +
  +	if (theSourceStringIsNullTerminated == true)
  +	{
  +		theRealSourceStringLength = XalanDOMString::length(theSourceString);
  +	}
  +	else
  +	{
  +        typedef XalanMemMngArrayAllocate<SourceType> ArrayAllocateSourceType;
  +
  +        theGuard.reset( &( theTargetVector.getMemoryManager()), 
  +                        ArrayAllocateSourceType::allocate(theRealSourceStringLength + 1, theTargetVector.getMemoryManager()),
  +                        theRealSourceStringLength + 1);
  +
  +        assert(theGuard.get() != 0);
  +
  +        for (XalanDOMString::size_type index = 0; index < theRealSourceStringLength; ++index)
  +        {
  +            theGuard[index] = theSourceString[index];
  +        }
  +
  +        theGuard[theRealSourceStringLength] = SourceType(0);
  +
  +        theRealSourceString = theGuard.get(); 
  +    }
  +
  +    // wild guessing about the size of the result transcoded vector
  +    theTargetVector.resize(2*(theRealSourceStringLength + 1), TargetType(0));
  +
  +    assert(theRealSourceString != 0);
  +
  +    bool			fSuccess = false;
  +
  +    XALAN_USING_XERCES(XMLString)
   
  +    // $$$ ToDo: We should use the Xerces transcoder interface
  +    // instead of XMLString::transcode(), so we can better control
  +    // error handling and failures due to inadequate space.
  +
  +    fSuccess = XMLString::transcode(
  +        theRealSourceString,
  +        &*theTargetVector.begin(),
  +        (unsigned int)(theTargetVector.size() - 1),
  +        & (theTargetVector.getMemoryManager()));
  +
  +    if (fSuccess == false)
  +    {
  +        // Do the "manual" transcoding. First , clean uo form the previose phase
  +        theTargetVector.clear();
  +
  +        //check if there is illegal caracters for the local code page encoding
  +        SourceType oneCharArray[2];
  +
  +        oneCharArray[1] = SourceType(0);
  +
  +        const size_t theOneTranslatedWbCharLen = 10;
  +
  +        TargetType theOneTranslatedWbChar[theOneTranslatedWbCharLen]; 
  +
  +        for (size_t i = 0 ; i <= theRealSourceStringLength ; ++i)
  +        {
  +            oneCharArray[0] = theRealSourceString[i];
  +
  +            theOneTranslatedWbChar[0] = TargetType(0);
  +
  +            fSuccess = XMLString::transcode(
  +                oneCharArray,
  +                theOneTranslatedWbChar,
  +                theOneTranslatedWbCharLen - 1,
  +                & (theTargetVector.getMemoryManager()));
  +
  +            if( fSuccess == false )
  +            {
  +                theTargetVector.push_back(theSubstitutionChar);
  +            }
  +            else
  +            {
  +                // append the translated set of characters
  +                theTargetVector.insert(
  +                    theTargetVector.end(),
  +                    theOneTranslatedWbChar,
  +                    theOneTranslatedWbChar + XalanDOMString::length(theOneTranslatedWbChar));
  +            }
  +        }
  +
  +    }
  +
  +    if (terminate == false)
  +	{
  +		while(theTargetVector.back() == TargetType(0))
  +		{
  +			theTargetVector.pop_back();
  +		}
  +	}
  +}
   
   template <class SourceType, class TargetType>
   bool
  @@ -882,7 +992,7 @@
   		fSuccess = XMLString::transcode(
   					theRealSourceString,
   					&*theTargetVector.begin(),
  -					theTargetVector.size() - 1,
  +					(unsigned int)(theTargetVector.size() - 1),
                       & (theTargetVector.getMemoryManager()));
   
   		if (fSuccess == false)
  @@ -915,22 +1025,16 @@
   	return fSuccess;
   }
   
  -#endif
   
  -
  -
  -static bool
  +static void
   doTranscodeToLocalCodePage(
   			const XalanDOMChar*			theSourceString,
   			XalanDOMString::size_type	theSourceStringLength,
   			bool						theSourceStringIsNullTerminated,
   			CharVectorType&				theTargetVector,
  -			bool						terminate)
  +			bool						terminate,
  +            char                        theSubstitutionChar)
   {
  -#if defined(XALAN_STRICT_ANSI_HEADERS)
  -	using std::wcstombs;
  -#endif
  -
       // Short circuit if it's a null pointer, or of length 0.
       if (!theSourceString || (!theSourceString[0]))
       {
  @@ -944,93 +1048,89 @@
   		{
   			theTargetVector.clear();
   		}
  -
  -        return true;
   	}
  -#if defined(XALAN_USE_XERCES_LOCAL_CODEPAGE_TRANSCODERS)
   	else
   	{
  -		return doXercesTranscode(
  +		doXercesTranscode(
   					theSourceString,
   					theSourceStringLength,
   					theSourceStringIsNullTerminated,
   					theTargetVector,
  -					terminate);
  +					terminate,
  +                    theSubstitutionChar);
   	}
  -#else
  -	const wchar_t*	theTempSource = 0;
  +}
   
  -	// If our char sizes are not the same, or the input string is not null-terminated,
  -	// we have to use a temporary buffer.
  -	XalanMemMgrAutoPtrArray<wchar_t>	theTempSourceJanitor;
  -
  -#if !defined(XALAN_XALANDOMCHAR_USHORT_MISMATCH)
  -	// This is a short-cut for when the theSourceString is mull-terminated _and_
  -	// XalanDOMChar and wchar_t are the same thing.
  -	if (theSourceStringIsNullTerminated == true)
  -	{
  -		theTempSource = theSourceString;
  -	}
  -	else
  -#endif
  -	{
  -		if (theSourceStringIsNullTerminated == true)
  +static bool
  +doTranscodeToLocalCodePage(
  +			const XalanDOMChar*			theSourceString,
  +			XalanDOMString::size_type	theSourceStringLength,
  +			bool						theSourceStringIsNullTerminated,
  +			CharVectorType&				theTargetVector,
  +			bool						terminate)
  +{
  +    // Short circuit if it's a null pointer, or of length 0.
  +    if (!theSourceString || (!theSourceString[0]))
  +    {
  +		if (terminate == true)
   		{
  -			theSourceStringLength = length(theSourceString);
  -		}
  -
  -        theTempSourceJanitor.reset(
  -            &(theTargetVector.getMemoryManager()),
  -            XalanMemMngArrayAllocate<wchar_t>::allocate( theSourceStringLength + 1, theTargetVector.getMemoryManager()),
  -            theSourceStringLength + 1);
  -
  -        typedef XalanMemMgrAutoPtrArray<wchar_t>::size_type size_type;
  +			theTargetVector.resize(1);
   
  -		for (size_t	index = 0; index < theSourceStringLength; ++index)
  +			theTargetVector.back() = '\0';
  +		}
  +		else
   		{
  -			theTempSourceJanitor[size_type(index)] = wchar_t(theSourceString[index]);
  +			theTargetVector.clear();
   		}
   
  -		theTempSourceJanitor[theSourceStringLength] = 0;
  -
  -		theTempSource = theTempSourceJanitor.get();
  -	}
  -
  -    // See how many chars we need to transcode.
  -    const size_t	targetLen = wcstombs(0, theTempSource, 0);
  -
  -	if (targetLen == ~size_t(0))
  -	{
  -		return false;
  +        return true;
   	}
   	else
   	{
  -		// Resize, adding one byte if terminating...
  -		theTargetVector.resize(terminate == true ? targetLen + 1 : targetLen);
  -
  -		//  And transcode our temp source buffer to the local buffer. Terminate
  -		//
  -		if (wcstombs(&theTargetVector[0], theTempSource, targetLen) == ~size_t(0))
  -		{
  -			theTargetVector.clear();
  -
  -			return false;
  -		}
  -		else
  -		{
  -			if (terminate == true)
  -			{
  -				theTargetVector.back() = '\0';
  -			}
  -
  -			return true;
  -		}
  +		return doXercesTranscode(
  +					theSourceString,
  +					theSourceStringLength,
  +					theSourceStringIsNullTerminated,
  +					theTargetVector,
  +					terminate);
   	}
  -#endif
  +}
  +
  +XALAN_DOM_EXPORT_FUNCTION(void)
  +TranscodeToLocalCodePage(
  +			const XalanDOMChar*			theSourceString,
  +			XalanDOMString::size_type	theSourceStringLength,
  +			CharVectorType&				theTargetVector,
  +			bool						terminate,
  +            char                        theSubstitutionChar)
  +{
  +	doTranscodeToLocalCodePage(
  +                            theSourceString, 
  +                            theSourceStringLength, 
  +                            false, 
  +                            theTargetVector, 
  +                            terminate,
  +                            theSubstitutionChar);
   }
   
   
   
  +XALAN_DOM_EXPORT_FUNCTION(void)
  +TranscodeToLocalCodePage(
  +			const XalanDOMChar*		theSourceString,
  +			CharVectorType&			theTargetVector,
  +			bool					terminate,
  +            char                    theSubstitutionChar)
  +{
  +	doTranscodeToLocalCodePage(
  +                            theSourceString, 
  +                            0, 
  +                            true, 
  +                            theTargetVector, 
  +                            terminate,
  +                            theSubstitutionChar);
  +}
  +
   XALAN_DOM_EXPORT_FUNCTION(bool)
   TranscodeToLocalCodePage(
   			const XalanDOMChar*			theSourceString,
  @@ -1062,10 +1162,6 @@
   			XalanDOMCharVectorType&		theTargetVector,
   			bool						terminate)
   {
  -#if defined(XALAN_STRICT_ANSI_HEADERS)
  -	using std::mbstowcs;
  -#endif
  -
   	typedef XalanDOMString::size_type	size_type;
   
       // Short circuit if it's a null pointer, or of length 0.
  @@ -1084,7 +1180,6 @@
   
           return true;
   	}
  -#if defined(XALAN_USE_XERCES_LOCAL_CODEPAGE_TRANSCODERS)
   	else
   	{
   		return doXercesTranscode(
  @@ -1094,83 +1189,6 @@
   					theTargetVector,
   					terminate);
   	}
  -#else
  -    XalanMemMgrAutoPtrArray<char> tempString;
  -
  -	if (theSourceStringIsNullTerminated == true)
  -	{
  -		theSourceStringLength = XalanDOMString::length(theSourceString);
  -	}
  -	else
  -	{
  -		tempString.reset(
  -            &(theTargetVector.getMemoryManager()),
  -            XalanMemMngArrayAllocate<char>::allocate( theSourceStringLength + 1, theTargetVector.getMemoryManager()),
  -            theSourceStringLength + 1);
  -
  -#if defined(XALAN_STRICT_ANSI_HEADERS)
  -		std::strncpy(tempString.get(), theSourceString, theSourceStringLength);
  -#else
  -		strncpy(tempString.get(), theSourceString, theSourceStringLength);
  -#endif
  -
  -		tempString[theSourceStringLength] = '\0';
  -
  -		theSourceString = tempString.get();
  -	}
  -
  -    // See how many chars we need to transcode.
  -	const size_t	theTargetLength =
  -			mbstowcs(0, theSourceString, size_t(theSourceStringLength));
  -
  -	if (theTargetLength == ~size_t(0))
  -	{
  -		return false;
  -	}
  -	else
  -	{
  -#if defined(XALAN_XALANDOMCHAR_USHORT_MISMATCH)
  -		typedef XalanDOMString::WideCharVectorType	WideCharVectorType;
  -
  -		WideCharVectorType	theTempResult(theTargetVector.getMemoryManager());
  -
  -		theTempResult.resize(terminate == true ? theTargetLength + 1 : theTargetLength);
  -
  -		wchar_t* const	theTargetPointer = &theTempResult[0];
  -#else
  -		theTargetVector.resize(terminate == true ? theTargetLength + 1 : theTargetLength);
  -
  -		wchar_t* const	theTargetPointer = &theTargetVector[0];
  -#endif
  -
  -		if (mbstowcs(theTargetPointer, theSourceString, size_t(theSourceStringLength)) == ~size_t(0))
  -		{
  -			theTargetVector.clear();
  -
  -			return false;
  -		}
  -		else
  -		{
  -#if defined(XALAN_XALANDOMCHAR_USHORT_MISMATCH)
  -			const WideCharVectorType::size_type		theTempSize = theTempResult.size();
  -
  -			theTargetVector.reserve(theTempSize);
  -
  -			for(WideCharVectorType::size_type i = 0; i < theTempSize; ++i)
  -			{
  -				theTargetVector.push_back(theTempResult[i]);
  -			}
  -#endif
  -
  -			if (terminate == true)
  -			{
  -				theTargetVector.back() = '\0';
  -			}
  -
  -			return true;
  -		}
  -	}
  -#endif
   }
   
   
  
  
  
  1.13      +58 -3     xml-xalan/c/src/xalanc/XalanDOM/XalanDOMString.hpp
  
  Index: XalanDOMString.hpp
  ===================================================================
  RCS file: /home/cvs/xml-xalan/c/src/xalanc/XalanDOM/XalanDOMString.hpp,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- XalanDOMString.hpp	10 Nov 2004 19:09:12 -0000	1.12
  +++ XalanDOMString.hpp	8 Aug 2005 15:25:52 -0000	1.13
  @@ -957,6 +957,27 @@
   			bool						terminate = false);
   
   /**
  + * Convert a XalanDOMChar string to C++ standard library
  + * vector, transcoding to the default local code
  + * page. If the source string contines code points, that can't be 
  + * represented in the local code page, the substitution character will be used
  + *
  + * @param sourceString The source string
  + * @param sourceStringLength The source string length.
  + * @param targetVector The target string
  + * @param terminate If true, the transcoded string will be null-terminated
  + * @param theSubstitutionChar The substitution character for code points that are not presentable
  + *                              in the local page
  + */
  +XALAN_DOM_EXPORT_FUNCTION(void)
  +TranscodeToLocalCodePage(
  +            const XalanDOMChar*         theSourceString,
  +            XalanDOMString::size_type   theSourceStringLength,
  +            CharVectorType&             targetVector,
  +            bool                        terminate,
  +            char                        theSubstitutionChar);
  +
  +/**
    * Convert a string to a XalanDOMString, transcoding from
    * the default local code page.
    * 
  @@ -992,6 +1013,22 @@
   			bool					terminate = false);
   
   /**
  + * Convert a XalanDOMChar string to C++ standard library
  + * vector, transcoding to the default local code
  + * page.  The string _must_ be null-terminated.
  + * 
  + * @param theSourceString The source string
  + * @param targetVector The target string
  + * @param terminate If true, the transcoded string will be null-terminated
  + */
  +XALAN_DOM_EXPORT_FUNCTION(void)
  +TranscodeToLocalCodePage(
  +            const XalanDOMChar*     theSourceString,
  +            CharVectorType&         targetVector,
  +            bool                    terminate,
  +            char                    theSubstitutionChar);
  +
  +/**
    * Convert XalanDOMString to C++ standard library
    * vector, transcoding to the default local code
    * page.  Null-terminate the sttring...
  @@ -1005,7 +1042,7 @@
   {
   	CharVectorType	theResult;
   
  -	TranscodeToLocalCodePage(theSourceString, theResult, true);
  +	TranscodeToLocalCodePage(theSourceString, theResult, true, '?');
   
   	return theResult;
   }
  @@ -1030,7 +1067,25 @@
   	return TranscodeToLocalCodePage(theSourceString.c_str(), targetVector, terminate);
   }
   
  -
  +/**
  + * Convert XalanDOMString to C++ standard library
  + * vector, transcoding to the default local code
  + * page.
  + * 
  + * @param theSourceString The source string
  + * @param targetVector The target string
  + * @param theSubstitutionChar The substitution character for code points that are not presentable
  + *                              in the local page
  + */
  +inline void
  +TranscodeToLocalCodePage(
  +			const XalanDOMString&	theSourceString,
  +			CharVectorType&			targetVector,
  +			bool					terminate ,
  +            char                    theSubstitutionChar)
  +{
  +	TranscodeToLocalCodePage(theSourceString.c_str(), targetVector, terminate, theSubstitutionChar);
  +}
   
   /**
    * Convert XalanDOMString to C++ standard library
  @@ -1046,7 +1101,7 @@
   {
   	CharVectorType	theResult;
   
  -	TranscodeToLocalCodePage(theSourceString, theResult, true);
  +	TranscodeToLocalCodePage(theSourceString.c_str(), theResult, true, '?');
   
   	return theResult;
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: xalan-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: xalan-cvs-help@xml.apache.org