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

00001 /*=========================================================================
00002 
00003   Library   : Image Registration Toolkit (IRTK)
00004   Module    : $Id: irtkCofstream.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 _IRTKCOFSTREAM_H
00014 
00015 #define _IRTKCOFSTREAM_H
00016 
00025 class irtkCofstream : public irtkObject
00026 {
00027 
00029   FILE *_uncompressedFile;
00030 
00031 #ifdef HAS_ZLIB
00033   gzFile _compressedFile;
00034 #endif
00035 
00036 protected:
00037 
00039   int _compressed;
00040 
00042   int _swapped;
00043 
00044 public:
00045 
00047   irtkCofstream();
00048 
00050   ~irtkCofstream();
00051 
00053   void Write(char *data, int offset, int length);
00054 
00056   void WriteAsChar  (char data, int offset = -1);
00058   void WriteAsChar  (char *data, int length, int offset = -1);
00059 
00061   void WriteAsUChar (unsigned char data, int offset = -1);
00063   void WriteAsUChar (unsigned char *data, int length, int offset = -1);
00064 
00066   void WriteAsShort (short data, int offset = -1);
00068   void WriteAsShort (short *data, int length, int offset = -1);
00069 
00071   void WriteAsUShort(unsigned short data, int offset = -1);
00073   void WriteAsUShort(unsigned short *data, int length, int offset = -1);
00074 
00076   void WriteAsInt   (int data, int offset = -1);
00078   void WriteAsInt   (int *data, int length, int offset = -1);
00079 
00081   void WriteAsUInt  (unsigned int data, int offset = -1);
00083   void WriteAsUInt  (unsigned int *data, int length, int offset = -1);
00084 
00086   void WriteAsFloat (float data, int offset = -1);
00088   void WriteAsFloat (float *data, int length, int offset = -1);
00089 
00091   void WriteAsDouble(double data, int offset = -1);
00093   void WriteAsDouble(double *data, int length, int offset = -1);
00094 
00096   void WriteAsString(char *data, int offset = -1);
00097 
00099   void Open(const char *);
00100 
00102   void Close();
00103 
00105   int  IsCompressed();
00106 
00108   void IsCompressed(int);
00109 
00111   int  IsSwapped();
00112 
00114   void IsSwapped(int);
00115 
00116 };
00117 
00118 inline void irtkCofstream::Open(const char *filename)
00119 {
00120   if (strstr(basename2(filename), ".gz") == NULL) {
00121     _compressed = False;
00122     _uncompressedFile = fopen(filename, "wb");
00123 
00124     // Check whether file was opened successful
00125     if (_uncompressedFile == NULL) {
00126       cerr << "cofstream::Open: Can't open file " << filename << endl;
00127       exit(1);
00128     }
00129   } else {
00130 #ifdef HAS_ZLIB
00131     _compressed = True;
00132     _compressedFile = gzopen(filename, "wb");
00133 
00134     // Check whether file was opened successful
00135     if (_compressedFile == NULL) {
00136       cerr << "cofstream::Open: Can't open file " << filename << endl;
00137       exit(1);
00138     }
00139 #else
00140     cerr << "cofstream::Open: Can't write compressed file without zlib" << endl;
00141     exit(1);
00142 #endif
00143   }
00144 }
00145 
00146 inline void irtkCofstream::Close()
00147 {
00148 #ifdef HAS_ZLIB
00149   if (_compressedFile != NULL) {
00150     gzclose(_compressedFile);
00151     _compressedFile = NULL;
00152   }
00153 #endif
00154   if (_uncompressedFile != NULL) {
00155     fclose(_uncompressedFile);
00156     _uncompressedFile = NULL;
00157   }
00158 }
00159 
00160 inline int irtkCofstream::IsCompressed()
00161 {
00162   return _compressed;
00163 }
00164 
00165 inline void irtkCofstream::IsCompressed(int compressed)
00166 {
00167   _compressed = compressed;
00168 }
00169 
00170 inline int irtkCofstream::IsSwapped()
00171 {
00172   return _swapped;
00173 }
00174 
00175 inline void irtkCofstream::IsSwapped(int swapped)
00176 {
00177   _swapped = swapped;
00178 }
00179 
00180 #endif
00181 
00182