iDataStream.h
Go to the documentation of this file.
00001 /*
00002 imagen - Simple object-oriented imaging library
00003 Copyright (C) 2011-2012 Mark D. Procarione
00004 
00005 imagen is free software: you can redistribute it and/or
00006 modify it under the terms of the GNU Lesser General Public
00007 License as published by the Free Software Foundation, either
00008 version 3 of the License, or (at your option) any later version.
00009 
00010 imagen is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013 GNU General Public License for more details.
00014 */
00015 
00016 #ifndef __IDATASTREAM_H__
00017 #define __IDATASTREAM_H__
00018 
00019 #include "imagen.h"
00020 
00021 namespace imagen
00022 {
00024     class iDataStream
00025     {
00026         public:
00028             iDataStream() {};
00029 
00031             virtual ~iDataStream() {};
00032 
00034 
00035             virtual bool close() = 0;
00036 
00038 
00042                         virtual long read( void *data, long size, long count ) = 0;
00043 
00045 
00049             virtual long write( const void *data, long size, long count ) = 0;
00050 
00052 
00053             virtual int getc() = 0;
00054 
00056 
00057             virtual int putc( int chr ) = 0;
00058 
00060 
00061             virtual bool flush() = 0;
00062 
00064             virtual long size() const = 0;
00065 
00067                         virtual long tell() const = 0;
00068 
00070 
00073                         virtual bool seek( long offset, iDataSeek origin ) = 0;
00074 
00076 
00077                         virtual bool atEnd() const = 0;
00078     };
00079 
00081     class iFileStream : public iDataStream
00082     {
00083         public:
00085             iFileStream() : file(NULL), sz(0) {}
00086 
00088 
00090             iFileStream( const char *filename, iFileMode mode );
00091 
00093 
00095             ~iFileStream();
00096 
00098 
00101                         bool open( const char *filename, iFileMode mode );
00102 
00104 
00107                         bool close();
00108 
00109                         long read( void *data, long size, long count );
00110 
00111             long write( const void *data, long size, long count );
00112 
00113             int getc();
00114 
00115             int putc( int chr );
00116 
00118 
00121             bool flush();
00122 
00123             long size() const { return sz; }
00124 
00125                         long tell() const;
00126 
00127                         bool seek( long offset, iDataSeek origin );
00128 
00129                         bool atEnd() const;
00130 
00131         private:
00132             FILE *file;
00133             long sz;
00134     };
00135 
00137         class iMemoryStream : public iDataStream
00138         {
00139                 public:
00141                         iMemoryStream() { data = NULL; sz = 0; position = 0; delOnClose = false; }
00142 
00144 
00146                         iMemoryStream( void *bdata, long bsize );
00147 
00149                         ~iMemoryStream();
00150 
00152 
00154             void deleteOnClose( bool del ) { delOnClose = del; }
00155 
00157 
00160                         bool open( void *bdata, long bsize );
00161 
00163 
00166                         bool close();
00167 
00168                         long read( void *bdata, long size, long count );
00169 
00170                         long write( const void *bdata, long size, long count );
00171 
00172             int getc();
00173 
00174             int putc( int chr );
00175 
00177 
00179             bool flush();
00180 
00181             long size() const { return sz; }
00182 
00183                         long tell() const { return position; }
00184 
00185                         bool seek( long offset, iDataSeek origin );
00186 
00187                         bool atEnd() const;
00188 
00189         private:
00190                         void *data;
00191                         long sz;
00192                         long position;
00193                         bool delOnClose;
00194         };
00195 }
00196 
00197 #endif // __IDATASTREAM_H__
00198