bes Updated for version 3.20.10
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
43using std::ostringstream;
44using std::hex;
45using std::setw;
46using std::setfill;
47
48#include "PPTStreamBuf.h"
49
50const char* eod_marker = "0000000d";
51const size_t eod_marker_len = 8;
52
53PPTStreamBuf::PPTStreamBuf(int fd, unsigned bufsize) : d_fd(fd),
54 d_bufsize(bufsize), d_buffer(nullptr), count(0)
55{
56 open(fd, bufsize);
57}
58
59PPTStreamBuf::~PPTStreamBuf()
60{
61 if (d_buffer) {
62 sync();
63 delete[] d_buffer;
64 }
65}
66
67void 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// We're stuck with this return type because this is inherited from stdc++ streambuf. jhrg
77int PPTStreamBuf::sync()
78{
79 if (pptr() > pbase()) {
80 ostringstream strm;
81 strm << hex << setw(7) << setfill('0') << (unsigned int) (pptr() - pbase()) << "d";
82 write(d_fd, strm.str().c_str(), strm.str().length());
83
84 count += write(d_fd, d_buffer, pptr() - pbase());
85 setp(d_buffer, d_buffer + d_bufsize);
86 }
87
88 return 0;
89}
90
91int PPTStreamBuf::overflow(int c)
92{
93 sync();
94 if (c != EOF) {
95 *pptr() = static_cast<char>(c);
96 pbump(1);
97 }
98 return c;
99}
100
101void PPTStreamBuf::finish()
102{
103 sync();
104
105 write(d_fd, eod_marker, eod_marker_len); // tmp_str.c_str(), tmp_str.length() ) ;
106
107 count = 0;
108}
109