Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
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 }