macosunicodeconverter.cpp

来自「IBM的解析xml的工具Xerces的源代码」· C++ 代码 · 共 1,049 行 · 第 1/3 页

CPP
1,049
字号
bool MacOSUnicodeConverter::supportsSrcOfs() const{	// For now, we don't support source offsets    return false;}void MacOSUnicodeConverter::upperCase(XMLCh* const toUpperCase) const{#if TARGET_API_MAC_CARBON   // If we're targeting carbon, use the CFString conversion to uppercase   int len = XMLString::stringLen(toUpperCase);   CFMutableStringRef cfString = CFStringCreateMutableWithExternalCharactersNoCopy(        kCFAllocatorDefault,        (UniChar*)toUpperCase,        len,		// length        len,		// capacity        kCFAllocatorNull);   CFStringUppercase(cfString, NULL);   CFRelease(cfString);#elif defined(XML_METROWERKS) || (__GNUC__ >= 3 && _GLIBCPP_USE_WCHAR_T)	// Use this if there's a reasonable c library available.	// Metrowerks does this reasonably	wchar_t c;	for (XMLCh* p = (XMLCh*)toUpperCase; ((c = *p) != 0); )		*p++ = std::towupper(c);#else	#error Sorry, no support for upperCase#endif}void MacOSUnicodeConverter::lowerCase(XMLCh* const toLowerCase) const{#if TARGET_API_MAC_CARBON   // If we're targeting carbon, use the CFString conversion to uppercase   int len = XMLString::stringLen(toLowerCase);   CFMutableStringRef cfString = CFStringCreateMutableWithExternalCharactersNoCopy(        kCFAllocatorDefault,        (UniChar*)toLowerCase,        len,		// length        len,		// capacity        kCFAllocatorNull);   CFStringLowercase(cfString, NULL);   CFRelease(cfString);#elif defined(XML_METROWERKS) || (__GNUC__ >= 3 && _GLIBCPP_USE_WCHAR_T)	// Use this if there's a reasonable c library available.	// Metrowerks does this reasonably	wchar_t c;	for (XMLCh* p = (XMLCh*)toLowerCase; ((c = *p) != 0); )		*p++ = std::towlower(c);#else	#error Sorry, no support for lowerCase#endif}voidMacOSUnicodeConverter::ConvertWideToNarrow(const XMLCh* wide, char* narrow, std::size_t maxChars){	while (maxChars-- > 0)		if ((*narrow++ = *wide++) == 0)			break;}// ---------------------------------------------------------------------------//  MacOSTransService: The protected virtual transcoding service API// ---------------------------------------------------------------------------XMLTranscoder*MacOSUnicodeConverter::makeNewXMLTranscoder(const   XMLCh* const		encodingName                                        ,       XMLTransService::Codes& resValue                                        , const unsigned int			blockSize                                        ,       MemoryManager* const    manager){	XMLTranscoder* result = NULL;	resValue = XMLTransService::Ok;		TextToUnicodeInfo textToUnicodeInfo = NULL;	UnicodeToTextInfo unicodeToTextInfo = NULL;	//	Map the encoding to a Mac OS Encoding value	Str255 pasEncodingName;	char cEncodingName[256];	ConvertWideToNarrow(encodingName, cEncodingName, sizeof(cEncodingName));	CopyCStringToPascal(cEncodingName, pasEncodingName);		TextEncoding textEncoding = 0;	OSStatus status = TECGetTextEncodingFromInternetName (							&textEncoding,							pasEncodingName);                                //  Make a transcoder for that encoding	if (status == noErr)		result = makeNewXMLTranscoder(encodingName, resValue, blockSize, textEncoding, manager);	else		resValue = XMLTransService::UnsupportedEncoding;		return result;}XMLTranscoder*MacOSUnicodeConverter::makeNewXMLTranscoder(const   XMLCh* const		encodingName                                        ,       XMLTransService::Codes& resValue                                        , const unsigned int			blockSize										,		TextEncoding            textEncoding                                        ,       MemoryManager* const    manager){    XMLTranscoder* result = NULL;	resValue = XMLTransService::Ok;    OSStatus status = noErr;        TECObjectRef textToUnicode = NULL;    TECObjectRef unicodeToText = NULL;        //  We convert to and from utf16    TextEncoding utf16Encoding = CreateTextEncoding(kTextEncodingUnicodeDefault,                                        kTextEncodingDefaultVariant,                                        kUnicode16BitFormat);    //  Create a TEC from our encoding to utf16    if (status == noErr)        status = TECCreateConverter(&textToUnicode, textEncoding, utf16Encoding);    //  Create a TEC from utf16 to our encoding    if (status == noErr)        status = TECCreateConverter(&unicodeToText, utf16Encoding, textEncoding);	if (status != noErr)	{        //  Clean up on error		if (textToUnicode != NULL)            TECDisposeConverter(textToUnicode);					if (unicodeToText != NULL)            TECDisposeConverter(unicodeToText);		resValue = XMLTransService::UnsupportedEncoding;	}	else    {        //  Create our transcoder, passing in the converters		result = new (manager) MacOSTranscoder(encodingName, textToUnicode, unicodeToText, blockSize, manager);    }	    return result;}// ---------------------------------------------------------------------------//  IsMacOSUnicodeConverterSupported// ---------------------------------------------------------------------------boolMacOSUnicodeConverter::IsMacOSUnicodeConverterSupported(void){    return UpgradeScriptInfoToTextEncoding != (void*)kUnresolvedCFragSymbolAddress        && CreateTextToUnicodeInfoByEncoding != (void*)kUnresolvedCFragSymbolAddress        ;}// ---------------------------------------------------------------------------//  MacOSTranscoder: Constructors and Destructor// ---------------------------------------------------------------------------MacOSTranscoder::MacOSTranscoder(const  XMLCh* const    encodingName								, TECObjectRef          textToUnicode								, TECObjectRef          unicodeToText                                , const unsigned int    blockSize                                , MemoryManager* const  manager) :    XMLTranscoder(encodingName, blockSize, manager),    mTextToUnicode(textToUnicode),    mUnicodeToText(unicodeToText){}MacOSTranscoder::~MacOSTranscoder(){	//	Dispose our text encoding converters	TECDisposeConverter(mTextToUnicode);	TECDisposeConverter(mUnicodeToText);}// ---------------------------------------------------------------------------//  MacOSTranscoder: The virtual transcoder API// ---------------------------------------------------------------------------unsigned intMacOSTranscoder::transcodeFrom(  const  XMLByte* const			srcData                                , const unsigned int			srcCount                                ,       XMLCh* const			toFill                                , const unsigned int			maxChars                                ,       unsigned int&			bytesEaten                                ,       unsigned char* const	charSizes){	//  Reset the tec state (since we don't know that we're part of a	//  larger run of text).	TECClearConverterContextInfo(mTextToUnicode);	    //  Do the conversion    ByteCount bytesConsumed = 0;    ByteCount bytesProduced = 0;    OSStatus status = TECConvertText(mTextToUnicode,                (ConstTextPtr) srcData,                srcCount,                   // inputBufferLength                &bytesConsumed,				// actualInputLength                (TextPtr) toFill,           // outputBuffer                maxChars * sizeof(XMLCh),	// outputBufferLength                &bytesProduced);			// actualOutputLength    //  Ignorable error codes    if(    status == kTECUsedFallbacksStatus        || status == kTECOutputBufferFullStatus        || status == kTECPartialCharErr		)        status = noErr;    	    if (status != noErr)        ThrowXML(TranscodingException, XMLExcepts::Trans_BadSrcSeq);		std::size_t charsProduced = bytesProduced / sizeof(XMLCh);	    bytesEaten = bytesConsumed;    return charsProduced;}unsigned intMacOSTranscoder::transcodeTo(const  XMLCh* const    srcData                            , const unsigned int    srcCount                            ,       XMLByte* const  toFill                            , const unsigned int    maxBytes                            ,       unsigned int&   charsEaten                            , const UnRepOpts       options){	//  Reset the tec state (since we don't know that we're part of a	//  larger run of text).	TECClearConverterContextInfo(mUnicodeToText);	    //  Do the conversion    ByteCount bytesConsumed = 0;    ByteCount bytesProduced = 0;    OSStatus status = TECConvertText(mUnicodeToText,                (ConstTextPtr) srcData,                srcCount * sizeof(XMLCh),   // inputBufferLength                &bytesConsumed,				// actualInputLength                (TextPtr) toFill,           // outputBuffer                maxBytes,                   // outputBufferLength                &bytesProduced);			// actualOutputLength    //  Ignorable error codes    if(    status == kTECUsedFallbacksStatus        || status == kTECOutputBufferFullStatus        || status == kTECPartialCharErr		)        status = noErr;            std::size_t charsConsumed = bytesConsumed / sizeof(XMLCh);        //  Deal with errors    if (status != noErr)    {    	if (status == kTECUnmappableElementErr && options == UnRep_Throw)    	{    		XMLCh tmpBuf[17];            XMLString::binToText((unsigned int)&srcData[charsConsumed], tmpBuf, 16, 16);            ThrowXML2            (                TranscodingException                , XMLExcepts::Trans_Unrepresentable                , tmpBuf                , getEncodingName()            );    	}    }	    charsEaten = charsConsumed;    return bytesProduced;}boolMacOSTranscoder::canTranscodeTo(const unsigned int toCheck) const{	//    //  If the passed value is really a surrogate embedded together, then    //  we need to break it out into its two chars. Else just one.    //    unsigned int    srcCnt = 0;    UniChar         srcBuf[2];    if (toCheck & 0xFFFF0000)    {        srcBuf[srcCnt++] = XMLCh(toCheck >> 10)   + 0xD800;        srcBuf[srcCnt++] = XMLCh(toCheck & 0x3FF) + 0xDC00;    }    else    {        srcBuf[srcCnt++] = XMLCh(toCheck);    }	//  Clear the converter state: we're in a new run of text	TECClearConverterContextInfo(mUnicodeToText);    //    //  Use a local temp buffer that would hold any sane multi-byte char    //  sequence and try to transcode this guy into it.    //    char tmpBuf[64];    ByteCount bytesConsumed = 0;    ByteCount bytesProduced = 0;    OSStatus status = TECConvertText(mUnicodeToText,                (ConstTextPtr) srcBuf,                srcCnt * sizeof(XMLCh),     // inputBufferLength                &bytesConsumed,				// actualInputLength                (TextPtr) tmpBuf,           // outputBuffer                sizeof(tmpBuf),             // outputBufferLength                &bytesProduced);			// actualOutputLength    std::size_t charsConsumed = bytesConsumed / sizeof(XMLCh);		//	Return true if we transcoded the character(s)	//	successfully	return status == noErr && charsConsumed == srcCnt;}// ---------------------------------------------------------------------------//  MacOSLCPTranscoder: Constructors and Destructor// ---------------------------------------------------------------------------MacOSLCPTranscoder::MacOSLCPTranscoder(XMLTranscoder* const transcoder, MemoryManager* const manager) : mTranscoder(transcoder),   mManager(manager){}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?