win32platformutils.cpp

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

CPP
853
字号
        return 0;    return retVal;}FileHandle XMLPlatformUtils::openStdInHandle(MemoryManager* const manager){    //    //  Get the standard input handle. Duplicate it and return that copy    //  since the outside world cannot tell the difference and will shut    //  down this handle when its done with it. If we gave out the orignal,    //  shutting it would prevent any further output.    //    HANDLE stdInOrg = ::GetStdHandle(STD_INPUT_HANDLE);    if (stdInOrg == INVALID_HANDLE_VALUE) {        XMLCh stdinStr[] = {chLatin_s, chLatin_t, chLatin_d, chLatin_i, chLatin_n, chNull};        ThrowXMLwithMemMgr1(XMLPlatformUtilsException, XMLExcepts::File_CouldNotOpenFile, stdinStr, manager);    }    HANDLE retHandle;    if (!::DuplicateHandle    (        ::GetCurrentProcess()        , stdInOrg        , ::GetCurrentProcess()        , &retHandle        , 0        , FALSE        , DUPLICATE_SAME_ACCESS))    {        ThrowXMLwithMemMgr(XMLPlatformUtilsException, XMLExcepts::File_CouldNotDupHandle, manager);    }    return retHandle;}unsigned intXMLPlatformUtils::readFileBuffer(       FileHandle      theFile                                , const unsigned int    toRead                                ,       XMLByte* const  toFill                                , MemoryManager* const  manager){    unsigned long bytesRead = 0;    if (!::ReadFile(theFile, toFill, toRead, &bytesRead, 0))    {        //        //  Check specially for a broken pipe error. If we get this, it just        //  means no more data from the pipe, so return zero.        //        if (::GetLastError() != ERROR_BROKEN_PIPE)            ThrowXMLwithMemMgr(XMLPlatformUtilsException, XMLExcepts::File_CouldNotReadFromFile, manager);    }    return (unsigned int)bytesRead;}voidXMLPlatformUtils::writeBufferToFile( FileHandle     const  theFile                                   , long                  toWrite                                   , const XMLByte* const  toFlush                                   , MemoryManager* const  manager){    if (!theFile        ||        (toWrite <= 0 ) ||        !toFlush         )        return;    const XMLByte* tmpFlush = (const XMLByte*) toFlush;    unsigned long  bytesWritten = 0;    while (true)    {        if (!::WriteFile(theFile, tmpFlush, toWrite, &bytesWritten, 0))            ThrowXMLwithMemMgr(XMLPlatformUtilsException, XMLExcepts::File_CouldNotWriteToFile, manager);        if (bytesWritten < (unsigned long) toWrite) //incomplete write        {            tmpFlush+=bytesWritten;            toWrite-=bytesWritten;            bytesWritten=0;        }        else            return;    }    return;}void XMLPlatformUtils::resetFile(FileHandle theFile                                 , MemoryManager* const manager){    // Seek to the start of the file    if (::SetFilePointer(theFile, 0, 0, FILE_BEGIN) == 0xFFFFFFFF)        ThrowXMLwithMemMgr(XMLPlatformUtilsException, XMLExcepts::File_CouldNotResetFile, manager);}// ---------------------------------------------------------------------------//  XMLPlatformUtils: File system methods// ---------------------------------------------------------------------------XMLCh* XMLPlatformUtils::getFullPath(const XMLCh* const srcPath,                                     MemoryManager* const manager){    //    //  If we are on NT, then use wide character APIs, else use ASCII APIs.    //  We have to do it manually since we are only built in ASCII mode from    //  the standpoint of the APIs.    //    if (gOnNT)    {        // Use a local buffer that is big enough for the largest legal path        const unsigned int bufSize = 1024;        XMLCh tmpPath[bufSize + 1];        XMLCh* namePart = 0;        if (!::GetFullPathNameW((LPCWSTR)srcPath, bufSize, (LPWSTR)tmpPath, (LPWSTR*)&namePart))            return 0;        // Return a copy of the path        return XMLString::replicate(tmpPath, manager);    }     else    {        // Transcode the incoming string        char* tmpSrcPath = XMLString::transcode(srcPath, fgMemoryManager);        ArrayJanitor<char> janSrcPath(tmpSrcPath, fgMemoryManager);        // Use a local buffer that is big enough for the largest legal path        const unsigned int bufSize = 511;        char tmpPath[511 + 1];        char* namePart = 0;        if (!::GetFullPathNameA(tmpSrcPath, bufSize, tmpPath, &namePart))            return 0;        // Return a transcoded copy of the path        return XMLString::transcode(tmpPath, manager);    }}bool XMLPlatformUtils::isRelative(const XMLCh* const toCheck                                  , MemoryManager* const manager){    // Check for pathological case of empty path    if (!toCheck[0])        return false;    //    //  If its starts with a drive, then it cannot be relative. Note that    //  we checked the drive not being empty above, so worst case its one    //  char long and the check of the 1st char will fail because its really    //  a null character.    //    if (toCheck[1] == chColon)    {        if (((toCheck[0] >= chLatin_A) && (toCheck[0] <= chLatin_Z))        ||  ((toCheck[0] >= chLatin_a) && (toCheck[0] <= chLatin_z)))        {            return false;        }    }    //    //  If it starts with a double slash, then it cannot be relative since    //  it's a remote file.    //    if (isBackSlash(toCheck[0]) && isBackSlash(toCheck[1]))        return false;    // Else assume its a relative path    return true;}XMLCh* XMLPlatformUtils::getCurrentDirectory(MemoryManager* const manager){    //    //  If we are on NT, then use wide character APIs, else use ASCII APIs.    //  We have to do it manually since we are only built in ASCII mode from    //  the standpoint of the APIs.    //    if (gOnNT)    {        // Use a local buffer that is big enough for the largest legal path        const unsigned int bufSize = 1024;        XMLCh tmpPath[bufSize + 1];        if (!::GetCurrentDirectoryW(bufSize, (LPWSTR)tmpPath))            return 0;        // Return a copy of the path        return XMLString::replicate(tmpPath, manager);    }     else    {        // Use a local buffer that is big enough for the largest legal path        const unsigned int bufSize = 511;        char tmpPath[511 + 1];        if (!::GetCurrentDirectoryA(bufSize, tmpPath))            return 0;        // Return a transcoded copy of the path        return XMLString::transcode(tmpPath, manager);    }}inline bool XMLPlatformUtils::isAnySlash(XMLCh c){    return c == chBackSlash    ||           c == chForwardSlash ||           c == chYenSign      ||           c == chWonSign;}// ---------------------------------------------------------------------------//  XMLPlatformUtils: Timing Methods// ---------------------------------------------------------------------------unsigned long XMLPlatformUtils::getCurrentMillis(){    return (unsigned long)::GetTickCount();}// ---------------------------------------------------------------------------//  Mutex methods// ---------------------------------------------------------------------------void XMLPlatformUtils::closeMutex(void* const mtxHandle){    ::DeleteCriticalSection((LPCRITICAL_SECTION)mtxHandle);    delete (CRITICAL_SECTION*)mtxHandle;}void XMLPlatformUtils::lockMutex(void* const mtxHandle){    ::EnterCriticalSection((LPCRITICAL_SECTION)mtxHandle);}void* XMLPlatformUtils::makeMutex(){    CRITICAL_SECTION* newCS = new CRITICAL_SECTION;    InitializeCriticalSection(newCS);    return newCS;}void XMLPlatformUtils::unlockMutex(void* const mtxHandle){    ::LeaveCriticalSection((LPCRITICAL_SECTION)mtxHandle);}// ---------------------------------------------------------------------------//  Miscellaneous synchronization methods// ---------------------------------------------------------------------------void*XMLPlatformUtils::compareAndSwap(       void**      toFill                                , const void* const newValue                                , const void* const toCompare){#if defined WIN64    return ::InterlockedCompareExchangePointer(toFill, (void*)newValue, (void*)toCompare);#else    //    //  InterlockedCompareExchange is only supported on Windows 98,    //  Windows NT 4.0, and newer -- not on Windows 95...    //  If you are willing to give up Win95 support change this to #if 0    //  otherwise we are back to using assembler.    //  (But only if building with compilers that support inline assembler.)    //    #if (defined(_MSC_VER) || defined(__BCPLUSPLUS__)) && !defined(XERCES_NO_ASM)    void*   result;    __asm    {        mov             eax, toCompare;        mov             ebx, newValue;        mov             ecx, toFill        lock cmpxchg    [ecx], ebx;        mov             result, eax;    }    return result;    #else    //    //  Note we have to cast off the constness of some of these because    //  the system APIs are not C++ aware in all cases.    //    return (void*) ::InterlockedCompareExchange((LPLONG)toFill, (LONG)newValue, (LONG)toCompare);    #endif#endif}// ---------------------------------------------------------------------------//  Atomic increment and decrement methods// ---------------------------------------------------------------------------int XMLPlatformUtils::atomicIncrement(int &location){    return ::InterlockedIncrement(&(long &)location);}int XMLPlatformUtils::atomicDecrement(int &location){    return ::InterlockedDecrement(&(long &)location);}// ---------------------------------------------------------------------------//  XMLPlatformUtils: Private Static Methods// ---------------------------------------------------------------------------////  This method is called by the platform independent part of this class//  during initialization. We have to create the type of net accessor that//  we want to use. If none, then just return zero.//XMLNetAccessor* XMLPlatformUtils::makeNetAccessor(){#if defined (XML_USE_NETACCESSOR_LIBWWW)    return new LibWWWNetAccessor();#elif defined (XML_USE_NETACCESSOR_WINSOCK)    return new WinSockNetAccessor();#else    return 0;#endif}////  This method is called by the platform independent part of this class//  when client code asks to have one of the supported message sets loaded.//  In our case, we use the ICU based message loader mechanism.//XMLMsgLoader* XMLPlatformUtils::loadAMsgSet(const XMLCh* const msgDomain){#if defined (XML_USE_INMEM_MESSAGELOADER)    return new InMemMsgLoader(msgDomain);#elif defined (XML_USE_WIN32_MSGLOADER)    return new Win32MsgLoader(msgDomain);#elif defined (XML_USE_ICU_MESSAGELOADER)    return new ICUMsgLoader(msgDomain);#else    #error You must provide a message loader#endif}////  This method is called very early in the bootstrapping process. This guy//  must create a transcoding service and return it. It cannot use any string//  methods, any transcoding services, throw any exceptions, etc... It just//  makes a transcoding service and returns it, or returns zero on failure.//XMLTransService* XMLPlatformUtils::makeTransService(){    //    //  Since we are going to use the ICU service, we have to tell it where    //  its converter files are. If the ICU_DATA environment variable is set,    //  then its been told. Otherwise, we tell it our default value relative    //  to our DLL.    //#if defined (XML_USE_ICU_TRANSCODER)    return new ICUTransService;#elif defined (XML_USE_WIN32_TRANSCODER)    return new Win32TransService;#elif defined (XML_USE_CYGWIN_TRANSCODER)    return new CygwinTransService;#else    #error You must provide a transcoding service implementation#endif}////  This method handles the Win32 per-platform basic init functions. The//  primary jobs here are getting the path to our DLL and to get the//  stdout and stderr file handles setup.//void XMLPlatformUtils::platformInit(){#if 0 && defined(_DEBUG)    //  Enable this code for memeory leak testing   // Send all reports to STDOUT   _CrtSetReportMode( _CRT_WARN, _CRTDBG_MODE_FILE );   _CrtSetReportFile( _CRT_WARN, _CRTDBG_FILE_STDOUT );   _CrtSetReportMode( _CRT_ERROR, _CRTDBG_MODE_FILE );   _CrtSetReportFile( _CRT_ERROR, _CRTDBG_FILE_STDOUT );   _CrtSetReportMode( _CRT_ASSERT, _CRTDBG_MODE_FILE );   _CrtSetReportFile( _CRT_ASSERT, _CRTDBG_FILE_STDOUT );    int tmpDbgFlag;    tmpDbgFlag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);    tmpDbgFlag |= _CRTDBG_LEAK_CHECK_DF;    _CrtSetDbgFlag(tmpDbgFlag);#endif    // Figure out if we are on NT and save that flag for later use    OSVERSIONINFO   OSVer;    OSVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);    ::GetVersionEx(&OSVer);    gOnNT = (OSVer.dwPlatformId == VER_PLATFORM_WIN32_NT);}void XMLPlatformUtils::platformTerm(){    // We don't have any temrination requirements for win32 at this time}#include <xercesc/util/LogicalPath.c>XERCES_CPP_NAMESPACE_END

⌨️ 快捷键说明

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