cmacfile_carbon.cpp

来自「symbian 下的helix player源代码」· C++ 代码 · 共 937 行 · 第 1/2 页

CPP
937
字号
				break;

			case SEEK_CUR:
				pos = mWriteFile->GetCurPos() + offset;
				break;

			case SEEK_END:
				pos = (mWriteFile->GetBufSize() - 1) - offset;
				break;
		}
		mWriteFile->Seek(pos);
	}
	else
	{
		switch(fromWhere)
		{
			case SEEK_SET:
				fromWhere = fsFromStart;
				break;

			case SEEK_CUR:
				fromWhere = fsFromMark;
				break;

			case SEEK_END:
				fromWhere = fsFromLEOF;
				break;
		}
		
		err =  ::SetFPos(mRefNum, fromWhere, offset);

		// returning eofErr was causing problems for ChunkyRes during http play
		if (err == eofErr)
		{
			err = ::SetEOF(mRefNum, offset);
			err = ::SetFPos(mRefNum, fromWhere, offset);
			err = HXR_OK;
		}
			
		long pos;
		::GetFPos(mRefNum,(long *)&pos);

		if(!err && mBufferedRead)
		{
			long count; 
			::GetEOF(mRefNum, &count);
			mBufferFile->PreLoad(pos,count - offset);
		}

		/* if(!theErr && mBufferedWrite)
		{
			mWriteFile->Seek(pos);
		} */
	}
	mLastError = err;
	return(err != noErr ? HXR_INVALID_FILE : 0);	
}

// Tell 
ULONG32 CMacFile::Tell(void)
{
	ULONG32	pos;

	if (m_pseudoFileHandle)
	{
		pos = m_pseudoFileOffset;
		mLastError = noErr;
	}
	else
	{
		mLastError = ::GetFPos(mRefNum,(long *)&pos);
	}
	return(pos);	
}

// callback could be at interrupt time
pascal void ReadCallback(ParamBlockRec* pb)
{
    OSErr theErr = (*pb).ioParam.ioResult;    	 
    gReadCount = pb->ioParam.ioActCount;
    gReadDone = TRUE;
}

/* 	Read reads up to count bytes of data into buf.
	returns the number of bytes read, EOF, or -1 if the read failed */
ULONG32 CMacFile::Read (char *buf, ULONG32 count)
{
	OSErr theErr = noErr;

	if (m_pseudoFileHandle)
	{
		INT32	actualCount;
		
		theErr = Read(buf, count, &actualCount);
		
		count = actualCount;
	}
	else
	{
		if(mBufferedRead)
		{
			count = Copy(buf,count);
			if(count == 0)
				theErr = eofErr;
		}
		else
		{
	   	    // can't do synchronous read at interrupt time
	   	    if (IsRunningNativeOnMacOSX() || IsMacInCooperativeThread())
	   	    {
			theErr = ::FSRead(mRefNum, (long *)&count, buf);
		    }
		    else
		    {	// async i/o - callback could be at interrupt time
	#if 1
		    	ParamBlockRec pb;
			
		    	gReadCount = 0;
	    		gReadDone = FALSE;
			pb.ioParam.ioRefNum = mRefNum;
			pb.ioParam.ioBuffer = buf;
			pb.ioParam.ioReqCount = count;
			pb.ioParam.ioPosMode=fsAtMark;
			pb.ioParam.ioCompletion = gReadCallbackUPP;
			theErr = PBReadAsync(&pb);
			
			UINT32 timeout = TickCount() + 60L;
			while (!gReadDone && timeout-TickCount() > 0)
			{
			}
				
			count = gReadCount;
	#endif
		    }
		}
		if (theErr == eofErr && count > 0)
		{
			theErr = noErr;
		}
	}
	mLastError = theErr;
		
	return(mLastError ? 0 : count); //smplfsys::read assumes we return 0 for eof
}

/* 	Read reads up to count bytes of data into buf.
	returns the number of bytes read, EOF, or -1 if the read failed */
