PPTStreamBuf.cc

Go to the documentation of this file.
00001 // PPTStreamBuf.cc
00002 
00003 // This file is part of bes, A C++ back-end server implementation framework
00004 // for the OPeNDAP Data Access Protocol.
00005 
00006 // Copyright (c) 2004,2005 University Corporation for Atmospheric Research
00007 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
00008 //
00009 // This library is free software; you can redistribute it and/or
00010 // modify it under the terms of the GNU Lesser General Public
00011 // License as published by the Free Software Foundation; either
00012 // version 2.1 of the License, or (at your option) any later version.
00013 // 
00014 // This library is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 // Lesser General Public License for more details.
00018 // 
00019 // You should have received a copy of the GNU Lesser General Public
00020 // License along with this library; if not, write to the Free Software
00021 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 //
00023 // You can contact University Corporation for Atmospheric Research at
00024 // 3080 Center Green Drive, Boulder, CO 80301
00025  
00026 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
00027 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
00028 //
00029 // Authors:
00030 //      pwest       Patrick West <pwest@ucar.edu>
00031 //      jgarcia     Jose Garcia <jgarcia@ucar.edu>
00032 
00033 #include <sys/types.h>
00034 //#include <sys/uio.h>
00035 #include <unistd.h> // for sync
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         // If something doesn't look right try using fsync
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     // If something doesn't look right try using fsync
00117     fsync(d_fd);
00118     count = 0 ;
00119 }
00120 

Generated on Tue Mar 4 23:13:37 2008 for OPeNDAP Back End Server (BES) by  doxygen 1.5.1