Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | Related Pages

file.h

Go to the documentation of this file.
00001 /***************************************************************************
00002        file.h  -  File abstraction. Normal files, memory buffer files
00003                        and virtual files in packages
00004                              -------------------
00005     begin                : Thu Nov 1 2001
00006     copyright            : (C) 2001 by Reality Rift Studios
00007     email                : mattias@realityrift.com
00008  ***************************************************************************
00009 
00010  The contents of this file are subject to the Mozilla Public License Version
00011  1.1 (the "License"); you may not use this file except in compliance with
00012  the License. You may obtain a copy of the License at 
00013  http://www.mozilla.org/MPL/
00014 
00015  Software distributed under the License is distributed on an "AS IS" basis,
00016  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00017  for the specific language governing rights and limitations under the
00018  License.
00019 
00020  The Original Code is the NeoEngine, file.h
00021 
00022  The Initial Developer of the Original Code is Mattias Jansson.
00023  Portions created by Mattias Jansson are Copyright (C) 2001
00024  Reality Rift Studios. All Rights Reserved.
00025 
00026  ***************************************************************************/
00027 
00028 #ifndef __NEFILE_H
00029 #define __NEFILE_H
00030 
00031 
00038 #include "base.h"
00039 #include "core.h"
00040 
00041 #include <string>
00042 #include <fstream>
00043 
00044 
00045 namespace NeoEngine
00046 {
00047 
00048 
00049 // External classes
00050 class Directory;
00051 class Package;
00052 class HashString;
00053 
00054 
00055 
00071 class NEOENGINE_API File
00072 {
00073     protected:
00074 
00076         std::iostream                                *m_pkStream;
00077 
00079         std::ios_base::openmode                       m_uiOpenMode;
00080 
00082         bool                                          m_bBinary;
00083 
00085         Core::BYTEORDER                               m_eByteOrder;
00086 
00087         /* Path */
00088         std::string                                   m_strPath;
00089 
00090         /* Filename */
00091         std::string                                   m_strName;
00092 
00094         Directory                                    *m_pkDirectory;
00095 
00096 
00101         virtual void                                  AllocStream( const std::string &rstrFullPath );
00102 
00103 
00104     public:
00105 
00114                                                       File( const std::string &rstrPath = "", const std::string &rstrFilename = "", std::ios_base::openmode uiOpenMode = ( std::ios_base::in | std::ios_base::binary ), bool bOpen = false, Directory *pkDirectory = 0 );
00115 
00119         virtual                                      ~File();
00120 
00128         virtual bool                                  Open( const std::string &rstrPath = "", const std::string &rstrFilename = "", std::ios_base::openmode uiOpenMode = (std::ios_base::openmode)0 );
00129 
00133         virtual void                                  Close();
00134 
00139         virtual bool                                  IsValid() const { return( m_pkStream && !( !*m_pkStream  ) ); }
00140 
00145         void                                          SetByteOrder( Core::BYTEORDER eByteOrder ) { m_eByteOrder = eByteOrder; }
00146 
00151         virtual int                                   GetSize();
00152 
00157         inline std::streampos                         Tellg() const { return( m_pkStream ? m_pkStream->tellg() : (std::streampos)0 ); }
00158 
00163         inline std::streampos                         Tellp() const { return( m_pkStream ? m_pkStream->tellp() : (std::streampos)0 ); }
00164 
00171         virtual File                                 &Read( void *pBuffer, int iNumBytes );
00172 
00179         virtual File                                 &Write( const void *pBuffer, int iNumBytes );
00180 
00188         virtual File                                 &GetLine( char *pcDest, unsigned int uiCount, char cDelimiter = '\n' );
00189 
00196         inline File                                  &Seekg( std::streamoff uiOffset, std::ios_base::seekdir uiDirection ) { if( m_pkStream ) m_pkStream->seekg( uiOffset, uiDirection ); return( *this ); }
00197 
00204         inline File                                  &Seekp( std::streamoff uiOffset, std::ios_base::seekdir uiDirection ) { if( m_pkStream ) m_pkStream->seekp( uiOffset, uiDirection ); return( *this ); }
00205 
00209         const std::string                            &GetPath() const { return m_strPath; }
00210 
00214         const std::string                            &GetName() const { return m_strName; }
00215 
00219         Directory                                    *GetDirectory() { return m_pkDirectory; }
00220 
00224         virtual Core::BYTEORDER                       GetByteOrder() const { return m_eByteOrder; }
00225 
00234         bool                                          DetermineByteOrder( int iReferenceValue );
00235 
00241         bool                                          SetBinary( bool bBinary );
00242 
00247         bool                                          IsBinary() const;
00248 
00255         bool                                          DetermineBinaryMode( const unsigned char *pucReferenceValues );
00256 
00262         File                                         &operator << ( bool bData );
00263 
00269         File                                         &operator << ( int8_t cData );
00270 
00276         File                                         &operator << ( uint8_t ucData );
00277 
00283         File                                         &operator << ( int16_t sData );
00284 
00290         File                                         &operator << ( uint16_t usData );
00291 
00297         File                                         &operator << ( int32_t iData );
00298 
00304         File                                         &operator << ( uint32_t uiData );
00305 
00311         File                                         &operator << ( float fData );
00312 
00318         File                                         &operator << ( const char *pszData );
00319 
00325         File                                         &operator << ( const std::string &rstrData );
00326 
00332         File                                         &operator << ( const HashString &rstrData );
00333 
00339         File                                         &operator >> ( bool &rbData );
00340 
00346         File                                         &operator >> ( int8_t &rcData );
00347 
00353         File                                         &operator >> ( uint8_t &rucData );
00354 
00360         File                                         &operator >> ( int16_t &rsData );
00361 
00367         File                                         &operator >> ( uint16_t &rusData );
00368 
00374         File                                         &operator >> ( int32_t &riData );
00375 
00381         File                                         &operator >> ( uint32_t &ruiData );
00382 
00388         File                                         &operator >> ( float &rfData );
00389 
00395         File                                         &operator >> ( std::string &rstrData );
00396 
00402         File                                         &operator >> ( HashString &rstrData );
00403 
00408         inline File                                  &operator << ( std::ios_base &( NE_CDECL *pfnManip )( std::ios_base& ) )
00409         {
00410             (*pfnManip)( *( std::ios_base* )m_pkStream );
00411             return( *this );
00412         }
00413 
00418         inline File                                  &operator << ( std::basic_ostream< char > &( NE_CDECL *pfnManip )( std::basic_ostream< char >& ) )
00419         {
00420             (*pfnManip)( *( std::basic_ostream< char >* )m_pkStream );
00421             return( *this );
00422         }
00423 
00428         inline File                                  &operator >> ( std::ios_base &( NE_CDECL *pfnManip )( std::ios_base& ) )
00429         {
00430             (*pfnManip)( *( std::ios_base* )m_pkStream );
00431             return( *this );
00432         }
00433 
00438         inline File                                  &operator >> ( std::basic_ostream< char > &( NE_CDECL *pfnManip )( std::basic_ostream< char >& ) )
00439         {
00440             (*pfnManip)( *( std::basic_ostream< char >* )m_pkStream );
00441             return( *this );
00442         }
00443 
00448         inline bool                                   operator !() const { return !IsValid(); }
00449 
00455         static std::string                            ExtractBaseFileName( const std::string &rstrPath );
00456 
00462         static std::string                            ExtractFileExtension( const std::string &rstrPath );
00463 
00469         static std::string                            ExtractFileName( const std::string &rstrPath );
00470 
00476         static std::string                            ExtractPathName( const std::string &rstrPath );
00477 };
00478 
00479 
00480 
00487 class NEOENGINE_API BufferFileStreamBuf : public std::basic_streambuf< char >
00488 {
00489     public:
00490 
00491         typedef std::basic_streambuf< char >::pos_type pos_type;
00492         typedef std::basic_streambuf< char >::off_type off_type;
00493         typedef std::basic_streambuf< char >::int_type int_type;
00494 
00495     protected:
00496 
00498         unsigned char                                *m_pucBuffer;
00499 
00501         unsigned int                                  m_uiSize;
00502 
00504         unsigned int                                  m_uiGetOffset;
00505 
00506 
00513         virtual std::basic_streambuf< char >         *setbuf( char *pcBuffer, std::streamsize uiSize );
00514 
00522         virtual pos_type                              seekoff( off_type iOffset, std::ios_base::seekdir uiDir, std::ios_base::openmode uiMode );
00523 
00530         virtual pos_type                              seekpos( pos_type uiPosition, std::ios_base::openmode uiMode );
00531 
00536         virtual int_type                              underflow();
00537 
00538 
00539     public:
00540 
00546                                                       BufferFileStreamBuf( unsigned char *pucBuffer, unsigned int uiSize );
00547 
00551         virtual                                      ~BufferFileStreamBuf();
00552 };
00553 
00554 
00564 class NEOENGINE_API BufferFile : public File
00565 {
00566     protected:
00567 
00569         BufferFileStreamBuf                          *m_pkBuffer;
00570 
00571 
00576         virtual void                                  AllocStream( const std::string &rstrFullPath );
00577 
00578 
00579     public:
00580 
00590                                                       BufferFile( unsigned char *pucBuffer = 0, int iSize = 0, const std::string &rstrPath = "", const std::string &rstrFilename = "", std::ios_base::openmode uiOpenMode = ( std::ios_base::in | std::ios_base::binary ), bool bOpen = false, Directory *pkDirectory = 0 );
00591 
00594         virtual                                      ~BufferFile();
00595 
00599         virtual void                                  Close();
00600 
00606         void                                          SetBuffer( unsigned char *pucBuffer, int iSize );
00607 };
00608 
00609 
00610 
00611 
00612 
00613 
00622 class NEOENGINE_API VirtualFile : public BufferFile
00623 {
00624     friend class Package;
00625     friend class VirtualFileTemplate;
00626 
00627     protected:
00628 
00630         int                                           m_iFileOffset;
00631 
00633         int                                           m_iFileFlags;
00634 
00636         Package                                      *m_pkPackage;
00637 
00639         int                                           m_iFileData[4];
00640 
00642         int                                           m_iStoredFileSize;
00643         
00644 
00649         virtual void                                  AllocStream( const std::string &rstrFullPath );
00650 
00651     
00652     public:
00653 
00659                                                       VirtualFile( const std::string &rstrPath = "", const std::string &rstrFilename = "", Directory *pkDirectory = 0 );
00660 
00663         virtual                                      ~VirtualFile();
00664 };
00665 
00666 
00667 };
00668 
00669 
00670 #endif

Generated on Wed Jan 21 14:21:06 2004 for NeoEngine by doxygen 1.3.5