Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members

RageFileBasic.h

Go to the documentation of this file.
00001 /* RageFileBasic - simple file interface. */ 00002 00003 #ifndef RAGE_FILE_BASIC_H 00004 #define RAGE_FILE_BASIC_H 00005 00006 /* This is a simple file I/O interface. Although most of these operations 00007 * are straightforward, there are several of them; most of the time, you'll 00008 * only want to implement RageFileObj. */ 00009 class RageFileBasic 00010 { 00011 public: 00012 virtual ~RageFileBasic() { } 00013 00014 virtual CString GetError() const = 0; 00015 virtual void ClearError() = 0; 00016 virtual bool AtEOF() const = 0; 00017 00018 /* Seek to the given absolute offset. Return to the position actually 00019 * seeked to; if the position given was beyond the end of the file, the 00020 * return value will be the size of the file. */ 00021 virtual int Seek( int iOffset ) = 0; 00022 virtual int Seek( int offset, int whence ) = 0; 00023 virtual int Tell() const = 0; 00024 00025 /* Read at most iSize bytes into pBuf. Return the number of bytes read, 00026 * 0 on end of stream, or -1 on error. Note that reading less than iSize 00027 * does not necessarily mean that the end of the stream has been reached; 00028 * keep reading until 0 is returned. */ 00029 virtual int Read( void *pBuffer, size_t iBytes ) = 0; 00030 virtual int Read( CString &buffer, int bytes = -1 ) = 0; 00031 virtual int Read( void *buffer, size_t bytes, int nmemb ) = 0; 00032 00033 /* Write iSize bytes of data from pBuf. Return 0 on success, -1 on error. */ 00034 virtual int Write( const void *pBuffer, size_t iBytes ) = 0; 00035 virtual int Write( const CString &sString ) = 0; 00036 virtual int Write( const void *buffer, size_t bytes, int nmemb ) = 0; 00037 00038 /* Due to buffering, writing may not happen by the end of a Write() call, so not 00039 * all errors may be returned by it. Data will be flushed when the stream (or its 00040 * underlying object) is destroyed, but errors can no longer be returned. Call 00041 * Flush() to flush pending data, in order to check for errors. */ 00042 virtual int Flush() = 0; 00043 00044 /* This returns a descriptive path for the file, or "". */ 00045 virtual CString GetDisplayPath() const { return ""; } 00046 00047 virtual RageFileBasic *Copy() const = 0; 00048 00049 virtual int GetLine( CString &out ) = 0; 00050 virtual int PutLine( const CString &str ) = 0; 00051 00052 virtual void EnableCRC32( bool on=true ) = 0; 00053 virtual bool GetCRC32( uint32_t *iRet ) = 0; 00054 00055 virtual int GetFileSize() const = 0; 00056 }; 00057 00058 class RageFileObj: public RageFileBasic 00059 { 00060 public: 00061 RageFileObj(); 00062 virtual ~RageFileObj(); 00063 00064 virtual CString GetError() const { return m_sError; } 00065 virtual void ClearError() { SetError(""); } 00066 00067 bool AtEOF() const { return m_bEOF; } 00068 00069 int Seek( int iOffset ); 00070 int Seek( int offset, int whence ); 00071 int Tell() const { return m_iFilePos; } 00072 00073 int Read( void *pBuffer, size_t iBytes ); 00074 int Read( CString &buffer, int bytes = -1 ); 00075 int Read( void *buffer, size_t bytes, int nmemb ); 00076 00077 int Write( const void *pBuffer, size_t iBytes ); 00078 int Write( const CString &sString ) { return Write( sString.data(), sString.size() ); } 00079 int Write( const void *buffer, size_t bytes, int nmemb ); 00080 00081 int Flush(); 00082 00083 int GetLine( CString &out ); 00084 int PutLine( const CString &str ); 00085 00086 void EnableCRC32( bool on=true ); 00087 bool GetCRC32( uint32_t *iRet ); 00088 00089 virtual int GetFileSize() const = 0; 00090 virtual CString GetDisplayPath() const { return ""; } 00091 virtual RageFileBasic *Copy() const { FAIL_M( "Copying unimplemented" ); } 00092 00093 protected: 00094 virtual int SeekInternal( int iOffset ) { FAIL_M( "Seeking unimplemented" ); } 00095 virtual int ReadInternal( void *pBuffer, size_t iBytes ) = 0; 00096 virtual int WriteInternal( const void *pBuffer, size_t iBytes ) = 0; 00097 virtual int FlushInternal() { return 0; } 00098 00099 void EnableBuffering(); 00100 00101 void SetError( const CString &sError ) { m_sError = sError; } 00102 CString m_sError; 00103 00104 private: 00105 int FillBuf(); 00106 void ResetBuf(); 00107 00108 bool m_bEOF; 00109 int m_iFilePos; 00110 00111 /* 00112 * If buffering is enabled, m_pBuffer is the buffer, m_pBuf is the current read 00113 * position in the buffer and m_iBufAvail is the number of bytes at m_pBuf. Note 00114 * that buffering is only enabled if: 00115 * 00116 * - GetLine() is called (which requires buffering to efficiently search for newlines); 00117 * - or EnableBuffering() is called 00118 * 00119 * Currently, once buffering is enabled, it stays enabled for the life of the 00120 * object. 00121 * 00122 * If buffering is not enabled, this buffer will not be allocated, keeping the 00123 * size overhead of each file down. Layered RageFileBasic implementations, which 00124 * read from other RageFileBasics, should generally not use buffering, in order 00125 * to avoid reads being passed through several buffers, which is only a waste of 00126 * memory. 00127 */ 00128 enum { BSIZE = 1024 }; 00129 char *m_pBuffer; 00130 char *m_pBuf; 00131 int m_iBufAvail; 00132 00133 /* If EnableCRC32() is called, a CRC32 will be calculated as the file is read. 00134 * This is only meaningful if EnableCRC32() is called at the very start of the 00135 * file, and no seeking is performed. */ 00136 bool m_bCRC32Enabled; 00137 uint32_t m_iCRC32; 00138 }; 00139 00140 #endif 00141 00142 /* 00143 * Copyright (c) 2003-2004 Glenn Maynard, Chris Danford, Steve Checkoway 00144 * All rights reserved. 00145 * 00146 * Permission is hereby granted, free of charge, to any person obtaining a 00147 * copy of this software and associated documentation files (the 00148 * "Software"), to deal in the Software without restriction, including 00149 * without limitation the rights to use, copy, modify, merge, publish, 00150 * distribute, and/or sell copies of the Software, and to permit persons to 00151 * whom the Software is furnished to do so, provided that the above 00152 * copyright notice(s) and this permission notice appear in all copies of 00153 * the Software and that both the above copyright notice(s) and this 00154 * permission notice appear in supporting documentation. 00155 * 00156 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 00157 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00158 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF 00159 * THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS 00160 * INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT 00161 * OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS 00162 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR 00163 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 00164 * PERFORMANCE OF THIS SOFTWARE. 00165 */ 00166

Generated on Thu Jan 27 20:57:27 2005 for StepMania by doxygen 1.3.7