INT16 CMacFile::Read (char *buf, INT32 count, INT32 *actualCount)
{
OSErr theErr = noErr;

	long readCount = (long) count;
	
	if (m_pseudoFileHandle)
	{
		// read from our pseudo-file
		SInt32 remainingBytes;
		
		remainingBytes = m_pseudoFileSize - m_pseudoFileOffset;
		if (remainingBytes <= 0)
		{
			// we've already exhausted the buffer
			theErr = eofErr;
		}
		else
		{
			// some bytes remain to be "read" so read them directly
			// from the resource into the caller's buffer
			
			if (remainingBytes < count)
			{
				count = remainingBytes;
			}
			
			ReadPartialResource(m_pseudoFileHandle, m_pseudoFileOffset, buf, count);
			theErr = ResError();
			
			HX_ASSERT(theErr == noErr);
			// while we don't expect any errors, -188 (resourceInMemory) isn't fatal
			
			if (theErr == noErr || theErr == resourceInMemory)
			{
				// update our pseudo-file pointer
				
				readCount = count;
				
				m_pseudoFileOffset += count;
				
				theErr = noErr;
			}
		}
				
	}
	else
	{
		if(mBufferedRead)
		{
			readCount = Copy(buf,readCount);
			if(readCount == 0)
				theErr = eofErr;
		}
		else
		{
	   	    if (IsRunningNativeOnMacOSX() || IsMacInCooperativeThread())
			theErr = ::FSRead(mRefNum, &readCount, buf);
		    else
		    {	// async i/o - callback could be at interrupt time
	#if 0
		    	ParamBlockRec pb;
	//		IOCompletionUPP proc = NewIOCompletionProc(ReadCallback);
			
		    	gReadCount = 0;
	    		gReadDone = FALSE;
			pb.ioParam.ioRefNum = mRefNum;
			pb.ioParam.ioBuffer = buf;
			pb.ioParam.ioReqCount = count;
			pb.ioParam.ioPosMode=fsAtMark;
			pb.ioParam.ioCompletion = m_CompletionProc;
			theErr = PBReadAsync(&pb);
			
			EventRecord  	theEvent;
			long		sleepTime=10;

			long timeout = 0;
			// timeout, in case file read can't complete
			while (!gReadDone && timeout < 100)
			{
				::WaitNextEvent(everyEvent, &theEvent, sleepTime, nil);
				timeout++;
			}
			count = gReadCount;
	#endif
		    }
		}
	}
	
	if(theErr == eofErr && readCount > 0)
		theErr = noErr;

	if (actualCount)
	{
		*actualCount = (INT32) readCount;
	}
	
	return(theErr);
}

/* 	Write writes up to count bytes of data from buf.
	returns the number of bytes written, or -1 if the write failed */
ULONG32 CMacFile::Write(const char *buf, ULONG32 count)
{
	HX_ASSERT(!m_pseudoFileHandle);
	
    OSErr theErr = noErr;
    if (IsRunningNativeOnMacOSX() || IsMacInCooperativeThread())
    {
	if(mAppendMode) 
	{
		theErr = ::SetFPos(mRefNum,fsFromLEOF,0L);
	}
	
	if(!theErr)
	{
		if(mBufferedWrite)
		{
			mWriteFile->write_data((Ptr)buf,(long)count);
		}
		else
		{
		 	theErr = ::FSWrite(mRefNum, (long *)&count, buf);
			//check_noerr(theErr);	 a disk full error may occur
		}
	}
	mLastError = theErr;
//	return(theErr ? -1 : count);
    }
    else
    {
	if(mBufferedWrite)
	{
		mWriteFile->write_data((Ptr)buf,(long)count);
	}
	mLastError = theErr;
    }

	return ((theErr == noErr) ? count : -1);
}

/* 	Write writes up to count bytes of data from buf.
	returns the number of bytes written, or -1 if the write failed */
