libdap  Updated for version 3.20.1
libdap4 is an implementation of OPeNDAP's DAP protocol.
XDRFileUnMarshaller.cc
1 // XDRFileUnMarshaller.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 "XDRFileUnMarshaller.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 XDRFileUnMarshaller::XDRFileUnMarshaller( FILE *out )
58  : _source( 0 )
59 {
60  _source = new_xdrstdio( out, XDR_DECODE ) ;
61 }
62 
63 XDRFileUnMarshaller::XDRFileUnMarshaller()
64  : UnMarshaller(), _source( 0 )
65 {
66  throw InternalErr( __FILE__, __LINE__, "Default constructor not implemented." ) ;
67 }
68 
69 XDRFileUnMarshaller::XDRFileUnMarshaller( const XDRFileUnMarshaller &um )
70  : UnMarshaller( um ), _source( 0 )
71 {
72  throw InternalErr( __FILE__, __LINE__, "Copy constructor not implemented." ) ;
73 }
74 
75 XDRFileUnMarshaller &
76 XDRFileUnMarshaller::operator=( const XDRFileUnMarshaller & )
77 {
78  throw InternalErr( __FILE__, __LINE__, "Copy operator not implemented." ) ;
79 
80  return *this ;
81 }
82 
83 XDRFileUnMarshaller::~XDRFileUnMarshaller( )
84 {
85  // Some static code analysis tools complain that delete_xdrstdio
86  // does not close the FILE* it holds, but that's not true with
87  // modern XDR libraries. Don't try to close that FILE*. jhrg 8/27/13
88 
89  delete_xdrstdio( _source ) ;
90 }
91 
92 void
93 XDRFileUnMarshaller::get_byte( dods_byte &val )
94 {
95  if( !xdr_char( _source, (char *)&val ) )
96  throw Error("Network I/O Error. Could not read byte data.");
97 }
98 
99 void
100 XDRFileUnMarshaller::get_int16( dods_int16 &val )
101 {
102  if( !XDR_INT16( _source, &val ) )
103  throw Error("Network I/O Error. Could not read int 16 data.");
104 }
105 
106 void
107 XDRFileUnMarshaller::get_int32( dods_int32 &val )
108 {
109  if( !XDR_INT32( _source, &val ) )
110  throw Error("Network I/O Error. Could not read int 32 data.");
111 }
112 
113 void
114 XDRFileUnMarshaller::get_float32( dods_float32 &val )
115 {
116  if( !xdr_float( _source, &val ) )
117  throw Error("Network I/O Error. Could not read float 32 data.");
118 }
119 
120 void
121 XDRFileUnMarshaller::get_float64( dods_float64 &val )
122 {
123  if( !xdr_double( _source, &val ) )
124  throw Error("Network I/O Error.Could not read float 64 data.");
125 }
126 
127 void
128 XDRFileUnMarshaller::get_uint16( dods_uint16 &val )
129 {
130  if( !XDR_UINT16( _source, &val ) )
131  throw Error("Network I/O Error. Could not read uint 16 data.");
132 }
133 
134 void
135 XDRFileUnMarshaller::get_uint32( dods_uint32 &val )
136 {
137  if( !XDR_UINT32( _source, &val ) )
138  throw Error("Network I/O Error. Could not read uint 32 data.");
139 }
140 
141 void
142 XDRFileUnMarshaller::get_str( string &val )
143 {
144  char *in_tmp = NULL ;
145 
146  if( !xdr_string( _source, &in_tmp, max_str_len ) )
147  throw Error("Network I/O Error. Could not read string data.");
148 
149  val = in_tmp ;
150 
151  free( in_tmp ) ;
152 }
153 
154 void
155 XDRFileUnMarshaller::get_url( string &val )
156 {
157  get_str( val ) ;
158 }
159 
160 void
161 XDRFileUnMarshaller::get_opaque( char *val, unsigned int len )
162 {
163  xdr_opaque( _source, val, len ) ;
164 }
165 
166 void
167 XDRFileUnMarshaller::get_int( int &val )
168 {
169  if( !xdr_int( _source, &val ) )
170  throw Error("Network I/O Error(1). This may be due to a bug in libdap or a\nproblem with the network connection.");
171 }
172 
173 void
174 XDRFileUnMarshaller::get_vector( char **val, unsigned int &num, Vector & )
175 {
176  if( !xdr_bytes( _source, val, &num, DODS_MAX_ARRAY) )
177  throw Error("Network I/O error (1).");
178 }
179 
180 void
181 XDRFileUnMarshaller::get_vector( char **val, unsigned int &num, int width, Vector &vec )
182 {
183  BaseType *var = vec.var() ;
184 
185  if( !xdr_array( _source, val, &num, DODS_MAX_ARRAY, width,
186  XDRUtils::xdr_coder( var->type() ) ) )
187  {
188  throw Error("Network I/O error (2).");
189  }
190 }
191 
192 void
193 XDRFileUnMarshaller::dump(ostream &strm) const
194 {
195  strm << DapIndent::LMarg << "XDRFileUnMarshaller::dump - ("
196  << (void *)this << ")" << endl ;
197 }
198 
199 } // namespace libdap
200 
top level DAP object to house generic methods
Definition: AlarmHandler.h:35
virtual void dump(ostream &strm) const
dump the contents of this object to the specified ostream
static xdrproc_t xdr_coder(const Type &t)
Returns a function used to encode elements of an array.
Definition: XDRUtils.cc:145