CPI_Stream_LocalFile.c
上传用户:hxb_1234
上传日期:2010-03-30
资源大小:8328k
文件大小:3k
源码类别:

VC书籍

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "globals.h"
  3. #include "CPI_Stream.h"
  4. typedef struct _CPs_InStream_File
  5. {
  6.     HANDLE m_hFile;
  7. } CPs_InStream_File;
  8. //
  9. void CPSLOCAL_Uninitialise(CPs_InStream* pStream);
  10. BOOL CPSLOCAL_Read(CPs_InStream* pStream, void* pDestBuffer, const unsigned int iBytesToRead, unsigned int* piBytesRead);
  11. void CPSLOCAL_Seek(CPs_InStream* pStream, const unsigned int iNewOffset);
  12. unsigned int CPSLOCAL_GetLength(CPs_InStream* pStream);
  13. BOOL CPSLOCAL_IsSeekable(CPs_InStream* pStream);
  14. unsigned int CPSLOCAL_Tell(CPs_InStream* pStream);
  15. CPs_InStream* CP_CreateInStream_LocalFile(const char* pcFlexiURL, HWND hWndOwner)
  16. {
  17.     HANDLE hFile;
  18.     hFile = CreateFile(pcFlexiURL, GENERIC_READ,
  19.                        FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
  20.                        OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, 0);
  21.     if(hFile == INVALID_HANDLE_VALUE)
  22.         return NULL;
  23.     {
  24.         CPs_InStream* pNewStream = (CPs_InStream*)malloc(sizeof(CPs_InStream));
  25.         CPs_InStream_File* pContext = (CPs_InStream_File*)malloc(sizeof(CPs_InStream_File));
  26.         pNewStream->Uninitialise = CPSLOCAL_Uninitialise;
  27.         pNewStream->Read = CPSLOCAL_Read;
  28.         pNewStream->Seek = CPSLOCAL_Seek;
  29. pNewStream->Tell = CPSLOCAL_Tell;
  30.         pNewStream->GetLength = CPSLOCAL_GetLength;
  31.         pNewStream->IsSeekable = CPSLOCAL_IsSeekable;
  32.         pNewStream->m_pModuleCookie = pContext;
  33.         pContext->m_hFile = hFile;
  34.         return pNewStream;
  35.     }
  36. }
  37. //
  38. void CPSLOCAL_Uninitialise(CPs_InStream* pStream)
  39. {
  40.     CPs_InStream_File* pContext = (CPs_InStream_File*)pStream->m_pModuleCookie;
  41.     CP_CHECKOBJECT(pContext);
  42.     CloseHandle(pContext->m_hFile);
  43.     free(pContext);
  44.     free(pStream);
  45. }
  46. //
  47. BOOL CPSLOCAL_Read(CPs_InStream* pStream, void* pDestBuffer, const unsigned int iBytesToRead, unsigned int* piBytesRead)
  48. {
  49.     CPs_InStream_File* pContext = (CPs_InStream_File*)pStream->m_pModuleCookie;
  50.     CP_CHECKOBJECT(pContext);
  51.     return ReadFile(pContext->m_hFile, pDestBuffer, iBytesToRead, piBytesRead, 0);
  52. }
  53. //
  54. void CPSLOCAL_Seek(CPs_InStream* pStream, const unsigned int iNewOffset)
  55. {
  56.     CPs_InStream_File* pContext = (CPs_InStream_File*)pStream->m_pModuleCookie;
  57.     CP_CHECKOBJECT(pContext);
  58.     SetFilePointer(pContext->m_hFile, iNewOffset, 0, FILE_BEGIN);
  59. }
  60. //
  61. unsigned int CPSLOCAL_GetLength(CPs_InStream* pStream)
  62. {
  63.     CPs_InStream_File* pContext = (CPs_InStream_File*)pStream->m_pModuleCookie;
  64.     CP_CHECKOBJECT(pContext);
  65.     return GetFileSize(pContext->m_hFile, NULL);
  66. }
  67. //
  68. BOOL CPSLOCAL_IsSeekable(CPs_InStream* pStream)
  69. {
  70.     return TRUE;
  71. }
  72. //
  73. #define GetFilePointer(hFile) SetFilePointer(hFile, 0, NULL, FILE_CURRENT)
  74. unsigned int CPSLOCAL_Tell(CPs_InStream* pStream)
  75. {
  76.     CPs_InStream_File* pContext = (CPs_InStream_File*)pStream->m_pModuleCookie;
  77.     CP_CHECKOBJECT(pContext);
  78.     return GetFilePointer(pContext->m_hFile);
  79. }