bes  Updated for version 3.20.13
PPTStreamBuf.cc
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 "config.h"
34 
35 #include <sys/types.h>
36 
37 #include <cstdio>
38 #include <unistd.h> // for sync
39 #include <sstream>
40 #include <iomanip>
41 #include <iostream>
42 
43 using std::ostringstream;
44 using std::hex;
45 using std::setw;
46 using std::setfill;
47 
48 #include "PPTStreamBuf.h"
49 
50 const char* eod_marker = "0000000d";
51 const size_t eod_marker_len = 8;
52 
53 PPTStreamBuf::PPTStreamBuf(int fd, unsigned bufsize) : d_bufsize(bufsize), d_fd(fd)
54 {
55  open(fd, bufsize);
56 }
57 
58 PPTStreamBuf::~PPTStreamBuf()
59 {
60  if (d_buffer) {
61  sync();
62  delete[] d_buffer;
63  }
64 }
65 
66 void PPTStreamBuf::open(int fd, unsigned bufsize)
67 {
68  d_fd = fd;
69  d_bufsize = bufsize == 0 ? 1 : bufsize;
70 
71  d_buffer = new char[d_bufsize];
72  setp(d_buffer, d_buffer + d_bufsize);
73 }
74 
75 // We're stuck with this return type because this is inherited from stdc++ streambuf. jhrg
76 int PPTStreamBuf::sync()
77 {
78  if (pptr() > pbase()) {
79  ostringstream strm;
80  strm << hex << setw(7) << setfill('0') << (unsigned int) (pptr() - pbase()) << "d";
81  write(d_fd, strm.str().c_str(), strm.str().length());
82 
83  count += write(d_fd, d_buffer, pptr() - pbase());
84  setp(d_buffer, d_buffer + d_bufsize);
85  }
86 
87  return 0;
88 }
89 
90 int PPTStreamBuf::overflow(int c)
91 {
92  sync();
93  if (c != EOF) {
94  *pptr() = static_cast<char>(c);
95  pbump(1);
96  }
97  return c;
98 }
99 
100 void PPTStreamBuf::finish()
101 {
102  sync();
103 
104  write(d_fd, eod_marker, eod_marker_len);
105 
106  count = 0;
107 }
108