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