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

Generated on 27 Oct 2009 for OPeNDAP Hyrax Back End Server (BES) by  doxygen 1.6.1