INT16 CMacFile::Write(const char *buf, INT32 count, INT32 *actualCount)
{
	HX_ASSERT(!m_pseudoFileHandle);

    OSErr theErr = noErr;
    *actualCount = 0;

    if (IsRunningNativeOnMacOSX() || IsMacInCooperativeThread())
    {
	long writeCount = 0;
	
	if(mAppendMode) 
		theErr = ::SetFPos(mRefNum, fsFromLEOF, 0L);
	
	if(!theErr) 
	{
		writeCount = count;
		theErr = ::FSWrite(mRefNum, &writeCount, buf);
	}
	
	*actualCount = (INT32) writeCount;
    }
    
	//check_noerr(theErr); a disk full error may occur
	
	// elsewhere the file code expects a negative actualCount when
	// the write failed
    if (theErr != noErr) *actualCount = -1;
    
    return(theErr);
}

void CMacFile::SetFileType(OSType creator, OSType type)
{
	sFileType = type;
	sCreator = creator;
}


long CMacFile::Copy(Ptr destBuf, long numBytes)
{
	long count;

	// check for buffered i/o
	if(mBufferedRead)
		count = mBufferFile->Copy(destBuf,numBytes);
	else
		count = 0;
		
	return(count);

}


HX_RESULT CMacFile::set_buffered_read	(char buffered)
{
	Boolean OK = TRUE;

	HX_ASSERT(!m_pseudoFileHandle);

	// If this file is setup for writing then we don't want to do any buffered reading.
	if (m_mode & O_WRONLY) return HXR_OK;
	if (m_mode & O_RDWR) return HXR_OK;

	mBufferedRead = 0;
	return HXR_OK; //HACK! - this needs to be redone!
	

	// set up buffered read object if necessary
	if(buffered && !mBufferedRead)	
	{
		if(mBufferFile == NULL) 
		{
			mBufferFile = new CBufferFile;
			OK = mBufferFile != NULL;
		}
			
		if(OK) 
			OK = mBufferFile->Specify(mRefNum);
			
			
	}
	else if(!buffered && mBufferedRead)
	{
		if(mBufferFile != NULL) 
		{
			delete mBufferFile;
			mBufferFile = NULL;
		}
	}

	mBufferedRead = buffered != 0;

	if(OK)
		Seek(0,SEEK_SET);
		
	return(OK ? HXR_OK : HXR_OUTOFMEMORY);
}

HX_RESULT CMacFile::set_buffered_write	(char buffered)
{
Boolean OK = TRUE;

	HX_ASSERT(!m_pseudoFileHandle);

	// set up buffered read object if necessary
	if(buffered && !mBufferedWrite)	
	{
		if(mWriteFile == NULL) 
		{
			mWriteFile = new CWriteFile;
			OK = mWriteFile != NULL;
		}
			
		if(OK) 
		{
			OK = mWriteFile->Specify(mRefNum,Tell());
		}
	}
	else if(!buffered && mBufferedWrite)
	{
		if(mWriteFile != NULL) 
		{
			delete mWriteFile;
			mWriteFile = NULL;
		}
	}

	mBufferedWrite = buffered != 0;
		
	return(OK ? HXR_OK : HXR_OUTOFMEMORY);
}


// This seems to exist solely so FileIO::status can get the size and an error value;
// Otherwise, it's redundant with GetSize

INT16 CMacFile::FileSize(long *size)
{
	if (m_pseudoFileSize)
	{
		*size = m_pseudoFileSize;
		return 0;
	}
	return ::GetEOF(mRefNum, size);
}

BOOL CMacFile::GetTemporaryFileName(const char *tag, char* name, UINT32 ulBufLen)
{
	// we'll make a temp name like tag_2345
	
	CHXString tempNameTemplate(tag);
	
	tempNameTemplate += "_%SUB%";
	
	CHXDirSpecifier tempDirSpec = CHXFileSpecUtils::GetSystemTempDirectory();
	
	CHXFileSpecifier tempFileSpec = CHXFileSpecUtils::GetUniqueTempFileSpec(
		tempDirSpec, (const char *) tempNameTemplate, "%SUB%");
	
	// copy the temp path to the supplied buffer, name
	
	name[0] = '\0';
	if (tempFileSpec.IsSet())
	{
		CHXString strPath = tempFileSpec.GetPathName();
		
		SafeStrCpy(name, (const char *) strPath, ulBufLen);
		
		return TRUE;
	}
	return FALSE;
	
}



⌨️ 快捷键说明

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