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 <sys/types.h>
00034
00035 #include <unistd.h>
00036 #include <sstream>
00037 #include <iomanip>
00038 #include <iostream>
00039
00040 using std::ostringstream ;
00041 using std::hex ;
00042 using std::setw ;
00043 using std::setfill ;
00044
00045 #include "PPTStreamBuf.h"
00046 #include "PPTProtocol.h"
00047
00048 PPTStreamBuf::PPTStreamBuf( int fd, unsigned bufsize )
00049 : d_bufsize( bufsize ),
00050 d_buffer( 0 ),
00051 count( 0 )
00052 {
00053 open( fd, bufsize ) ;
00054 }
00055
00056 PPTStreamBuf::~PPTStreamBuf()
00057 {
00058 if(d_buffer)
00059 {
00060 sync() ;
00061 delete d_buffer ;
00062 }
00063 }
00064
00065 void
00066 PPTStreamBuf::open( int fd, unsigned bufsize )
00067 {
00068 d_fd = fd ;
00069 d_bufsize = bufsize == 0 ? 1 : bufsize ;
00070
00071 d_buffer = new char[d_bufsize] ;
00072 setp( d_buffer, d_buffer + d_bufsize ) ;
00073 }
00074
00075 int
00076 PPTStreamBuf::sync()
00077 {
00078 if( pptr() > pbase() )
00079 {
00080 ostringstream strm ;
00081 strm << hex << setw( 4 ) << setfill( '0' ) << (unsigned int)(pptr() - pbase()) << "d" ;
00082 string tmp_str = strm.str() ;
00083 write( d_fd, tmp_str.c_str(), tmp_str.length() ) ;
00084 count += write( d_fd, d_buffer, pptr() - pbase() ) ;
00085 setp( d_buffer, d_buffer + d_bufsize ) ;
00086
00087 fsync(d_fd);
00088 }
00089 return 0 ;
00090 }
00091
00092 int
00093 PPTStreamBuf::overflow( int c )
00094 {
00095 sync() ;
00096 if( c != EOF )
00097 {
00098 *pptr() = static_cast<char>(c) ;
00099 pbump( 1 ) ;
00100 }
00101 return c ;
00102 }
00103
00104 void
00105 PPTStreamBuf::finish()
00106 {
00107 sync() ;
00108 ostringstream strm ;
00109 ostringstream xstrm ;
00110 xstrm << "count=" << hex << setw( 8 ) << setfill( '0' ) << how_many() << ";" ;
00111 string xstr = xstrm.str() ;
00112 strm << hex << setw( 4 ) << setfill( '0' ) << (unsigned int)xstr.length() << "x" << xstr ;
00113 strm << hex << setw( 4 ) << setfill( '0' ) << (unsigned int)0 << "d" ;
00114 string tmp_str = strm.str() ;
00115 write( d_fd, tmp_str.c_str(), tmp_str.length() ) ;
00116
00117 fsync(d_fd);
00118 count = 0 ;
00119 }
00120