/vol/vipdata/irtk/common++/include/irtkCifstream.h

00001 /*=========================================================================
00002 
00003   Library   : Image Registration Toolkit (IRTK)
00004   Module    : $Id: irtkCifstream.h 2 2008-12-23 12:40:14Z dr $
00005   Copyright : Imperial College, Department of Computing
00006               Visual Information Processing (VIP), 2008 onwards
00007   Date      : $Date: 2008-12-23 12:40:14 +0000 (Tue, 23 Dec 2008) $
00008   Version   : $Revision: 2 $
00009   Changes   : $Author: dr $
00010 
00011 =========================================================================*/
00012 
00013 #ifndef _IRTKCIFSTREAM_H
00014 
00015 #define _IRTKCIFSTREAM_H
00016 
00024 class irtkCifstream : public irtkObject
00025 {
00026 
00028 #ifdef HAS_ZLIB
00029   gzFile _file;
00030 #else
00031   FILE *_file;
00032 #endif
00033 
00034 #ifdef ENABLE_UNIX_COMPRESS
00036   long _pos;
00037 #endif
00038 
00039 protected:
00040 
00042   int _swapped;
00043 
00044 public:
00045 
00047   irtkCifstream();
00048 
00050   ~irtkCifstream();
00051 
00053   void Read(char *data, int length, int offset);
00054 
00056   void ReadAsChar  (char *data, int length, int offset = -1);
00057 
00059   void ReadAsUChar (unsigned char  *data, int length, int offset = -1);
00060 
00062   void ReadAsShort (short *data, int length, int offset = -1);
00063 
00065   void ReadAsUShort(unsigned short *data, int length, int offset = -1);
00066 
00068   void ReadAsInt   (int *data, int length, int offset = -1);
00069 
00071   void ReadAsUInt  (unsigned int *data, int length, int offset = -1);
00072 
00074   void ReadAsFloat (float *data, int length, int offset = -1);
00075 
00077   void ReadAsDouble(double *data, int length, int offset = -1);
00078 
00080   void ReadAsString(char *data, int length, int offset = -1);
00081 
00083   void Open(const char *);
00084 
00086   void Close();
00087 
00089   void Seek(long);
00090 
00092   long Tell();
00093 
00095   int  IsSwapped();
00096 
00098   void IsSwapped(int);
00099 
00100 };
00101 
00102 inline void irtkCifstream::Open(const char *filename)
00103 {
00104 #ifdef HAS_ZLIB
00105   _file = gzopen(filename, "rb");
00106 #else
00107   _file = fopen(filename, "rb");
00108 #endif
00109 
00110   // Check whether file was opened successful
00111   if (_file == NULL) {
00112     cerr << "cifstream::Open: Can't open file " << filename << endl;
00113     exit(1);
00114   }
00115 
00116 #ifdef ENABLE_UNIX_COMPRESS
00117   _pos = 0;
00118 #endif
00119 }
00120 
00121 inline void irtkCifstream::Close()
00122 {
00123   if (_file != NULL) {
00124 #ifdef HAS_ZLIB
00125     gzclose(_file);
00126 #else
00127     fclose(_file);
00128 #endif
00129     _file = NULL;
00130   }
00131 #ifdef ENABLE_UNIX_COMPRESS
00132   _pos = 0;
00133 #endif
00134 }
00135 
00136 inline int irtkCifstream::IsSwapped()
00137 {
00138   return _swapped;
00139 }
00140 
00141 inline void irtkCifstream::IsSwapped(int swapped)
00142 {
00143   _swapped = swapped;
00144 }
00145 
00146 inline long irtkCifstream::Tell()
00147 {
00148 #ifdef HAS_ZLIB
00149   return gztell(_file);
00150 #else
00151   return ftell(_file);
00152 #endif
00153 }
00154 
00155 inline void irtkCifstream::Seek(long offset)
00156 {
00157 #ifdef HAS_ZLIB
00158   gzseek(_file, offset, SEEK_SET);
00159 #else
00160   fseek(_file, offset, SEEK_SET);
00161 #endif
00162 #ifdef ENABLE_UNIX_COMPRESS
00163   _pos = offset;
00164 #endif
00165 }
00166 
00167 #endif