cmtkCompressedStreamPipe.cxx

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: 2728 $
00026 //
00027 //  $LastChangedDate: 2011-01-13 15:03:21 -0800 (Thu, 13 Jan 2011) $
00028 //
00029 //  $LastChangedBy: torstenrohlfing $
00030 //
00031 */
00032 
00033 #include "cmtkCompressedStream.h"
00034 
00035 #include <System/cmtkConsole.h>
00036 #include <System/cmtkExitException.h>
00037 
00038 #include <stdlib.h>
00039 #include <stdio.h>
00040 #include <limits.h>
00041 #include <errno.h>
00042 
00043 namespace
00044 cmtk
00045 {
00046 
00049 
00050 CompressedStream::Pipe::Pipe( const char* filename, const char* command )
00051 {
00052   char cmd[PATH_MAX];
00053 
00054 #ifndef _MSC_VER  
00055   if ( static_cast<size_t>( snprintf( cmd, sizeof( cmd ), command, filename ) ) >= sizeof( cmd ) )
00056     {
00057     StdErr << "WARNING: length of path exceeds system PATH_MAX in CompressedStream::OpenDecompressionPipe and will be truncated.\n";
00058     }
00059   errno = 0;
00060   
00061   this->m_File = popen( cmd, CMTK_FILE_MODE );
00062   if ( !this->m_File ) 
00063     {
00064     fprintf( stderr, "ERROR: popen() return NULL (errno=%d).\n", errno );
00065     perror( "System message" );
00066     throw 0;
00067     } 
00068 #else
00069   if ( snprintf( cmd, sizeof( cmd ), command, filename, tmpnam( this->m_TempName) ) >= sizeof( cmd ) )
00070     {
00071     StdErr << "WARNING: length of path exceeds system PATH_MAX in CompressedStream::OpenDecompressionPipe and will be truncated.\n";
00072     }
00073   
00074   _flushall();
00075   int sysReturn = system( cmd );
00076   
00077   if ( sysReturn ) 
00078     {
00079     fprintf( stderr, "Command %s returned %d\n", cmd, sysReturn );
00080     fprintf( stderr, "Errno = %d\n", errno );
00081     }
00082   
00083   this->m_File = fopen( this->m_TempName, CMTK_FILE_MODE);
00084   
00085   if ( !this->m_File )
00086     {
00087     throw 0;
00088     }
00089 #endif
00090 
00091   this->m_BytesRead = 0;
00092 }
00093 
00094 void 
00095 CompressedStream::Pipe::Close()
00096 {
00097 #ifndef _MSC_VER
00098   pclose( this->m_File );
00099 #else
00100   remove( this->m_TempName );
00101 #endif // # ifndef _MSC_VER
00102 }
00103 
00104 void
00105 CompressedStream::Pipe::Rewind() 
00106 {
00107   StdErr << "CompressedStream::Pipe::Rewind() is not implemented\n";
00108   throw ExitException( 1 );
00109 }
00110 
00111 size_t
00112 CompressedStream::Pipe::Read( void *data, size_t size, size_t count ) 
00113 {
00114   const size_t result = fread( data, size, count, this->m_File );
00115   this->m_BytesRead += result;
00116   return result / size;  
00117 }
00118 
00119 bool
00120 CompressedStream::Pipe::Get ( char &c)
00121 {
00122   const int data = fgetc( this->m_File );
00123   if ( data != EOF ) 
00124     {
00125     c=(char) data;
00126     ++this->m_BytesRead;
00127     return true;
00128     }
00129 
00130   return false;
00131 }
00132 
00133 int
00134 CompressedStream::Pipe::Tell () const 
00135 {
00136   return this->m_BytesRead;
00137 }
00138 
00139 bool
00140 CompressedStream::Pipe::Feof () const 
00141 {
00142   return (feof( this->m_File ) != 0);
00143 }
00144 
00145 } // namespace cmtk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines