macposixfile.cpp

来自「IBM的解析xml的工具Xerces的源代码」· C++ 代码 · 共 185 行

CPP
185
字号
/* * 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: MacPosixFile.cpp,v 1.3 2004/09/08 13:56:40 peiyongz Exp $ */#include <xercesc/util/XercesDefs.hpp>#include <xercesc/util/XMLString.hpp>#include <xercesc/util/Janitor.hpp>#include <limits.h>#include <stdio.h>#include <xercesc/util/Platforms/MacOS/MacPosixFile.hpp>XERCES_CPP_NAMESPACE_BEGIN//----------------------------------------------------------------------------// XMLMacPosixFile methods//----------------------------------------------------------------------------XMLMacPosixFile::XMLMacPosixFile()  : mFile(NULL){}XMLMacPosixFile::~XMLMacPosixFile(){	if (mFile)		close();}unsigned intXMLMacPosixFile::currPos(){    if (!mFile)		ThrowXML(XMLPlatformUtilsException, XMLExcepts::CPtr_PointerIsZero);		     long curPos = ftell(mFile);	    if (curPos == -1)        ThrowXML(XMLPlatformUtilsException, XMLExcepts::File_CouldNotGetSize);    return (unsigned int)curPos;}voidXMLMacPosixFile::close(){    if (!mFile)		ThrowXML(XMLPlatformUtilsException, XMLExcepts::CPtr_PointerIsZero);		    if (fclose(mFile))        ThrowXML(XMLPlatformUtilsException, XMLExcepts::File_CouldNotCloseFile);			mFile = NULL;}unsigned intXMLMacPosixFile::size(){    if (mFile == NULL)		ThrowXML(XMLPlatformUtilsException, XMLExcepts::CPtr_PointerIsZero);		    // Get the current position    long curPos = ftell(mFile);    if (curPos == -1)        ThrowXML(XMLPlatformUtilsException, XMLExcepts::File_CouldNotGetCurPos);    // Seek to the end and save that value for return    if (fseek(mFile, 0, SEEK_END))        ThrowXML(XMLPlatformUtilsException, XMLExcepts::File_CouldNotSeekToEnd);    long retVal = ftell(mFile);    if (retVal == -1)        ThrowXML(XMLPlatformUtilsException, XMLExcepts::File_CouldNotSeekToEnd);    // And put the pointer back    if (fseek(mFile, curPos, SEEK_SET))        ThrowXML(XMLPlatformUtilsException, XMLExcepts::File_CouldNotSeekToPos);    return (unsigned int)retVal;}boolXMLMacPosixFile::open(const XMLCh* const path, bool toWrite){    if (!path)		ThrowXML(XMLPlatformUtilsException, XMLExcepts::CPtr_PointerIsZero);	//	Transcode the unicode path to UTF8, which is what the Mac posix routines want	char tmpPath[kMaxMacStaticPathChars];	std::size_t len = TranscodeUniCharsToUTF8(path, tmpPath, XMLString::stringLen(path), kMaxMacStaticPathChars-1);	tmpPath[len] = 0;		//	Call through to the char version to do the work	return open(tmpPath, toWrite);}boolXMLMacPosixFile::open(const char* const path, bool toWrite){    if (!path)		ThrowXML(XMLPlatformUtilsException, XMLExcepts::CPtr_PointerIsZero);	const char* perms = (toWrite) ? "w" : "r";	mFile = fopen(path, perms);		return (mFile != NULL);}unsigned intXMLMacPosixFile::read(const unsigned int byteCount, XMLByte* const buffer){    if (!mFile || !buffer)		ThrowXML(XMLPlatformUtilsException, XMLExcepts::CPtr_PointerIsZero);		    size_t bytesRead = 0;	if (byteCount > 0)	{    	bytesRead = fread((void*)buffer, 1, byteCount, mFile);		if (ferror(mFile))			ThrowXML(XMLPlatformUtilsException, XMLExcepts::File_CouldNotReadFromFile);	}	    return (unsigned int)bytesRead;}voidXMLMacPosixFile::write(long byteCount, const XMLByte* buffer){    if (!mFile || !buffer)		ThrowXML(XMLPlatformUtilsException, XMLExcepts::CPtr_PointerIsZero);    while (byteCount > 0)    {        size_t bytesWritten = fwrite(buffer, sizeof(XMLByte), byteCount, mFile);        if (ferror(mFile))			ThrowXML(XMLPlatformUtilsException, XMLExcepts::File_CouldNotWriteToFile);		buffer		+= bytesWritten;		byteCount	-= bytesWritten;    }}voidXMLMacPosixFile::reset(){    if (!mFile)		ThrowXML(XMLPlatformUtilsException, XMLExcepts::CPtr_PointerIsZero);		    // Seek to the start of the file    if (fseek(mFile, 0, SEEK_SET))        ThrowXML(XMLPlatformUtilsException, XMLExcepts::File_CouldNotResetFile);}XERCES_CPP_NAMESPACE_END

⌨️ 快捷键说明

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