SIBCFileRam.h

00001 //***************************************************************************************
00002 //
00003 // File supervisor: Softimage 3D Games & 3D Bridge team
00004 //
00005 // (c) Copyright 2001-2002 Avid Technology, Inc. . All rights reserved.
00006 //
00007 //***************************************************************************************
00008 
00009 /****************************************************************************************
00010 THIS CODE IS PUBLISHED AS A SAMPLE ONLY AND IS PROVIDED "AS IS".
00011 IN NO EVENT SHALL SOFTIMAGE, AVID TECHNOLOGY, INC. AND/OR THEIR RESPECTIVE
00012 SUPPLIERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
00013 DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
00014 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
00015 CONNECTION WITH THE USE OR PERFORMANCE OF THIS CODE .
00016 
00017 COPYRIGHT NOTICE. Copyright © 1999-2002 Avid Technology Inc. . All rights reserved. 
00018 
00019 SOFTIMAGE is a registered trademark of Avid Technology Inc. or its subsidiaries 
00020 or divisions. Windows NT is a registered trademark of Microsoft Corp. All other
00021 trademarks contained herein are the property of their respective owners. 
00022 ****************************************************************************************/
00023 
00024 //***************************************************************************************
00025 // Defines
00026 //***************************************************************************************
00027 #ifndef __CSIBCFileRam_H__
00028 #define __CSIBCFileRam_H__
00029 
00030 //***************************************************************************************
00031 // Includes
00032 //***************************************************************************************
00033 #include <SIBCUtil.h>
00034 
00035 //***************************************************************************************
00036 // Typedefs
00037 //***************************************************************************************
00038 
00039 
00040 class CSIBCFileRam;
00041 
00042 
00044 class CSIBCFileRam
00045 {
00046     public
00047         :
00051         CSIBCFileRam()
00052         {
00053             Init();
00054         }
00055 
00067         CSIBCFileRam(SI_Char *pFileName, _SI_FILE_MODE pAccessRights, void *pMemoryBlock, SI_Int nMemorySize)
00068         { 
00069             Init();
00070 
00071             Open(pFileName, pAccessRights, pMemoryBlock, nMemorySize);
00072         }
00073 
00076         ~CSIBCFileRam()
00077         { 
00078             Close();
00079         }
00080 
00084         SI_Void Init()
00085         {
00086             m_pFile = _SI_FILE_NULL;
00087 
00088             m_pMemRoot = NULL;
00089             m_pMemPos = NULL;
00090             m_nMemSize = 0;
00091         }
00092 
00098         SI_Bool IsValid()
00099         {
00100 #if !defined( _PSX2 ) && !defined ( _WIN32_WCE )
00101             return (m_pMemRoot != (void*)m_pFile);
00102 #else
00103             return (m_pMemRoot != NULL);
00104 #endif
00105         }
00106         
00107 
00115         SI_Void Open(SI_Char *pFileName, _SI_FILE_MODE pAccessRights, SI_Void *pMemoryBlock, SI_Int nMemorySize) 
00116         { 
00117             if (pMemoryBlock != NULL)
00118             {
00119                 m_pMemRoot = (SI_UByte*)pMemoryBlock;
00120                 m_pMemPos = m_pMemRoot;
00121                 m_nMemSize = nMemorySize;
00122             }
00123             else
00124             if (pFileName) 
00125             {
00126                 m_pMemRoot = NULL;
00127                 m_pMemPos = NULL;
00128                 m_nMemSize = 0;
00129 
00130                 m_pFile = _SI_FOPEN(pFileName, pAccessRights);
00131             }
00132         }
00133 
00137         SI_Void Close() 
00138         { 
00139             if (m_pFile) 
00140             {
00141                 _SI_FCLOSE(m_pFile);
00142                 m_pFile = _SI_FILE_NULL;
00143             }
00144 
00145             m_pMemRoot = NULL;
00146             m_pMemPos  = NULL;
00147             m_nMemSize = 0;
00148         }
00149 
00156         SI_Int Read(void *pDest, SI_Int nDestSize, SI_Int nCount)
00157         {
00158             if (m_pFile != NULL)
00159             {
00160                 return (SI_Int)_SI_FREAD(pDest, nDestSize, nCount, m_pFile);
00161             }
00162             else // Use memory block instead of file pointer.
00163             {
00164                 SI_Int size = nDestSize * nCount;
00165                 // Since our memory file can't be bigger than 4Gigs (m_nMemSize is a 32 bits)
00166                 // we can cast to a 32 bits without worrying...
00167                 SI_Int ofs = (SI_Int)(m_pMemPos - m_pMemRoot);
00168 
00169                 // Make sure we don't read out of bounds.
00170                 if ( (ofs + size) > m_nMemSize)
00171                 {
00172                     size = m_nMemSize - ofs;
00173                 }
00174 
00175                 memmove(pDest, m_pMemPos, size );
00176 
00177                 m_pMemPos += size;
00178 
00179                 assert( (size % nDestSize) == 0);
00180 
00181                 return (size / nDestSize);
00182             }
00183 
00184             return 0;
00185         }
00186         // @todo add write function
00187         SI_Int Write(void *pSrc, SI_Int nSrcSize, SI_Int nCount)
00188         {
00189             // Not implemented yet - not needed.
00190             return 0;
00191         }
00192         // @todo add a seek function
00193         // Seeks somewhere in the stream
00194         SI_Int Seek()
00195         {
00196             return 0;
00197         }
00198         
00199         // @todo add a tell function
00200         // Returns the size of the stream
00201         SI_Int Tell()
00202         {
00203             return 0;
00204         }
00205 
00206     private:
00207         SI_UByte *m_pMemRoot;
00208         SI_UByte *m_pMemPos;
00209         SI_Int    m_nMemSize;
00210 
00211         _SI_FILE  m_pFile;
00212 };
00213 
00214 
00215 #endif 
00216 // CSIBCFileRam