win32platformutils.cpp

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

CPP
853
字号
/* * Copyright 1999-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. *//* * $Id: Win32PlatformUtils.cpp,v 1.25 2004/09/08 13:56:43 peiyongz Exp $ */// ---------------------------------------------------------------------------//  Includes// ---------------------------------------------------------------------------#include <xercesc/util/Janitor.hpp>#include <xercesc/util/PlatformUtils.hpp>#include <xercesc/util/RuntimeException.hpp>#include <xercesc/util/XMLExceptMsgs.hpp>#include <xercesc/util/XMLString.hpp>#include <xercesc/util/XMLUniDefs.hpp>#include <xercesc/util/XMLUni.hpp>#include <xercesc/util/PanicHandler.hpp>#include <windows.h>#include <stdio.h>#include <stdlib.h>#ifdef _DEBUG #ifdef _MSC_VER    #include <crtdbg.h> #else    #include <assert.h> #endif#endif////  These control which transcoding service is used by the Win32 version.//  They allow this to be controlled from the build process by just defining//  one of these values.//#if defined (XML_USE_ICU_TRANSCODER)    #include <xercesc/util/Transcoders/ICU/ICUTransService.hpp>#elif defined (XML_USE_WIN32_TRANSCODER)    #include <xercesc/util/Transcoders/Win32/Win32TransService.hpp>#elif defined (XML_USE_CYGWIN_TRANSCODER)    #include <xercesc/util/Transcoders/Cygwin/CygwinTransService.hpp>#else    #error A transcoding service must be chosen#endif////  These control which message loading service is used by the Win32 version.//  They allow this to be controlled from the build process by just defining//  one of these values.//#if defined (XML_USE_INMEM_MESSAGELOADER)    #include <xercesc/util/MsgLoaders/InMemory/InMemMsgLoader.hpp>#elif defined (XML_USE_WIN32_MSGLOADER)    #include <xercesc/util/MsgLoaders/Win32/Win32MsgLoader.hpp>#elif defined(XML_USE_ICU_MESSAGELOADER)    #include <xercesc/util/MsgLoaders/ICU/ICUMsgLoader.hpp>#else    #error A message loading service must be chosen#endif////  These control which network access service is used by the Win32 version.//  They allow this to be controlled from the build process by just defining//  one of these values.//#if defined (XML_USE_NETACCESSOR_LIBWWW)    #include <xercesc/util/NetAccessors/libWWW/LibWWWNetAccessor.hpp>#elif defined (XML_USE_NETACCESSOR_WINSOCK)    #include <xercesc/util/NetAccessors/WinSock/WinSockNetAccessor.hpp>#endifXERCES_CPP_NAMESPACE_BEGIN// ---------------------------------------------------------------------------//  Local data////  gOnNT//      We figure out during init if we are on NT or not. If we are, then//      we can avoid a lot of transcoding in our system services stuff.// ---------------------------------------------------------------------------static bool     gOnNT;// ---------------------------------------------------------------------------//  XMLPlatformUtils: The panic method// ---------------------------------------------------------------------------void XMLPlatformUtils::panic(const PanicHandler::PanicReasons reason){    fgUserPanicHandler? fgUserPanicHandler->panic(reason) : fgDefaultPanicHandler->panic(reason);	}// ---------------------------------------------------------------------------//  XMLPlatformUtils: File Methods// ---------------------------------------------------------------------------////  Functions to look for Unicode forward and back slashes.//  This operation is complicated by the fact that some Japanese and Korean//    encodings use the same encoding for both '\' and their currency symbol//    (Yen or Won).  In these encodings, which is meant is context dependent.//    Unicode converters choose the currency symbols.  But in the context//    of a Windows file name, '\' is generally what was intended.////    So we make a leap of faith, and assume that if we get a Yen or Won//    here, in the context of a file name, that it originated in one of//    these encodings, and is really supposed to be a '\'.//static bool isBackSlash(XMLCh c) {    return c == chBackSlash ||           c == chYenSign   ||           c == chWonSign;}unsigned int XMLPlatformUtils::curFilePos(FileHandle theFile                                          , MemoryManager* const manager){    // Get the current position    const unsigned int curPos = ::SetFilePointer(theFile, 0, 0, FILE_CURRENT);    if (curPos == 0xFFFFFFFF)        ThrowXMLwithMemMgr(XMLPlatformUtilsException, XMLExcepts::File_CouldNotGetCurPos, manager);    return curPos;}void XMLPlatformUtils::closeFile(FileHandle theFile                                 , MemoryManager* const manager){    if (!::CloseHandle(theFile))        ThrowXMLwithMemMgr(XMLPlatformUtilsException, XMLExcepts::File_CouldNotCloseFile, manager);}unsigned int XMLPlatformUtils::fileSize(FileHandle theFile                                        , MemoryManager* const manager){    // Get the current position    const unsigned int curPos = ::SetFilePointer(theFile, 0, 0, FILE_CURRENT);    if (curPos == 0xFFFFFFFF)        ThrowXMLwithMemMgr(XMLPlatformUtilsException, XMLExcepts::File_CouldNotGetCurPos, manager);    // Seek to the end and save that value for return    const unsigned int retVal = ::SetFilePointer(theFile, 0, 0, FILE_END);    if (retVal == 0xFFFFFFFF)        ThrowXMLwithMemMgr(XMLPlatformUtilsException, XMLExcepts::File_CouldNotSeekToEnd, manager);    // And put the pointer back    if (::SetFilePointer(theFile, curPos, 0, FILE_BEGIN) == 0xFFFFFFFF)        ThrowXMLwithMemMgr(XMLPlatformUtilsException, XMLExcepts::File_CouldNotSeekToPos, manager);    return retVal;}FileHandle XMLPlatformUtils::openFile(const char* const fileName                                      , MemoryManager* const manager){    FileHandle retVal = ::CreateFileA    (        fileName        , GENERIC_READ        , FILE_SHARE_READ        , 0        , OPEN_EXISTING        , FILE_FLAG_SEQUENTIAL_SCAN        , 0    );    if (retVal == INVALID_HANDLE_VALUE)        return 0;    return retVal;}FileHandle XMLPlatformUtils::openFile(const XMLCh* const fileName                                      , MemoryManager* const manager){    // Watch for obvious wierdness    if (!fileName)        return 0;    //    //  We have to play a little trick here. If its /x:.....    //  style fully qualified path, we have to toss the leading /    //  character.    //    const XMLCh* nameToOpen = fileName;    if (*fileName == chForwardSlash)    {        if (XMLString::stringLen(fileName) > 3)        {            if (*(fileName + 2) == chColon)            {                const XMLCh chDrive = *(fileName + 1);                if (((chDrive >= chLatin_A) && (chDrive <= chLatin_Z))                ||  ((chDrive >= chLatin_a) && (chDrive <= chLatin_z)))                {                    nameToOpen = fileName + 1;                }            }            // Similarly for UNC paths            if ( *(fileName + 1) == *(fileName + 2) &&                 (*(fileName + 1) == chForwardSlash ||                  *(fileName + 1) == chBackSlash) )            {                nameToOpen = fileName + 1;            }        }    }    //  Ok, this might look stupid but its a semi-expedient way to deal    //  with a thorny problem. Shift-JIS and some other Asian encodings    //  are fundamentally broken and map both the backslash and the Yen    //  sign to the same code point. Transcoders have to pick one or the    //  other to map '\' to Unicode and tend to choose the Yen sign.    //    //  Unicode Yen or Won signs as directory separators will fail.    //    //  So, we will check this path name for Yen or won signs and, if they are    //  there, we'll replace them with slashes.    //    //  A further twist:  we replace Yen and Won with forward slashes rather    //   than back slashes.  Either form of slash will work as a directory    //   separator.  On Win 95 and 98, though, Unicode back-slashes may    //   fail to transode back to 8-bit 0x5C with some Unicode converters    //   to  some of the problematic code pages.  Forward slashes always    //   transcode correctly back to 8 bit char * form.    //    XMLCh *tmpUName = 0;    const XMLCh* srcPtr = nameToOpen;    while (*srcPtr)    {        if (*srcPtr == chYenSign ||            *srcPtr == chWonSign)            break;        srcPtr++;    }    //    //  If we found a yen, then we have to create a temp file name. Else    //  go with the file name as is and save the overhead.    //    if (*srcPtr)    {        tmpUName = XMLString::replicate(nameToOpen, manager);        XMLCh* tmpPtr = tmpUName;        while (*tmpPtr)        {            if (*tmpPtr == chYenSign ||                *tmpPtr == chWonSign)                *tmpPtr = chForwardSlash;            tmpPtr++;        }        nameToOpen = tmpUName;    }    FileHandle retVal = 0;    if (gOnNT)    {        retVal = ::CreateFileW            (            (LPCWSTR) nameToOpen            , GENERIC_READ            , FILE_SHARE_READ            , 0            , OPEN_EXISTING            , FILE_FLAG_SEQUENTIAL_SCAN            , 0            );    }    else    {        //        //  We are Win 95 / 98.  Take the Unicode file name back to (char *)        //    so that we can open it.        //        char* tmpName = XMLString::transcode(nameToOpen, manager);        retVal = ::CreateFileA            (            tmpName            , GENERIC_READ            , FILE_SHARE_READ            , 0            , OPEN_EXISTING            , FILE_FLAG_SEQUENTIAL_SCAN            , 0            );        manager->deallocate(tmpName);//delete [] tmpName;    }    if (tmpUName)        manager->deallocate(tmpUName);//delete [] tmpUName;    if (retVal == INVALID_HANDLE_VALUE)        return 0;    return retVal;}FileHandle XMLPlatformUtils::openFileToWrite(const char* const fileName                                             , MemoryManager* const manager){    FileHandle retVal = ::CreateFileA    (        fileName        , GENERIC_WRITE        , 0              // no shared write        , 0        , CREATE_ALWAYS        , FILE_ATTRIBUTE_NORMAL        , 0    );    if (retVal == INVALID_HANDLE_VALUE)        return 0;    return retVal;}FileHandle XMLPlatformUtils::openFileToWrite(const XMLCh* const fileName                                             , MemoryManager* const manager){    // Watch for obvious wierdness    if (!fileName)        return 0;    //  Ok, this might look stupid but its a semi-expedient way to deal    //  with a thorny problem. Shift-JIS and some other Asian encodings    //  are fundamentally broken and map both the backslash and the Yen    //  sign to the same code point. Transcoders have to pick one or the    //  other to map '\' to Unicode and tend to choose the Yen sign.    //    //  Unicode Yen or Won signs as directory separators will fail.    //    //  So, we will check this path name for Yen or won signs and, if they are    //  there, we'll replace them with slashes.    //    //  A further twist:  we replace Yen and Won with forward slashes rather    //   than back slashes.  Either form of slash will work as a directory    //   separator.  On Win 95 and 98, though, Unicode back-slashes may    //   fail to transode back to 8-bit 0x5C with some Unicode converters    //   to  some of the problematic code pages.  Forward slashes always    //   transcode correctly back to 8 bit char * form.    //    XMLCh *tmpUName = 0;    const XMLCh *nameToOpen = fileName;    const XMLCh* srcPtr = fileName;    while (*srcPtr)    {        if (*srcPtr == chYenSign ||            *srcPtr == chWonSign)            break;        srcPtr++;    }    //    //  If we found a yen, then we have to create a temp file name. Else    //  go with the file name as is and save the overhead.    //    if (*srcPtr)    {        tmpUName = XMLString::replicate(fileName, manager);        XMLCh* tmpPtr = tmpUName;        while (*tmpPtr)        {            if (*tmpPtr == chYenSign ||                *tmpPtr == chWonSign)                *tmpPtr = chForwardSlash;            tmpPtr++;        }        nameToOpen = tmpUName;    }    FileHandle retVal = 0;    if (gOnNT)    {        retVal = ::CreateFileW            (            (LPCWSTR) nameToOpen            , GENERIC_WRITE            , 0              // no shared write            , 0            , CREATE_ALWAYS            , FILE_ATTRIBUTE_NORMAL            , 0            );    }    else    {        //        //  We are Win 95 / 98.  Take the Unicode file name back to (char *)        //    so that we can open it.        //        char* tmpName = XMLString::transcode(nameToOpen, manager);        retVal = ::CreateFileA            (            tmpName            , GENERIC_WRITE            , 0              // no shared write            , 0            , CREATE_ALWAYS            , FILE_ATTRIBUTE_NORMAL            , 0            );        manager->deallocate(tmpName);//delete [] tmpName;    }    if (tmpUName)        manager->deallocate(tmpUName);//delete [] tmpUName;    if (retVal == INVALID_HANDLE_VALUE)

⌨️ 快捷键说明

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