⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mystream.cpp

📁 佳能数码相机 SDK 7.3 开发包 VC++代码 含英文说明文档
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	{
		memcpy( pMemStrm->cpBuffer, pBuf, *pBufsize );
		pMemStrm->lPos += (*pBufsize);
	}
	
	if( pMemStrm->dwVisibleSize < (cdUInt32)pMemStrm->lPos )
	{
		pMemStrm->dwVisibleSize = pMemStrm->lPos;
	}
	
}

/* The SEEK function registered into a stream */
void cdSTDCALL _SeekMyMemStream(	cdContext	contextH,
									cdWhence	origin,
									cdInt32		offset,
									cdError		*pErr )
{
	MemStreamData	*pMemStrm;
	cdInt32			lNewPos;
	cdChar			*cpNewBuffer;
	
	*pErr = cdOK;
	/* It investigates whether data required for processing is assembled. */
	pMemStrm = (MemStreamData*)contextH;
	if( !pMemStrm )
	{
		*pErr = cdSTREAM_SEEK_ERROR;
		return;
	}
	
	if( !(pMemStrm->mode&MODE_MEMSTREAM_OPEN) )
	{
		*pErr = cdSTREAM_NOT_OPEN;
		return;
	}
	
	switch( origin )
	{
		case cdSTART:
			lNewPos = offset;
			break;
		case cdCURRENT:
			lNewPos = pMemStrm->lPos + offset;
			break;
		case cdEND:
			lNewPos = pMemStrm->dwBufferSize - offset;
			break;
		default:
			*pErr = cdSTREAM_BAD_OPTIONS;
			return;
	}
	
	if( (cdUInt32)lNewPos <= pMemStrm->dwBufferSize )
	{
		pMemStrm->lPos = lNewPos;
	}
	else
	{
		/* A buffer is enlarged when larger than a buffer. */
		cpNewBuffer = new cdChar[lNewPos];
		if( cpNewBuffer == NULL )
		{
			*pErr = cdSTREAM_SEEK_ERROR;
			return;
		}
		
		memcpy( cpNewBuffer, pMemStrm->cpBuffer, pMemStrm->dwBufferSize );
		memset( cpNewBuffer+pMemStrm->dwBufferSize, 0, lNewPos-pMemStrm->lPos );
		
		delete	[]pMemStrm->cpBuffer;
		pMemStrm->cpBuffer = cpNewBuffer;
		pMemStrm->dwBufferSize = lNewPos;
		pMemStrm->lPos = lNewPos;
	}
	
	if( pMemStrm->dwVisibleSize < (cdUInt32)pMemStrm->lPos )
	{
		pMemStrm->dwVisibleSize = pMemStrm->lPos;
	}
	
}

/* The TELL function registered into a stream */
cdInt32 cdSTDCALL _TellMyMemStream(	cdContext	contextH,
									cdError		*pErr )
{
	MemStreamData	*pMemStrm;
	
	
	*pErr = cdOK;
	/* It investigates whether data required for processing is assembled. */
	pMemStrm = (MemStreamData*)contextH;
	if( !pMemStrm )
	{
		*pErr = cdSTREAM_TELL_ERROR;
		return	0;
	}
	
	if( !(pMemStrm->mode&MODE_MEMSTREAM_OPEN) )
	{
		*pErr = cdSTREAM_NOT_OPEN;
		return	0;
	}
	
	return	pMemStrm->lPos;
}


///////////////////////////////////////////////////////////////
//	My file stream

BOOL	CreateMyFilStream(	cdStream	*pStream,
							cdChar		*szFileName )
{
	FilStreamData	*pFilStrm;
	
	/* The domain for data is secured. */
	pFilStrm = new FilStreamData;
	if( pFilStrm == NULL )
	{
		return	FALSE;
	}
	
	/* Data is changed the first stage. */
	pFilStrm->hFile = INVALID_HANDLE_VALUE;
	strcpy( pFilStrm->szFileName, szFileName );
	
	pStream->contextH = (cdContext)pFilStrm;
	pStream->close = _CloseMyFilStream;
	pStream->open = _OpenMyFilStream;
	pStream->read = _ReadMyFilStream;
	pStream->seek = _SeekMyFilStream;
	pStream->tell = _TellMyFilStream;
	pStream->write = _WriteMyFilStream;
	
	return	TRUE;
}

/* A stream is released. */
void	ReleaseMyFilStream( cdStream	*pStream )
{
	FilStreamData	*pFilStrm;
	
	/* A domain is released. */
	pFilStrm = (FilStreamData*)pStream->contextH;
	if( pFilStrm )
	{
		delete	pFilStrm;
		pStream->contextH = (cdContext)NULL;
	}
	
	pStream->close = NULL;
	pStream->open = NULL;
	pStream->read = NULL;
	pStream->seek = NULL;
	pStream->tell = NULL;
	pStream->write = NULL;
}

/* The OPEN function registered into a stream */
void cdSTDCALL _OpenMyFilStream(	cdContext		contextH,
									cdPermission	mode,
									cdError			*pErr )
{
	FilStreamData		*pFilStrm;
	DWORD				dwDesiredAccess,dwShareMode,dwCreationDisposition;
	
	
	*pErr = cdOK;
	/* It investigates whether data required for processing is assembled. */
	pFilStrm = (FilStreamData*)contextH;
	if( !pFilStrm )
	{
		*pErr = cdSTREAM_OPEN_ERROR;
		return;
	}
	
	if( pFilStrm->hFile != INVALID_HANDLE_VALUE )
	{
		*pErr = cdSTREAM_ALREADY_OPEN;
		return;
	}
	
	/* The current position is set up. */
	if( mode == cdUPDATE )
	{
		dwDesiredAccess = GENERIC_WRITE|GENERIC_READ;
		dwShareMode = 0;
		dwCreationDisposition = OPEN_ALWAYS;
	}
	else if( (mode&cdREAD) || (mode&cdWRITE) )
	{
		switch( mode )
		{
			case (cdREAD|cdWRITE):
				dwDesiredAccess = GENERIC_READ|GENERIC_WRITE;
				dwShareMode = 0;
				dwCreationDisposition = CREATE_ALWAYS;
				break;
			case cdREAD:
				dwDesiredAccess = GENERIC_READ;
				dwShareMode = FILE_SHARE_READ;
				dwCreationDisposition = OPEN_EXISTING;
				break;
			case cdWRITE:
				dwDesiredAccess = GENERIC_WRITE;
				dwShareMode = 0;
				dwCreationDisposition = CREATE_ALWAYS;
				break;
		}
	}
	else
	{
		*pErr = cdSTREAM_BAD_OPTIONS;
		return;
	}
	
	pFilStrm->hFile = CreateFile(	pFilStrm->szFileName,
									dwDesiredAccess,
									dwShareMode,
									NULL,
									dwCreationDisposition,
									FILE_ATTRIBUTE_NORMAL,
									NULL );
	if( pFilStrm->hFile == INVALID_HANDLE_VALUE )
	{
		*pErr = cdSTREAM_OPEN_ERROR;
	}
	
}

/* The CLOSE function registered into a stream */
void cdSTDCALL _CloseMyFilStream(	cdContext	contextH,
									cdError		*pErr )
{
	FilStreamData		*pFilStrm;
	BOOL				fRes;
	
	
	*pErr = cdOK;
	/* It investigates whether data required for processing is assembled. */
	pFilStrm = (FilStreamData*)contextH;
	if( !pFilStrm )
	{
		*pErr = cdSTREAM_CLOSE_ERROR;
		return;
	}
	
	fRes = CloseHandle( pFilStrm->hFile );
	if( fRes == FALSE )
	{
		*pErr = cdSTREAM_CLOSE_ERROR;
		return;
	}
	
}

/* The READ function registered into a stream */
void cdSTDCALL _ReadMyFilStream(	cdContext	contextH,
									void		*pBuf,
									cdUInt32	*pBufsize,
									cdError		*pErr )
{
	FilStreamData	*pFilStrm;
	BOOL			fRes;
	DWORD			dwNumberOfBytesToRead;
	
	
	*pErr = cdOK;
	/* It investigates whether data required for processing is assembled. */
	pFilStrm = (FilStreamData*)contextH;
	if( !pFilStrm )
	{
		*pErr = cdSTREAM_READ_ERROR;
		return;
	}
	
	dwNumberOfBytesToRead = *pBufsize;
	fRes = ReadFile( pFilStrm->hFile, pBuf, dwNumberOfBytesToRead, pBufsize, NULL );
	if( fRes == FALSE )
	{
		*pErr = cdSTREAM_READ_ERROR;
		return;
	}
	
}

/* The WRITE function registered into a stream */
void cdSTDCALL _WriteMyFilStream(	cdContext	contextH,
									const void	*pBuf,
									cdUInt32	*pBufsize,
									cdError		*pErr )
{
	FilStreamData	*pFilStrm;
	BOOL			fRes;
	DWORD			dwNumberOfBytesToWrite;
	
	
	*pErr = cdOK;
	/* It investigates whether data required for processing is assembled. */
	pFilStrm = (FilStreamData*)contextH;
	if( !pFilStrm )
	{
		*pErr = cdSTREAM_WRITE_ERROR;
		return;
	}
	
	dwNumberOfBytesToWrite = *pBufsize;
	fRes = WriteFile( pFilStrm->hFile, pBuf, dwNumberOfBytesToWrite, pBufsize, NULL );
	if( fRes ==FALSE )
	{
		*pErr = cdSTREAM_WRITE_ERROR;
		return;
	}
	
}

/* The SEEK function registered into a stream */
void cdSTDCALL _SeekMyFilStream(	cdContext	contextH,
									cdWhence	origin,
									cdInt32		offset,
									cdError		*pErr )
{
	FilStreamData	*pFilStrm;
	DWORD			dwMoveMethod;
	DWORD			dwLowPos;
	
	
	*pErr = cdOK;
	/* It investigates whether data required for processing is assembled. */
	pFilStrm = (FilStreamData*)contextH;
	if( !pFilStrm )
	{
		*pErr = cdSTREAM_SEEK_ERROR;
		return;
	}
	
	switch( origin )
	{
		case cdSTART:
			dwMoveMethod = FILE_BEGIN;
			break;
		case cdCURRENT:
			dwMoveMethod = FILE_CURRENT;
			break;
		case cdEND:
			dwMoveMethod = FILE_END;
			break;
		default:
			*pErr = cdSTREAM_BAD_OPTIONS;
			return;
	}
	
	dwLowPos = SetFilePointer( pFilStrm->hFile, offset, NULL, dwMoveMethod );
	if( (dwLowPos==0xFFFFFFFF) && (GetLastError() != NO_ERROR) )
	{
		*pErr = cdSTREAM_SEEK_ERROR;
	}
	
}

/* The TELL function registered into a stream */
cdInt32 cdSTDCALL _TellMyFilStream(	cdContext	contextH,
									cdError		*pErr )
{
	FilStreamData	*pFilStrm;
	cdInt32			dwLowPos;
	
	
	*pErr = cdOK;
	/* It investigates whether data required for processing is assembled. */
	pFilStrm = (FilStreamData*)contextH;
	if( !pFilStrm )
	{
		*pErr = cdSTREAM_TELL_ERROR;
		return	0;
	}
	
	dwLowPos = SetFilePointer( pFilStrm->hFile, 0, NULL, FILE_CURRENT );
	if( (dwLowPos==0xFFFFFFFF) && (GetLastError() != NO_ERROR) )
	{
		*pErr = cdSTREAM_SEEK_ERROR;
		return	0;
	}
	
	return	dwLowPos;
}

⌨️ 快捷键说明

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