• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

spl/io/FileStream.h

00001 /*
00002  *   This file is part of the Standard Portable Library (SPL).
00003  *
00004  *   SPL is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   SPL is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with SPL.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 #include <spl/io/Stream.h>
00018 #ifdef _stream2_h
00019 /* _stream2_h is in Stream.h, only compile this file if using the new Stream.h header */
00020 
00021 #ifndef _filestream_h
00022 #define _filestream_h
00023 
00024 #include <spl/io/File.h>
00025 #include <spl/RefCountPtr.h>
00026 
00033 class FdStream;
00034 typedef RefCountPtrCast<FdStream, spl::IStream, spl::IStreamPtr> FdStreamPtr;
00035 typedef WeakReference<FdStream, spl::IStreamPtr> FdStreamRef;
00036 
00037 REGISTER_TYPEOF(92, FdStreamPtr);
00038 REGISTER_TYPEOF( 453, FdStreamRef );
00039 
00043 class FdStream : public spl::IStream
00044 {
00045 protected:
00046         IStreamState *m_state;
00047         String m_filepathname;
00048         int m_fd;
00049         bool m_canClose;
00050         File::FileMode m_mode;
00051         File::FileAccess m_access;
00052         int m_lasterr;
00053 
00054 public:
00055         FdStream(int fd, File::FileAccess access, bool canClose = false);
00056         FdStream(const String& path, File::FileMode mode, File::FileAccess access);
00057         virtual ~FdStream();
00058 
00059         virtual void Close();
00060         virtual void Flush();
00061         virtual int Read(Array<byte>& buffer, const int offset, int count);
00062         virtual int ReadByte();
00063         virtual long Seek(const long offset, const SeekOrigin origin);
00064         virtual void Write(const Array<byte>& buffer, const int offset, const int count);
00065         virtual void WriteByte(byte value);
00066 
00067         virtual bool CanRead() const;
00068         virtual bool CanSeek() const;
00069         virtual bool CanWrite() const;
00070 
00071         virtual long Length() const;
00072         virtual long Position() const;
00073 
00074         friend class FdStreamState_Open;
00075 
00076 #ifdef DEBUG
00077         virtual void ValidateMem() const;
00078         virtual void CheckMem() const;
00079 #endif
00080 };
00081 
00082 REGISTER_TYPEOF(94, FdStream);
00083 
00084 class FileStream;
00085 typedef RefCountPtrCast<FileStream, spl::IStream, spl::IStreamPtr> FileStreamPtr;
00086 typedef WeakReference<FileStream, spl::IStreamPtr> FileStreamRef;
00087 
00088 REGISTER_TYPEOF(96, FileStreamPtr);
00089 REGISTER_TYPEOF( 454, FileStreamRef );
00090 
00094 class FileStream : public spl::IStream
00095 {
00096 private:
00097         void Init();
00098 
00099 protected:
00100         IStreamState *m_state;
00101         String m_filepathname;
00102         FILE *m_fp;
00103         bool m_canClose;
00104         File::FileMode m_mode;
00105         File::FileAccess m_access;
00106         int m_lasterr;
00107 
00108 public:
00109         FileStream(FILE *fp, File::FileAccess access, bool canClose = false);
00110         FileStream(const char *path, File::FileMode mode, File::FileAccess access);
00111         FileStream(const String& path, File::FileMode mode, File::FileAccess access);
00112         virtual ~FileStream();
00113 
00114         virtual void Close();
00115         virtual void Flush();
00116         virtual int Read(Array<byte>& buffer, const int offset, int count);
00117         virtual int ReadByte();
00118         virtual long Seek(const long offset, const SeekOrigin origin);
00119         virtual void Write(const Array<byte>& buffer, const int offset, const int count);
00120         virtual void WriteByte(byte value);
00121 
00122         virtual bool CanRead() const;
00123         virtual bool CanSeek() const;
00124         virtual bool CanWrite() const;
00125 
00126         virtual long Length() const;
00127         virtual long Position() const;
00128 
00129         friend class FileStreamState_Open;
00130 
00131 #ifdef DEBUG
00132         virtual void ValidateMem() const;
00133         virtual void CheckMem() const;
00134 #endif
00135 };
00136 
00137 REGISTER_TYPEOF(98, FileStream);
00138 
00141 #endif
00142 #endif