libdap Updated for version 3.20.10
libdap4 is an implementation of OPeNDAP's DAP protocol.
XDRFileMarshaller.cc
1// XDRFileMarshaller.cc
2
3// -*- mode: c++; c-basic-offset:4 -*-
4
5// This file is part of libdap, A C++ implementation of the OPeNDAP Data
6// Access Protocol.
7
8// Copyright (c) 2002,2003 OPeNDAP, Inc.
9// Author: Patrick West <pwest@ucar.edu>
10//
11// This library is free software; you can redistribute it and/or
12// modify it under the terms of the GNU Lesser General Public
13// License as published by the Free Software Foundation; either
14// version 2.1 of the License, or (at your option) any later version.
15//
16// This library is distributed in the hope that it will be useful,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19// Lesser General Public License for more details.
20//
21// You should have received a copy of the GNU Lesser General Public
22// License along with this library; if not, write to the Free Software
23// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24//
25// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
26
27// (c) COPYRIGHT URI/MIT 1994-1999
28// Please read the full copyright statement in the file COPYRIGHT_URI.
29//
30// Authors:
31// pwest Patrick West <pwest@ucar.edu>
32
33#include "config.h"
34
35#include "XDRFileMarshaller.h"
36
37#include "Byte.h"
38#include "Int16.h"
39#include "UInt16.h"
40#include "Int32.h"
41#include "UInt32.h"
42#include "Float32.h"
43#include "Float64.h"
44#include "Str.h"
45#include "Url.h"
46#include "Array.h"
47#include "Structure.h"
48#include "Sequence.h"
49#include "Grid.h"
50
51#include "util.h"
52#include "InternalErr.h"
53#include "DapIndent.h"
54
55namespace libdap {
56
57XDRFileMarshaller::XDRFileMarshaller(FILE *out) :
58 _sink(0)//, d_out(out)
59{
60 _sink = new_xdrstdio(out, XDR_ENCODE);
61}
62
63XDRFileMarshaller::XDRFileMarshaller() :
64 Marshaller(), _sink(0)//, d_out(0)
65{
66 throw InternalErr( __FILE__, __LINE__, "Default constructor not implemented.");
67}
68
69XDRFileMarshaller::XDRFileMarshaller(const XDRFileMarshaller &m) :
70 Marshaller(m), _sink(0)//, d_out(0)
71{
72 throw InternalErr( __FILE__, __LINE__, "Copy constructor not implemented.");
73}
74
75XDRFileMarshaller &
76XDRFileMarshaller::operator=(const XDRFileMarshaller &)
77{
78 throw InternalErr( __FILE__, __LINE__, "Copy operator not implemented.");
79}
80
81XDRFileMarshaller::~XDRFileMarshaller()
82{
83 delete_xdrstdio(_sink);
84}
85
86void XDRFileMarshaller::put_byte(dods_byte val)
87{
88 if (!xdr_char(_sink, (char *) &val))
89 throw Error(
90 "Network I/O Error. Could not send byte data.\nThis may be due to a bug in DODS, on the server or a\nproblem with the network connection.");
91}
92
93void XDRFileMarshaller::put_int16(dods_int16 val)
94{
95 if (!XDR_INT16(_sink, &val))
96 throw Error(
97 "Network I/O Error. Could not send int 16 data.\nThis may be due to a bug in libdap, on the server or a\nproblem with the network connection.");
98}
99
100void XDRFileMarshaller::put_int32(dods_int32 val)
101{
102 if (!XDR_INT32(_sink, &val))
103 throw Error(
104 "Network I/O Error. Could not read int 32 data.\nThis may be due to a bug in libdap, on the server or a\nproblem with the network connection.");
105}
106
107void XDRFileMarshaller::put_float32(dods_float32 val)
108{
109 if (!xdr_float(_sink, &val))
110 throw Error(
111 "Network I/O Error. Could not send float 32 data.\nThis may be due to a bug in libdap, on the server or a\nproblem with the network connection.");
112}
113
114void XDRFileMarshaller::put_float64(dods_float64 val)
115{
116 if (!xdr_double(_sink, &val))
117 throw Error(
118 "Network I/O Error. Could not send float 64 data.\nThis may be due to a bug in libdap, on the server or a\nproblem with the network connection.");
119}
120
121void XDRFileMarshaller::put_uint16(dods_uint16 val)
122{
123 if (!XDR_UINT16(_sink, &val))
124 throw Error("Network I/O Error. Could not send uint 16 data.");
125}
126
127void XDRFileMarshaller::put_uint32(dods_uint32 val)
128{
129 if (!XDR_UINT32(_sink, &val))
130 throw Error("Network I/O Error. Could not send uint 32 data.");
131}
132
133void XDRFileMarshaller::put_str(const string &val)
134{
135 const char *out_tmp = val.c_str();
136
137 if (!xdr_string(_sink, (char **) &out_tmp, max_str_len))
138 throw Error("Network I/O Error. Could not send string data.");
139}
140
141void XDRFileMarshaller::put_url(const string &val)
142{
143 put_str(val);
144}
145
146void XDRFileMarshaller::put_opaque(char *val, unsigned int len)
147{
148 if (!xdr_opaque(_sink, val, len))
149 throw Error("Network I/O Error. Could not send opaque data.");
150}
151
152void XDRFileMarshaller::put_int(int val)
153{
154 if (!xdr_int(_sink, &val))
155 throw Error("Network I/O Error(1).");
156}
157
158void XDRFileMarshaller::put_vector(char *val, int num, Vector &)
159{
160 if (!val) throw InternalErr(__FILE__, __LINE__, "Buffer pointer is not set.");
161
162 put_int(num);
163
164 if (!xdr_bytes(_sink, (char **) &val, (unsigned int *) &num, DODS_MAX_ARRAY)) {
165 throw Error("Network I/O Error(2).");
166 }
167}
168
169void XDRFileMarshaller::put_vector(char *val, int num, int width, Vector &vec)
170{
171 if (!val) throw InternalErr(__FILE__, __LINE__, "Buffer pointer is not set.");
172
173 put_int(num);
174
175 BaseType *var = vec.var();
176 if (!xdr_array(_sink, (char **) &val, (unsigned int *) &num, DODS_MAX_ARRAY, width,
177 XDRUtils::xdr_coder(var->type()))) {
178 throw Error("Network I/O Error(2).");
179 }
180}
181
182void XDRFileMarshaller::dump(ostream &strm) const
183{
184 strm << DapIndent::LMarg << "XDRFileMarshaller::dump - (" << (void *) this << ")" << endl;
185}
186
187} // namespace libdap
188
top level DAP object to house generic methods