cmtkCompressedStream.h

Go to the documentation of this file.
00001 /*
00002 //
00003 //  Copyright 1997-2009 Torsten Rohlfing
00004 //
00005 //  Copyright 2004-2011 SRI International
00006 //
00007 //  This file is part of the Computational Morphometry Toolkit.
00008 //
00009 //  http://www.nitrc.org/projects/cmtk/
00010 //
00011 //  The Computational Morphometry Toolkit is free software: you can
00012 //  redistribute it and/or modify it under the terms of the GNU General Public
00013 //  License as published by the Free Software Foundation, either version 3 of
00014 //  the License, or (at your option) any later version.
00015 //
00016 //  The Computational Morphometry Toolkit is distributed in the hope that it
00017 //  will be useful, but WITHOUT ANY WARRANTY; without even the implied
00018 //  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 //  GNU General Public License for more details.
00020 //
00021 //  You should have received a copy of the GNU General Public License along
00022 //  with the Computational Morphometry Toolkit.  If not, see
00023 //  <http://www.gnu.org/licenses/>.
00024 //
00025 //  $Revision: 2752 $
00026 //
00027 //  $LastChangedDate: 2011-01-17 11:33:31 -0800 (Mon, 17 Jan 2011) $
00028 //
00029 //  $LastChangedBy: torstenrohlfing $
00030 //
00031 */
00032 
00033 #ifndef __cmtkCompressedStream_h_included_
00034 #define __cmtkCompressedStream_h_included_
00035 
00036 #include <cmtkconfig.h>
00037 
00038 #include <System/cmtkCannotBeCopied.h>
00039 
00040 #include <System/cmtkException.h>
00041 #include <System/cmtkSmartPtr.h>
00042 
00043 #include <stdio.h>
00044 
00045 #include <zlib.h>
00046 
00047 #ifdef CMTK_USE_BZIP2
00048 #  include <bzlib.h>
00049 #endif
00050 
00051 #ifdef CMTK_USE_LZMA
00052 #  include <lzmadec.h>
00053 #endif
00054 
00055 #ifdef HAVE_SYS_STAT_H
00056 #  include <sys/stat.h>
00057 #endif
00058 
00059 #include <string>
00060 
00061 #if defined(_MSC_VER)
00062 #define CMTK_FILE_MODE "rb"
00063 #elif defined(__linux__)
00064 #define CMTK_FILE_MODE "r"
00065 #else
00066 #define CMTK_FILE_MODE "rb"
00067 #endif
00068 
00069 namespace
00070 cmtk
00071 {
00072 
00075 
00077 class CompressedStream :
00079   private CannotBeCopied
00080 {
00081 public:
00083   typedef CompressedStream Self;
00084   
00086   CompressedStream() : m_Reader( NULL ) {};
00087   
00089   CompressedStream ( const char *filename );
00090   
00092   ~CompressedStream ();
00093   
00095   bool IsValid() const
00096   {
00097     return (this->m_Reader != NULL); 
00098   }
00099   
00101   bool IsCompressed() const
00102   {
00103     return this->m_Compressed;
00104   }
00105   
00107   bool Open( const char *filename );
00108   
00110   void Close();
00111   
00114   void Rewind()
00115   {
00116     this->m_Reader->Rewind();
00117   }
00118 
00127   int Seek ( const long int offset, int whence )
00128   {
00129     return this->m_Reader->Seek( offset, whence );
00130   }
00131 
00133   size_t Read ( void *data, size_t size, size_t count )
00134   {
00135     return this->m_Reader->Read( data, size, count );
00136   }
00137 
00139   int Get ( char &c)
00140   {
00141     return this->m_Reader->Get( c );
00142   }
00143 
00145   int Tell () const
00146   {
00147     return this->m_Reader->Tell();
00148   }
00149 
00151   bool Feof () const
00152   {
00153     return this->m_Reader->Feof();
00154   }
00155 
00158   static std::string GetBaseName( const std::string& path );
00159 
00166   static int Stat( const char *path, struct stat *const buf = NULL );
00167 
00168 private:
00181   bool OpenDecompressionPipe ( const char* filename, const char* suffix, const char* command, const char* compressedSuffix );
00182 
00185   typedef struct 
00186   {
00188     const char *suffix;
00189     
00193     const char *command;
00194   } ArchiveLookupEntry;
00195   
00197   static const ArchiveLookupEntry ArchiveLookup[];
00198 
00199 private:
00201   class ReaderBase
00202   {
00203   public:
00205     typedef ReaderBase Self;
00206     
00208     typedef SmartPointer<Self> SmartPtr;
00209 
00211     ReaderBase() : m_BytesRead( 0 ) {}
00212 
00214     virtual void Close() = 0;
00215 
00217     virtual void Rewind()
00218     {
00219       this->m_BytesRead = 0;
00220     }
00221     
00230     virtual int Seek ( const long int offset, int whence );
00231     
00233     virtual size_t Read ( void *data, size_t size, size_t count ) = 0;
00234     
00236     virtual bool Get ( char &c) = 0;
00237     
00239     virtual int Tell () const = 0;
00240     
00242     virtual bool Feof () const = 0;
00243 
00244   private:
00246     static const size_t SeekBlockSize = 8192;
00247 
00248   protected:
00250     size_t m_BytesRead;
00251   };
00252 
00254   class File
00255     : public ReaderBase
00256   {
00257   public:
00259     typedef File Self;
00260     
00262     typedef SmartPointer<Self> SmartPtr;
00263 
00265     File( const char *filename );
00266     
00268     virtual void Close();
00269     
00271     virtual void Rewind();
00272     
00281     virtual int Seek ( const long int offset, int whence );
00282     
00284     virtual size_t Read ( void *data, size_t size, size_t count );
00285     
00287     virtual bool Get ( char &c);
00288     
00290     virtual int Tell () const;
00291     
00293     virtual bool Feof () const;
00294 
00295   private:
00297     FILE* m_File;    
00298   };
00299 
00301   class Pipe
00302     : public ReaderBase
00303   {
00304   public:
00306     typedef Pipe Self;
00307     
00309     typedef SmartPointer<Self> SmartPtr;
00310 
00312     Pipe( const char* filename, const char* command );
00313     
00315     virtual void Close();
00316     
00318     virtual void Rewind();
00319     
00321     virtual size_t Read ( void *data, size_t size, size_t count );
00322     
00324     virtual bool Get ( char &c);
00325     
00327     virtual int Tell () const;
00328     
00330     virtual bool Feof () const;
00331 
00332   private:
00334     FILE* m_File;    
00335     
00336 #ifdef _MSC_VER
00337 
00342     char m_TempName[256];
00343 #endif
00344   };
00345 
00347   class Zlib 
00348     : public ReaderBase
00349   {
00350   public:
00352     typedef Zlib Self;
00353     
00355     typedef SmartPointer<Self> SmartPtr;
00356 
00358     Zlib( const char *filename );
00359     
00361     virtual void Close();
00362     
00364     virtual void Rewind();
00365     
00374     virtual int Seek ( const long int offset, int whence );
00375     
00377     virtual size_t Read ( void *data, size_t size, size_t count );
00378     
00380     virtual bool Get ( char &c );
00381     
00383     virtual int Tell () const;
00384     
00386     virtual bool Feof () const;
00387 
00388   private:
00390     gzFile m_GzFile;    
00391   };
00392 
00393 #ifdef CMTK_USE_BZIP2
00394 
00395   class BZip2 
00396     : public ReaderBase
00397   {
00398   public:
00400     typedef BZip2 Self;
00401     
00403     typedef SmartPointer<Self> SmartPtr;
00404 
00406     BZip2( const char *filename );
00407     
00409     virtual void Close();
00410     
00412     virtual void Rewind();
00413     
00415     virtual size_t Read ( void *data, size_t size, size_t count );
00416     
00418     virtual bool Get ( char &c);
00419     
00421     virtual int Tell () const;
00422     
00424     virtual bool Feof () const;
00425 
00426   private:
00428     BZFILE* m_BzFile;    
00429 
00431     int m_BzError;
00432   };
00433 #endif
00434 
00435 #ifdef CMTK_USE_LZMA
00436 
00437   class LZMA 
00438     : public ReaderBase
00439   {
00440   public:
00442     typedef LZMA Self;
00443     
00445     typedef SmartPointer<Self> SmartPtr;
00446 
00448     LZMA( const char *filename );
00449     
00451     virtual void Close();
00452     
00454     virtual void Rewind();
00455     
00457     virtual size_t Read ( void *data, size_t size, size_t count );
00458     
00460     virtual bool Get ( char &c);
00461     
00463     virtual int Tell () const;
00464     
00466     virtual bool Feof () const;
00467 
00468   private:
00470     lzmadec_FILE* m_File;    
00471   };
00472 #endif // #ifdef CMTK_USE_LZMA
00473 
00475   ReaderBase::SmartPtr m_Reader;
00476 
00478   bool m_Compressed;
00479 };
00480 
00482 
00483 } // namespace cmtk
00484 
00485 #endif // #ifndef __cmtkCompressedStream_h_included_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines