libdap  Updated for version 3.20.0
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 
55 namespace libdap {
56 
57 XDRFileMarshaller::XDRFileMarshaller(FILE *out) :
58  _sink(0)//, d_out(out)
59 {
60  _sink = new_xdrstdio(out, XDR_ENCODE);
61 }
62 
63 XDRFileMarshaller::XDRFileMarshaller() :
64  Marshaller(), _sink(0)//, d_out(0)
65 {
66  throw InternalErr( __FILE__, __LINE__, "Default constructor not implemented.");
67 }
68 
69 XDRFileMarshaller::XDRFileMarshaller(const XDRFileMarshaller &m) :
70  Marshaller(m), _sink(0)//, d_out(0)
71 {
72  throw InternalErr( __FILE__, __LINE__, "Copy constructor not implemented.");
73 }
74 
75 XDRFileMarshaller &
76 XDRFileMarshaller::operator=(const XDRFileMarshaller &)
77 {
78  throw InternalErr( __FILE__, __LINE__, "Copy operator not implemented.");
79 
80  return *this;
81 }
82 
83 XDRFileMarshaller::~XDRFileMarshaller()
84 {
85  delete_xdrstdio(_sink);
86 }
87 
88 void XDRFileMarshaller::put_byte(dods_byte val)
89 {
90  if (!xdr_char(_sink, (char *) &val))
91  throw Error(
92  "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.");
93 }
94 
95 void XDRFileMarshaller::put_int16(dods_int16 val)
96 {
97  if (!XDR_INT16(_sink, &val))
98  throw Error(
99  "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.");
100 }
101 
102 void XDRFileMarshaller::put_int32(dods_int32 val)
103 {
104  if (!XDR_INT32(_sink, &val))
105  throw Error(
106  "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.");
107 }
108 
109 void XDRFileMarshaller::put_float32(dods_float32 val)
110 {
111  if (!xdr_float(_sink, &val))
112  throw Error(
113  "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.");
114 }
115 
116 void XDRFileMarshaller::put_float64(dods_float64 val)
117 {
118  if (!xdr_double(_sink, &val))
119  throw Error(
120  "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.");
121 }
122 
123 void XDRFileMarshaller::put_uint16(dods_uint16 val)
124 {
125  if (!XDR_UINT16(_sink, &val))
126  throw Error("Network I/O Error. Could not send uint 16 data.");
127 }
128 
129 void XDRFileMarshaller::put_uint32(dods_uint32 val)
130 {
131  if (!XDR_UINT32(_sink, &val))
132  throw Error("Network I/O Error. Could not send uint 32 data.");
133 }
134 
135 void XDRFileMarshaller::put_str(const string &val)
136 {
137  const char *out_tmp = val.c_str();
138 
139  if (!xdr_string(_sink, (char **) &out_tmp, max_str_len))
140  throw Error("Network I/O Error. Could not send string data.");
141 }
142 
143 void XDRFileMarshaller::put_url(const string &val)
144 {
145  put_str(val);
146 }
147 
148 void XDRFileMarshaller::put_opaque(char *val, unsigned int len)
149 {
150  if (!xdr_opaque(_sink, val, len))
151  throw Error("Network I/O Error. Could not send opaque data.");
152 }
153 
154 void XDRFileMarshaller::put_int(int val)
155 {
156  if (!xdr_int(_sink, &val))
157  throw Error("Network I/O Error(1).");
158 }
159 
160 void XDRFileMarshaller::put_vector(char *val, int num, Vector &)
161 {
162  if (!val) throw InternalErr(__FILE__, __LINE__, "Buffer pointer is not set.");
163 
164  put_int(num);
165 
166  if (!xdr_bytes(_sink, (char **) &val, (unsigned int *) &num, DODS_MAX_ARRAY)) {
167  throw Error("Network I/O Error(2).");
168  }
169 }
170 
171 void XDRFileMarshaller::put_vector(char *val, int num, int width, Vector &vec)
172 {
173  if (!val) throw InternalErr(__FILE__, __LINE__, "Buffer pointer is not set.");
174 
175  put_int(num);
176 
177  BaseType *var = vec.var();
178  if (!xdr_array(_sink, (char **) &val, (unsigned int *) &num, DODS_MAX_ARRAY, width,
179  XDRUtils::xdr_coder(var->type()))) {
180  throw Error("Network I/O Error(2).");
181  }
182 }
183 
184 void XDRFileMarshaller::dump(ostream &strm) const
185 {
186  strm << DapIndent::LMarg << "XDRFileMarshaller::dump - (" << (void *) this << ")" << endl;
187 }
188 
189 } // namespace libdap
190 
virtual unsigned int width(bool constrained=false) const
Returns the width of the data, in bytes.
Definition: Vector.cc:544
BaseType(const string &n, const Type &t, bool is_dap4=false)
The BaseType constructor.
Definition: BaseType.cc:126
virtual void dump(ostream &strm) const
dump the contents of this object to the specified ostream
top level DAP object to house generic methods
Definition: AlarmHandler.h:35
virtual BaseType * var(const string &name="", bool exact_match=true, btp_stack *s=0)
Definition: Vector.cc:435
virtual BaseType * var(const string &name="", bool exact_match=true, btp_stack *s=0)
Returns a pointer to a member of a constructor class.
Definition: BaseType.cc:750
Vector(const string &n, BaseType *v, const Type &t, bool is_dap4=false)
The Vector constructor.
Definition: Vector.cc:250
static xdrproc_t xdr_coder(const Type &t)
Returns a function used to encode elements of an array.
Definition: XDRUtils.cc:145