OPeNDAP Hyrax Back End Server (BES)  Updated for version 3.8.3
PPTStreamBuf.cc
Go to the documentation of this file.
1 // PPTStreamBuf.cc
2 
3 // This file is part of bes, A C++ back-end server implementation framework
4 // for the OPeNDAP Data Access Protocol.
5 
6 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research
7 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 //
23 // You can contact University Corporation for Atmospheric Research at
24 // 3080 Center Green Drive, Boulder, CO 80301
25 
26 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
27 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
28 //
29 // Authors:
30 // pwest Patrick West <pwest@ucar.edu>
31 // jgarcia Jose Garcia <jgarcia@ucar.edu>
32 
33 #include <sys/types.h>
34 
35 #include <cstdio>
36 #include <unistd.h> // for sync
37 #include <sstream>
38 #include <iomanip>
39 #include <iostream>
40 
41 using std::ostringstream ;
42 using std::hex ;
43 using std::setw ;
44 using std::setfill ;
45 
46 #include "PPTStreamBuf.h"
47 #include "BESDebug.h"
48 
49 PPTStreamBuf::PPTStreamBuf( int fd, unsigned bufsize )
50  : d_bufsize( bufsize ),
51  d_buffer( 0 ),
52  count( 0 )
53 {
54  open( fd, bufsize ) ;
55 }
56 
58 {
59  if(d_buffer)
60  {
61  sync() ;
62  delete [] d_buffer ;
63  }
64 }
65 
66 void
67 PPTStreamBuf::open( int fd, unsigned bufsize )
68 {
69  d_fd = fd ;
70  d_bufsize = bufsize == 0 ? 1 : bufsize ;
71 
72  d_buffer = new char[d_bufsize] ;
73  setp( d_buffer, d_buffer + d_bufsize ) ;
74 }
75 
76 int
78 {
79  if( pptr() > pbase() )
80  {
81  ostringstream strm ;
82  strm << hex << setw( 7 ) << setfill( '0' ) << (unsigned int)(pptr() - pbase()) << "d" ;
83  string tmp_str = strm.str() ;
84  write( d_fd, tmp_str.c_str(), tmp_str.length() ) ;
85  count += write( d_fd, d_buffer, pptr() - pbase() ) ;
86  setp( d_buffer, d_buffer + d_bufsize ) ;
87  // If something doesn't look right try using fsync
88  fsync(d_fd);
89  }
90  return 0 ;
91 }
92 
93 int
95 {
96  sync() ;
97  if( c != EOF )
98  {
99  *pptr() = static_cast<char>(c) ;
100  pbump( 1 ) ;
101  }
102  return c ;
103 }
104 
105 void
107 {
108  sync() ;
109  ostringstream strm ;
110  /*
111  ostringstream xstrm ;
112  xstrm << "count=" << hex << setw( 7 ) << setfill( '0' ) << how_many() << ";" ;
113  string xstr = xstrm.str() ;
114  strm << hex << setw( 7 ) << setfill( '0' ) << (unsigned int)xstr.length() << "x" << xstr ;
115  */
116  strm << hex << setw( 7 ) << setfill( '0' ) << (unsigned int)0 << "d" ;
117  string tmp_str = strm.str() ;
118  BESDEBUG( "ppt", "PPTStreamBuf::finish - writing " << tmp_str << endl ) ;
119  write( d_fd, tmp_str.c_str(), tmp_str.length() ) ;
120  // If something doesn't look right try using fsync
121  fsync(d_fd);
122  count = 0 ;
123 }
124 
void open(int fd, unsigned bufsize=1)
Definition: PPTStreamBuf.cc:67
virtual ~PPTStreamBuf()
Definition: PPTStreamBuf.cc:57
int overflow(int c)
Definition: PPTStreamBuf.cc:94
#define BESDEBUG(x, y)
macro used to send debug information to the debug stream
Definition: BESDebug.h:64