cygwintransservice.cpp

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

CPP
1,173
字号
/* * Copyright 2002-2004 The Apache Software Foundation. *  * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at *  *      http://www.apache.org/licenses/LICENSE-2.0 *  * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *//* * $Log: CygwinTransService.cpp,v $ * Revision 1.13  2004/09/08 13:56:43  peiyongz * Apache License Version 2.0 * * Revision 1.12  2004/01/19 16:06:56  amassari * WideCharToMultiByte and MultiByteToWideChar return 0 on failure, not -1 * * Revision 1.11  2004/01/13 16:34:22  cargilld * Misc memory management changes. * * Revision 1.10  2003/12/24 15:24:15  cargilld * More updates to memory management so that the static memory manager. * * Revision 1.9  2003/11/08 23:37:00  neilg * fix for bug 24287 by Abe Backus. * * Revision 1.8  2003/05/17 16:32:17  knoaman * Memory manager implementation : transcoder update. * * Revision 1.7  2003/05/16 14:04:46  neilg * fix compilation error * * Revision 1.6  2003/05/16 06:01:57  knoaman * Partial implementation of the configurable memory manager. * * Revision 1.5  2003/05/15 18:47:02  knoaman * Partial implementation of the configurable memory manager. * * Revision 1.4  2003/03/09 17:02:20  peiyongz * PanicHandler * * Revision 1.3  2003/03/07 18:15:57  tng * Return a reference instead of void for operator= * * Revision 1.2  2002/11/04 15:14:32  tng * C++ Namespace Support. * * Revision 1.1  2002/08/19 18:35:56  tng * [Bug 6467] Installing Xerces C++ on cygwin environment. * */// ---------------------------------------------------------------------------//  Includes// ---------------------------------------------------------------------------#include <xercesc/util/PlatformUtils.hpp>#include <xercesc/util/TranscodingException.hpp>#include <xercesc/util/XMLException.hpp>#include <xercesc/util/XMLString.hpp>#include <xercesc/util/XMLUniDefs.hpp>#include <xercesc/util/XMLUni.hpp>#include <xercesc/util/RefHashTableOf.hpp>#include "CygwinTransService.hpp"#include <windows.h>#include <stdlib.h>XERCES_CPP_NAMESPACE_BEGIN// ---------------------------------------------------------------------------//  Local, const data// ---------------------------------------------------------------------------static const XMLCh gMyServiceId[] ={    chLatin_C, chLatin_y, chLatin_g, chLatin_w, chLatin_i, chLatin_n, chNull};// Cygwin doesn't support iswspace(), so this table is used by// CygwinTransService::isSpace() based on a union of Unicode// Table 6-1 and the ANSI definition of whitespace, arranged// in order of likely occurrence.static const XMLCh gWhitespace[] ={    0x0020,    0x00a0,    0x0009,    0x000a,    0x000d,    0x000b,    0x000c,    0x3000,    0x2000,    0x2001,    0x2002,    0x2003,    0x2004,    0x2005,    0x2006,    0x2007,    0x2008,    0x2009,    0x200a,    0x200b,    0x202f};// Used by the kernel32 function LCMapStringW to uppercasify strings// appropriate to this locale.  Cygwin doesn't support _wcsupr().static const LCID gLocaleId =#if defined(CYGWINTRANSCODER_DEFAULT_LOCALE)    MAKELCID( MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT), SORT_DEFAULT);#else    // CYGWINTRANSCODER_DYNAMIC_LOCALE    GetThreadLocale();#endif// ---------------------------------------------------------------------------//  This is the simple CPMapEntry class. It just contains an encoding name//  and a code page for that encoding.// ---------------------------------------------------------------------------class CPMapEntry : public XMemory{public :    // -----------------------------------------------------------------------    //  Constructors and Destructor    // -----------------------------------------------------------------------    CPMapEntry    (        const   XMLCh* const    encodingName        , const unsigned int    cpId        , const unsigned int    ieId    );    CPMapEntry    (        const   char* const     encodingName        , const unsigned int    cpId        , const unsigned int    ieId    );    ~CPMapEntry();    // -----------------------------------------------------------------------    //  Getter methods    // -----------------------------------------------------------------------    const XMLCh* getEncodingName() const;    const XMLCh* getKey() const;    unsigned int getWinCP() const;    unsigned int getIEEncoding() const;private :    // -----------------------------------------------------------------------    //  Unimplemented constructors and operators    // -----------------------------------------------------------------------    CPMapEntry();    CPMapEntry(const CPMapEntry&);    CPMapEntry& operator=(const CPMapEntry&);    // -----------------------------------------------------------------------    //  Private data members    //    //  fEncodingName    //      This is the encoding name for the code page that this instance    //      represents.    //    //  fCPId    //      This is the Windows specific code page for the encoding that this    //      instance represents.    //    //  fIEId    //      This is the IE encoding id. Its not used at this time, but we    //      go ahead and get it and store it just in case for later.    // -----------------------------------------------------------------------    XMLCh*          fEncodingName;    unsigned int    fCPId;    unsigned int    fIEId;};// ---------------------------------------------------------------------------//  CPMapEntry: Constructors and Destructor// ---------------------------------------------------------------------------CPMapEntry::CPMapEntry( const   char* const     encodingName                        , const unsigned int    cpId                        , const unsigned int    ieId) :    fEncodingName(0)    , fCPId(cpId)    , fIEId(ieId){    // Transcode the name to Unicode and store that copy    const unsigned int srcLen = strlen(encodingName);    const unsigned int targetLen = ::MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, encodingName, srcLen, NULL, 0);    fEncodingName = (XMLCh*) XMLPlatformUtils::fgMemoryManager->allocate    (        (targetLen + 1) * sizeof(XMLCh)    );//new XMLCh[targetLen + 1];    ::MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, encodingName, srcLen, (LPWSTR)fEncodingName, targetLen);    fEncodingName[targetLen] = 0;    //    //  Upper case it because we are using a hash table and need to be    //  sure that we find all case combinations.    //    ::LCMapStringW( gLocaleId, LCMAP_UPPERCASE, (LPCWSTR)fEncodingName, targetLen, (LPWSTR)fEncodingName, targetLen);}CPMapEntry::CPMapEntry( const   XMLCh* const    encodingName                        , const unsigned int    cpId                        , const unsigned int    ieId) :    fEncodingName(0)    , fCPId(cpId)    , fIEId(ieId){		fEncodingName = XMLString::replicate(encodingName, XMLPlatformUtils::fgMemoryManager);    //    //  Upper case it because we are using a hash table and need to be    //  sure that we find all case combinations.    //    unsigned int itsLen = XMLString::stringLen( fEncodingName);    ::LCMapStringW( gLocaleId, LCMAP_UPPERCASE, (LPCWSTR)fEncodingName, itsLen, (LPWSTR)fEncodingName, itsLen);}CPMapEntry::~CPMapEntry(){    XMLPlatformUtils::fgMemoryManager->deallocate(fEncodingName);//delete [] fEncodingName;}// ---------------------------------------------------------------------------//  CPMapEntry: Getter methods// ---------------------------------------------------------------------------const XMLCh* CPMapEntry::getEncodingName() const{    return fEncodingName;}unsigned int CPMapEntry::getWinCP() const{    return fCPId;}unsigned int CPMapEntry::getIEEncoding() const{    return fIEId;}//---------------------------------------------------------------------------////  class CygwinTransService Implementation ...////---------------------------------------------------------------------------// ---------------------------------------------------------------------------//  CygwinTransService: Constructors and Destructor// ---------------------------------------------------------------------------CygwinTransService::CygwinTransService(){    fCPMap = new RefHashTableOf<CPMapEntry>(109);    //    //  Open up the registry key that contains the info we want. Note that,    //  if this key does not exist, then we just return. It will just mean    //  that we don't have any support except for intrinsic encodings supported    //  by the parser itself (and the LCP support of course.    //    HKEY charsetKey;    if (::RegOpenKeyExA    (        HKEY_CLASSES_ROOT        , "MIME\\Database\\Charset"        , 0        , KEY_READ        , &charsetKey))    {        return;    }    //    //  Read in the registry keys that hold the code page ids. Skip for now    //  those entries which indicate that they are aliases for some other    //  encodings. We'll come back and do a second round for those and look    //  up the original name and get the code page id.    //    //  Note that we have to use A versions here so that this will run on    //  98, and transcode the strings to Unicode.    //    const unsigned int nameBufSz = 1024;    char nameBuf[nameBufSz + 1];    unsigned int subIndex = 0;    unsigned long theSize;    while (true)    {        // Get the name of the next key        theSize = nameBufSz;        if (::RegEnumKeyExA        (            charsetKey            , subIndex            , nameBuf            , &theSize            , 0, 0, 0, 0) == ERROR_NO_MORE_ITEMS)        {            break;        }        // Open this subkey        HKEY encodingKey;        if (::RegOpenKeyExA        (            charsetKey            , nameBuf            , 0            , KEY_READ            , &encodingKey))        {            XMLPlatformUtils::panic(PanicHandler::Panic_NoTransService);        }        //        //  Lts see if its an alias. If so, then ignore it in this first        //  loop. Else, we'll add a new entry for this one.        //        if (!isAlias(encodingKey))        {            //            //  Lets get the two values out of this key that we are            //  interested in. There should be a code page entry and an            //  IE entry.            //            unsigned long theType;            unsigned int CPId;            unsigned int IEId;            theSize = sizeof(unsigned int);            if (::RegQueryValueExA            (                encodingKey                , "Codepage"                , 0                , &theType                , (unsigned char*)&CPId                , &theSize) != ERROR_SUCCESS)            {                XMLPlatformUtils::panic(PanicHandler::Panic_NoTransService);            }            //            //  If this is not a valid Id, and it might not be because its            //  not loaded on this system, then don't take it.            //            if (::IsValidCodePage(CPId))            {                theSize = sizeof(unsigned int);                if (::RegQueryValueExA                (                    encodingKey                    , "InternetEncoding"                    , 0                    , &theType                    , (unsigned char*)&IEId                    , &theSize) != ERROR_SUCCESS)                {                    XMLPlatformUtils::panic(PanicHandler::Panic_NoTransService);                }                CPMapEntry* newEntry = new CPMapEntry(nameBuf, CPId, IEId);                fCPMap->put((void*)newEntry->getEncodingName(), newEntry);            }        }        // And now close the subkey handle and bump the subkey index

⌨️ 快捷键说明

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