SIBCFileRam.h

00001 //***************************************************************************************
00002 //
00003 // File supervisor: Crosswalk team
00004 //
00005 // Copyright 2008 Autodesk, Inc.  All rights reserved.  
00006 // Use of this software is subject to the terms of the Autodesk license agreement 
00007 // provided at the time of installation or download, or which otherwise accompanies 
00008 // this software in either electronic or hard copy form.
00009 //
00010 //***************************************************************************************
00011 
00012 //***************************************************************************************
00013 // Defines
00014 //***************************************************************************************
00015 #ifndef __CSIBCFileRam_H__
00016 #define __CSIBCFileRam_H__
00017 
00018 //***************************************************************************************
00019 // Includes
00020 //***************************************************************************************
00021 #include <SIBCUtil.h>
00022 
00023 //***************************************************************************************
00024 // Typedefs
00025 //***************************************************************************************
00026 
00027 
00028 class CSIBCFileRam;
00029 
00030 
00032 class CSIBCFileRam
00033 {
00034     public
00035         :
00039         CSIBCFileRam()
00040         {
00041             Init();
00042         }
00043 
00055         CSIBCFileRam(SI_Char *pFileName, _SI_FILE_MODE pAccessRights, void *pMemoryBlock, SI_Int nMemorySize)
00056         { 
00057             Init();
00058 
00059             Open(pFileName, pAccessRights, pMemoryBlock, nMemorySize);
00060         }
00061 
00064         ~CSIBCFileRam()
00065         { 
00066             Close();
00067         }
00068 
00072         SI_Void Init()
00073         {
00074             m_pFile = _SI_FILE_NULL;
00075 
00076             m_pMemRoot = NULL;
00077             m_pMemPos = NULL;
00078             m_nMemSize = 0;
00079         }
00080 
00086         SI_Bool IsValid()
00087         {
00088 #if !defined( _PSX2 ) && !defined ( _WIN32_WCE )
00089             return (m_pMemRoot != (void*)m_pFile);
00090 #else
00091             return (m_pMemRoot != NULL);
00092 #endif
00093         }
00094         
00095 
00103         SI_Void Open(SI_Char *pFileName, _SI_FILE_MODE pAccessRights, SI_Void *pMemoryBlock, SI_Int nMemorySize) 
00104         { 
00105             if (pMemoryBlock != NULL)
00106             {
00107                 m_pMemRoot = (SI_UByte*)pMemoryBlock;
00108                 m_pMemPos = m_pMemRoot;
00109                 m_nMemSize = nMemorySize;
00110             }
00111             else
00112             if (pFileName) 
00113             {
00114                 m_pMemRoot = NULL;
00115                 m_pMemPos = NULL;
00116                 m_nMemSize = 0;
00117 
00118                 m_pFile = _SI_FOPEN(pFileName, pAccessRights);
00119             }
00120         }
00121 
00125         SI_Void Close() 
00126         { 
00127             if (m_pFile) 
00128             {
00129                 _SI_FCLOSE(m_pFile);
00130                 m_pFile = _SI_FILE_NULL;
00131             }
00132 
00133             m_pMemRoot = NULL;
00134             m_pMemPos  = NULL;
00135             m_nMemSize = 0;
00136         }
00137 
00144         SI_Int Read(void *pDest, SI_Int nDestSize, SI_Int nCount)
00145         {
00146             if (m_pFile != NULL)
00147             {
00148                 return (SI_Int)_SI_FREAD(pDest, nDestSize, nCount, m_pFile);
00149             }
00150             else // Use memory block instead of file pointer.
00151             {
00152                 SI_Int size = nDestSize * nCount;
00153                 // Since our memory file can't be bigger than 4Gigs (m_nMemSize is a 32 bits)
00154                 // we can cast to a 32 bits without worrying...
00155                 SI_Int ofs = (SI_Int)(m_pMemPos - m_pMemRoot);
00156 
00157                 // Make sure we don't read out of bounds.
00158                 if ( (ofs + size) > m_nMemSize)
00159                 {
00160                     size = m_nMemSize - ofs;
00161                 }
00162 
00163                 memmove(pDest, m_pMemPos, size );
00164 
00165                 m_pMemPos += size;
00166 
00167                 assert( (size % nDestSize) == 0);
00168 
00169                 return (size / nDestSize);
00170             }
00171 
00172             return 0;
00173         }
00174         // @todo add write function
00175         SI_Int Write(void *pSrc, SI_Int nSrcSize, SI_Int nCount)
00176         {
00177             // Not implemented yet - not needed.
00178             return 0;
00179         }
00180         // @todo add a seek function
00181         // Seeks somewhere in the stream
00182         SI_Int Seek()
00183         {
00184             return 0;
00185         }
00186         
00187         // @todo add a tell function
00188         // Returns the size of the stream
00189         SI_Int Tell()
00190         {
00191             return 0;
00192         }
00193 
00194     private:
00195         SI_UByte *m_pMemRoot;
00196         SI_UByte *m_pMemPos;
00197         SI_Int    m_nMemSize;
00198 
00199         _SI_FILE  m_pFile;
00200 };
00201 
00202 
00203 #endif 
00204 // CSIBCFileRam