SIBCFileRam.h

Go to the documentation of this file.
00001 //***************************************************************************************
00002 // File supervisor: Crosswalk team
00012 //***************************************************************************************
00013 
00014 //***************************************************************************************
00015 // Defines
00016 //***************************************************************************************
00017 #ifndef __CSIBCFileRam_H__
00018 #define __CSIBCFileRam_H__
00019 
00020 //***************************************************************************************
00021 // Includes
00022 //***************************************************************************************
00023 #include <SIBCUtil.h>
00024 
00025 //***************************************************************************************
00026 // Typedefs
00027 //***************************************************************************************
00028 
00029 
00030 class CSIBCFileRam;
00031 
00032 
00035 class CSIBCFileRam
00036 {
00037     public
00038         :
00042         CSIBCFileRam()
00043         {
00044             Init();
00045         }
00046 
00057         CSIBCFileRam(SI_Char *pFileName, _SI_FILE_MODE pAccessRights, void *pMemoryBlock, SI_Int nMemorySize)
00058         {
00059             Init();
00060 
00061             Open(pFileName, pAccessRights, pMemoryBlock, nMemorySize);
00062         }
00063 
00066         ~CSIBCFileRam()
00067         {
00068             Close();
00069         }
00070 
00073         SI_Void Init()
00074         {
00075             m_pFile = _SI_FILE_NULL;
00076 
00077             m_pMemRoot = NULL;
00078             m_pMemPos = NULL;
00079             m_nMemSize = 0;
00080         }
00081 
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 
00102         SI_Void Open(SI_Char *pFileName, _SI_FILE_MODE pAccessRights, SI_Void *pMemoryBlock, SI_Int nMemorySize)
00103         {
00104             if (pMemoryBlock != NULL)
00105             {
00106                 m_pMemRoot = (SI_UByte*)pMemoryBlock;
00107                 m_pMemPos = m_pMemRoot;
00108                 m_nMemSize = nMemorySize;
00109             }
00110             else
00111             if (pFileName)
00112             {
00113                 m_pMemRoot = NULL;
00114                 m_pMemPos = NULL;
00115                 m_nMemSize = 0;
00116 
00117                 m_pFile = _SI_FOPEN(pFileName, pAccessRights);
00118             }
00119         }
00120 
00123         SI_Void Close()
00124         {
00125             if (m_pFile)
00126             {
00127                 _SI_FCLOSE(m_pFile);
00128                 m_pFile = _SI_FILE_NULL;
00129             }
00130 
00131             m_pMemRoot = NULL;
00132             m_pMemPos  = NULL;
00133             m_nMemSize = 0;
00134         }
00135 
00142         SI_Int Read(void *pDest, SI_Int nDestSize, SI_Int nCount)
00143         {
00144             if (m_pFile != NULL)
00145             {
00146                 return (SI_Int)_SI_FREAD(pDest, nDestSize, nCount, m_pFile);
00147             }
00148             else // Use memory block instead of file pointer.
00149             {
00150                 SI_Int size = nDestSize * nCount;
00151                 // Since our memory file can't be bigger than 4Gigs (m_nMemSize is a 32 bits)
00152                 // we can cast to a 32 bits without worrying...
00153                 SI_Int ofs = (SI_Int)(m_pMemPos - m_pMemRoot);
00154 
00155                 // Make sure we don't read out of bounds.
00156                 if ( (ofs + size) > m_nMemSize)
00157                 {
00158                     size = m_nMemSize - ofs;
00159                 }
00160 
00161                 memmove(pDest, m_pMemPos, size );
00162 
00163                 m_pMemPos += size;
00164 
00165                 assert( (size % nDestSize) == 0);
00166 
00167                 return (size / nDestSize);
00168             }
00169 
00170             return 0;
00171         }
00172         // @todo add write function
00173         SI_Int Write(void *pSrc, SI_Int nSrcSize, SI_Int nCount)
00174         {
00175             // Not implemented yet - not needed.
00176             return 0;
00177         }
00178         // @todo add a seek function
00179         // Seeks somewhere in the stream
00180         SI_Int Seek()
00181         {
00182             return 0;
00183         }
00184 
00185         // @todo add a tell function
00186         // Returns the size of the stream
00187         SI_Int Tell()
00188         {
00189             return 0;
00190         }
00191 
00192     private:
00193         SI_UByte *m_pMemRoot;
00194         SI_UByte *m_pMemPos;
00195         SI_Int    m_nMemSize;
00196 
00197         _SI_FILE  m_pFile;
00198 };
00199 
00200 
00201 #endif
00202 // CSIBCFileRam