macosplatformutils.cpp

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

CPP
1,527
字号
/* * Copyright 1999-2000,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: MacOSPlatformUtils.cpp,v 1.28 2004/09/08 13:56:40 peiyongz Exp $ */// ---------------------------------------------------------------------------//  Includes// ---------------------------------------------------------------------------#include <cstring>#include <cstdlib>#include <cctype>#include <cstdio>#include <memory>#include <algorithm>#include <unistd.h>#if defined(__APPLE__)    //	Include from Frameworks Headers under ProjectBuilder    #include <Carbon/Carbon.h>#else    //	Classic include styles    #include <Files.h>    #include <Gestalt.h>    #include <TextUtils.h>    #include <TextEncodingConverter.h>    #include <Multiprocessing.h>    #include <DriverSynchronization.h>    #include <DriverServices.h>    #include <CFString.h>    #include <URLAccess.h>#endif#include <xercesc/util/XercesDefs.hpp>#include <xercesc/util/Janitor.hpp>#include <xercesc/util/PlatformUtils.hpp>#include <xercesc/util/RuntimeException.hpp>#include <xercesc/util/XMLUniDefs.hpp>#include <xercesc/util/XMLUni.hpp>#include <xercesc/util/XMLString.hpp>#include <xercesc/util/Platforms/MacOS/MacOSPlatformUtils.hpp>#include <xercesc/util/Platforms/MacOS/MacCarbonFile.hpp>#include <xercesc/util/Platforms/MacOS/MacPosixFile.hpp>#include <xercesc/util/PanicHandler.hpp>#if (defined(XML_USE_INMEMORY_MSGLOADER) || defined(XML_USE_INMEM_MESSAGELOADER))   #include <xercesc/util/MsgLoaders/InMemory/InMemMsgLoader.hpp>#endif#if (defined(XML_USE_MACOS_UNICODECONVERTER) || defined(XML_USE_NATIVE_TRANSCODER))   #include <xercesc/util/Transcoders/MacOSUnicodeConverter/MacOSUnicodeConverter.hpp>#endif//	Make up our minds about which netaccessor we'll use#if (defined(XML_USE_NETACCESSOR_URLACCESSCF) || (defined(XML_USE_NETACCESSOR_NATIVE) && TARGET_API_MAC_CARBON))    #define USE_URLACCESSCF#elif (defined(XML_USE_NETACCESSOR_URLACCESS) || (defined(XML_USE_NETACCESSOR_NATIVE) && !TARGET_API_MAC_CARBON))    #define USE_URLACCESS#endif#if defined(USE_URLACCESSCF)   #include <xercesc/util/NetAccessors/MacOSURLAccessCF/MacOSURLAccessCF.hpp>#elif defined(USE_URLACCESS)   #include <xercesc/util/NetAccessors/MacOSURLAccess/MacOSURLAccess.hpp>#endifXERCES_CPP_NAMESPACE_BEGIN//----------------------------------------------------------------------------// Function Prototypes//----------------------------------------------------------------------------XMLCh*	ConvertColonToSlash(XMLCh* p, std::size_t charCount);XMLCh*	ConvertSlashToColon(XMLCh* p, std::size_t charCount);char*	ConvertSlashToColon(char* p, std::size_t charCount);XMLCh*	XMLCreateFullPathFromFSRef_X(const FSRef& startingRef, MemoryManager* const manager);XMLCh*	XMLCreateFullPathFromFSRef_Classic(const FSRef& startingRef, MemoryManager* const manager);XMLCh*	XMLCreateFullPathFromFSSpec_Classic(const FSSpec& startingSpec,                                            MemoryManager* const manager);bool	XMLParsePathToFSRef_X(const XMLCh* const pathName, FSRef& ref, MemoryManager* const manager);bool	XMLParsePathToFSRef_Classic(const XMLCh* const pathName, FSRef& ref, MemoryManager* const manager);bool	XMLParsePathToFSSpec_Classic(const XMLCh* const pathName, FSSpec& spec, MemoryManager* const manager);//----------------------------------------------------------------------------//  Local Data////  gFileSystemCompatible//   This flag indicates whether the file system APIs meet our minimum//   requirements.////  gMacOSXOrBetter//   The system version is >= 10.//// gHasFSSpecAPIs//   True if the FSSpecAPIs are available. These are required.//// gHasF2TBAPIs//   True if the FS supports 2 terrabyte calls. These are required for//   use under Carbon.//// gHasHFSPlusAPIs//   True if the FS supports HFSPlus APIs. If this is true, then the//   new Fork calls are used, and all file name and path handling//   uses long unicode names. Note that once a file is opened with//   the fork calls, only fork calls may be used to access it.//// gHasFSPathAPIs//   True if the FS supports path creation APIs FSPathMakeRef and//	 FSRefMakePath.//// gPathAPIsUsePosixPaths//   True if the path creation APIs FSPathMakeRef and FSRefMakePath//	 use posix, rather than HFS, style paths. If so, these routines//	 are used to support path creation and  interpretation.//// gHasMPAPIs//	 True if the Multiprocessing APIs are available.//// gUsePosixFiles//   True if we're using XMLMacPosixFile rather than XMLMacCarbonFile.//// gUseGETCWD//   True if we can rely on getcwd to get the current directory path.//----------------------------------------------------------------------------bool gFileSystemCompatible	= false;bool gMacOSXOrBetter		= false;bool gHasFSSpecAPIs			= false;bool gHasFS2TBAPIs			= false;bool gHasHFSPlusAPIs		= false;bool gHasFSPathAPIs			= false;bool gPathAPIsUsePosixPaths	= false;bool gHasMPAPIs				= false;bool gUsePosixFiles			= false;bool gUseGETCWD				= false;// ---------------------------------------------------------------------------//  XMLPlatformUtils: The panic method// ---------------------------------------------------------------------------void XMLPlatformUtils::panic(const PanicHandler::PanicReasons reason){    if (fgUserPanicHandler)		fgUserPanicHandler->panic(reason);	else		fgDefaultPanicHandler->panic(reason);}// ---------------------------------------------------------------------------//  XMLPlatformUtils: File Methods// ---------------------------------------------------------------------------unsigned intXMLPlatformUtils::curFilePos(const FileHandle theFile                             , MemoryManager* const manager){	return reinterpret_cast<XMLMacAbstractFile*>(theFile)->currPos();}voidXMLPlatformUtils::closeFile(const FileHandle theFile                            , MemoryManager* const manager){    reinterpret_cast<XMLMacAbstractFile*>(theFile)->close();	delete reinterpret_cast<XMLMacAbstractFile*>(theFile);}unsigned intXMLPlatformUtils::fileSize(const FileHandle theFile                           , MemoryManager* const manager){    return reinterpret_cast<XMLMacAbstractFile*>(theFile)->size();}FileHandleXMLPlatformUtils::openFile(const char* const fileName                           , MemoryManager* const manager){    // Check to make sure the file system is in a state where we can use it    if (!gFileSystemCompatible)        ThrowXMLwithMemMgr1(XMLPlatformUtilsException, XMLExcepts::File_CouldNotOpenFile, fileName, manager);    Janitor<XMLMacAbstractFile> file(XMLMakeMacFile());        return (file->open(fileName, false)) ? file.release() : NULL;}FileHandleXMLPlatformUtils::openFile(const XMLCh* const fileName, MemoryManager* const manager){    // Check to make sure the file system is in a state where we can use it    if (!gFileSystemCompatible)        ThrowXMLwithMemMgr1(XMLPlatformUtilsException, XMLExcepts::File_CouldNotOpenFile, fileName, manager);    Janitor<XMLMacAbstractFile> file(XMLMakeMacFile());    return (file->open(fileName, false)) ? file.release() : NULL;}FileHandleXMLPlatformUtils::openFileToWrite(const char* const fileName                                  , MemoryManager* const manager){    // Check to make sure the file system is in a state where we can use it    if (!gFileSystemCompatible)        ThrowXMLwithMemMgr1(XMLPlatformUtilsException, XMLExcepts::File_CouldNotOpenFile, fileName, manager);    Janitor<XMLMacAbstractFile> file(XMLMakeMacFile());    return (file->open(fileName, true)) ? file.release() : NULL;}FileHandleXMLPlatformUtils::openFileToWrite(const XMLCh* const fileName                                  , MemoryManager* const manager){    // Check to make sure the file system is in a state where we can use it    if (!gFileSystemCompatible)        ThrowXMLwithMemMgr1(XMLPlatformUtilsException, XMLExcepts::File_CouldNotOpenFile, fileName, manager);    Janitor<XMLMacAbstractFile> file(XMLMakeMacFile());    return (file->open(fileName, true)) ? file.release() : NULL;}FileHandleXMLPlatformUtils::openStdInHandle(MemoryManager* const manager){    XMLCh stdinStr[] = {chLatin_s, chLatin_t, chLatin_d, chLatin_i, chLatin_n, chNull};    ThrowXMLwithMemMgr1(XMLPlatformUtilsException, XMLExcepts::File_CouldNotOpenFile, stdinStr, manager);    return NULL;}unsigned intXMLPlatformUtils::readFileBuffer(   const FileHandle      theFile                                 ,  const unsigned int    toRead                                 ,        XMLByte* const  toFill                                 ,  MemoryManager* const  manager){    return reinterpret_cast<XMLMacAbstractFile*>(theFile)->read(toRead, toFill);}voidXMLPlatformUtils::writeBufferToFile(   const   FileHandle   theFile                                    ,  const long		    toWrite                                    ,  const XMLByte* const toFlush                                    ,  MemoryManager* const manager){    return reinterpret_cast<XMLMacAbstractFile*>(theFile)->write(toWrite, toFlush);}voidXMLPlatformUtils::resetFile(FileHandle theFile                            , MemoryManager* const manager){    reinterpret_cast<XMLMacAbstractFile*>(theFile)->reset();}// ---------------------------------------------------------------------------//  XMLPlatformUtils: File system methods// ---------------------------------------------------------------------------XMLCh*XMLPlatformUtils::getFullPath(const XMLCh* const srcPath,                              MemoryManager* const manager){    XMLCh* path = NULL;    if (gHasHFSPlusAPIs)    {        FSRef ref;        if (   !XMLParsePathToFSRef(srcPath, ref, manager)			|| (path = XMLCreateFullPathFromFSRef(ref, manager)) == NULL		   )            path = XMLString::replicate(srcPath, manager);    }    else    {        FSSpec spec;        if (   !XMLParsePathToFSSpec(srcPath, spec, manager)		    || (path = XMLCreateFullPathFromFSSpec(spec, manager)) == NULL		   )            path = XMLString::replicate(srcPath, manager);    }    return path;}boolXMLPlatformUtils::isRelative(const XMLCh* const toCheck                             , MemoryManager* const manager){    return (toCheck[0] != L'/');}XMLCh* XMLPlatformUtils::getCurrentDirectory(MemoryManager* const manager){	//	Get a newly allocated path to the current directory	FSSpec spec;	//  Parse to path to determine current directory: this allows the	//  path parsing routines to determine best way to find the current	//  directory.	XMLCh curDirPath[] = { '.', 0 };	XMLCh* path =		(XMLParsePathToFSSpec(curDirPath, spec, manager))			? XMLCreateFullPathFromFSSpec(spec, manager)			: NULL;			    if (!path)		 ThrowXMLwithMemMgr(XMLPlatformUtilsException,	           XMLExcepts::File_CouldNotGetBasePathName, manager);	           	return path;}inline bool XMLPlatformUtils::isAnySlash(XMLCh c) {	//	We support only forward slash as a path delimiter    return (chForwardSlash == c);}// ---------------------------------------------------------------------------//  XMLPlatformUtils: Timing Methods// ---------------------------------------------------------------------------unsigned longXMLPlatformUtils::getCurrentMillis(){	if ((void*)kUnresolvedCFragSymbolAddress != UpTime)	{		// Use Driver services routines, now in Carbon,		// to get the elapsed milliseconds.		AbsoluteTime time = UpTime();		return AbsoluteToDuration(time);	}	else		return TickCount() * 100 / 6;}// ---------------------------------------------------------------------------//  Mutex methods//// There are a number of choices for multi-threading on Mac OS. Traditionally// there was the Thread Manager, which provided cooperative multitasking on// 68K and PPC platforms, and preemptive multitasking on 68K platforms only.// The primary threading model supported under Carbon is the Multiprocessing// library, which as of version 2.0 provides a nice set of primitives. Under// Mac OS X, the Multiprocessing library is a thin veneer over pthreads.//// For lack of any really universal solutions, I've implemented these mutexes// atop the Multiprocessing library. The critical regions employed here// support recursive behavior, which is required by Xerces.//// Please note that, despite this implementation, there may be other barriers

⌨️ 快捷键说明

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