00001 /// \file CATStreamFile.h 00002 /// \brief File stream class 00003 /// \ingroup CAT 00004 /// 00005 /// Copyright (c) 2003-2007 by Michael Ellison. 00006 /// See COPYING.txt for the \ref gaslicense License (MIT License). 00007 /// 00008 // $Author: mikeellison $ 00009 // $Date: 2008-01-21 08:33:12 -0600 (Mon, 21 Jan 2008) $ 00010 // $Revision: $ 00011 // $NoKeywords: $ 00012 00013 00014 #ifndef _CATStreamFile_H_ 00015 #define _CATStreamFile_H_ 00016 00017 #include "CATInternal.h" 00018 #include "CATStream.h" 00019 00020 /// \class CATStreamFile CATStreamFile.h 00021 /// \brief Generic file stream class based on STDIO 00022 /// \ingroup CAT 00023 /// 00024 /// CATStreamFile provides a basic file stream interface. It does NOT 00025 /// currently support locking (non-shared) modes, and will assert or 00026 /// return an error if you try to use them. 00027 /// 00028 /// File sharing is a platform dependant issue. Create a platform 00029 /// dependant class inherited from CATStream if you need to control 00030 /// it. Alternatively, implement lock files or named semaphores 00031 /// in this class. I don't need it yet. 00032 /// 00033 class CATStreamFile : public CATStream 00034 { 00035 public: 00036 /// Default constructor doesn't do much - you'll need 00037 /// to call Open() before trying to do much. 00038 CATStreamFile(); 00039 00040 /// Destructor will close file handle if its unclosed, but 00041 /// will assert in debug mode if you do this. 00042 /// 00043 /// Please call Close() before destroying a file if you 00044 /// have opened it previously. 00045 /// \sa Close() 00046 virtual ~CATStreamFile(); 00047 00048 /// Open() opens a file from a pathname. 00049 /// 00050 /// Call close when done. 00051 /// 00052 /// \param pathname - ptr to string specifying the path. 00053 /// \param mode - combination of OPEN_MODE enumerated flags. 00054 /// 00055 /// \return CATResult - CAT_SUCCESS on success. 00056 /// \sa Close() 00057 virtual CATResult Open(const CATWChar* pathname, OPEN_MODE mode); 00058 00059 /// Close() closes a previously opened file. 00060 /// 00061 /// File must have been previously successfuly opened. 00062 /// 00063 /// \return CATResult - CAT_SUCCESS on success. 00064 /// \sa Open() 00065 virtual CATResult Close(); 00066 00067 /// IsOpen() returns true if the file has been opened, and false otherwise. 00068 /// 00069 /// \return bool - true if file is open. 00070 /// \sa Open(), Close() 00071 virtual bool IsOpen(); 00072 00073 /// Read() reads the requested amount of data into a buffer. 00074 /// 00075 /// Will read up to, but not necessarily, [length] bytes. 00076 /// On return, length is set to the number of bytes actually read. 00077 /// buffer *must* be large enough for max value of length. 00078 /// 00079 /// \param buffer - target buffer for read 00080 /// \param length - min(length of buffer, desired read length). 00081 /// Set to amount read on return. 00082 /// \return CATResult - CAT_SUCCESS on success 00083 /// \sa Write() 00084 virtual CATResult Read(void* buffer, CATUInt32& length); 00085 00086 /// Write() writes the requested amount of data from a buffer. 00087 /// 00088 /// Incomplete writes are treated as an error. 00089 /// 00090 /// \param buffer - source buffer to write from. 00091 /// \param length - length of data to write. 00092 /// \return CATResult - CAT_SUCCESS on success 00093 /// \sa Read() 00094 virtual CATResult Write(const void* buffer, CATUInt32 length); 00095 00096 /// Size() returns the size of the object in filesize. 00097 /// 00098 /// This may not be supported on all stream types. 00099 /// \param filesize - 64-bit length of file. 00100 /// \return CATResult - CAT_SUCCESS on success 00101 virtual CATResult Size(CATInt64& filesize); 00102 00103 /// IsSeekable() returns true for files. 00104 virtual bool IsSeekable(); 00105 00106 /// SeekRelative() seeks from current position to a 00107 /// relative location. 00108 /// 00109 /// \param offset - signed offset from current position 00110 /// \return CATResult - CAT_SUCCESS on success. 00111 /// \sa IsSeekable(), SeekAbsolute(), SeekFromEnd() 00112 virtual CATResult SeekRelative(CATInt32 offset); 00113 00114 00115 /// SeekAbsolute() seeks from the start of the file 00116 /// to an absolute position. 00117 /// 00118 /// \param position - unsigned absolute position to seek to. 00119 /// \return CATResult - CAT_SUCCESS on success. 00120 /// \sa IsSeekable(), SeekRelative(), SeekFromEnd() 00121 virtual CATResult SeekAbsolute(CATInt64 position); 00122 00123 /// SeekFromEnd() seeks from the end of the file. 00124 /// 00125 /// \param offset - signed offset from end of stream 00126 /// \return CATResult - CAT_SUCCESS on success. 00127 /// \sa IsSeekable(), SeekRelative(), SeekAbsolute() 00128 virtual CATResult SeekFromEnd(CATInt32 offset); 00129 00130 /// GetPosition() returns the current position in the stream 00131 /// in position. 00132 /// 00133 /// \param position - current position - set on successful return. 00134 /// \return CATResult - CAT_SUCCESS on success. 00135 /// \sa IsSeekable(), Size() 00136 virtual CATResult GetPosition(CATInt64& position); 00137 00138 /// GetName() retrieves the filename of the stream. 00139 virtual CATString GetName() const; 00140 00141 /// ReadAbs() reads from the specified location, but does 00142 /// not change the current stream position. 00143 /// 00144 /// ReadAbs() is mainly for use in substreams, and may not be 00145 /// available from all stream types. If you're not implementing 00146 /// it, then please return an error from your derived class. 00147 /// 00148 /// \param buffer - target buffer for read 00149 /// \param length - min(length of buffer, desired read length). 00150 /// set to amount read on return. 00151 /// \param position - position within stream to read. 00152 /// \return CATResult - CAT_SUCCESS on success. 00153 virtual CATResult ReadAbs(void *buffer, CATUInt32& length, CATInt64 position); 00154 00155 /// WriteAbs() Writes from the specified location, but does 00156 /// not change the current stream position. 00157 /// 00158 /// WriteAbs() is mainly for use in substreams, and may not be 00159 /// available from all stream types. If you're not implementing 00160 /// it, then please return an error from your derived class. 00161 /// 00162 /// \param buffer - target buffer for Write 00163 /// \param length - length of data to write 00164 /// \param position - position within stream to read. 00165 /// \return CATResult - CAT_SUCCESS on success. 00166 virtual CATResult WriteAbs(const void *buffer, CATUInt32 length, CATInt64 position); 00167 00168 protected: 00169 CATString fFilename; 00170 00171 private: 00172 CATStreamFile& operator=(const CATStreamFile& srcStream) 00173 { 00174 CATASSERT(false,"Copy operator not currently supported for files."); 00175 return *this; 00176 } 00177 00178 /// STDIO file handle 00179 FILE* fFileHandle; 00180 00181 /// Filename of current file. 00182 }; 00183 00184 00185 #endif // _CATStreamFile_H_