libdap++  Updated for version 3.12.0
XDRUtils.cc
Go to the documentation of this file.
1 // XDRUtils.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 "XDRUtils.h"
34 #include "debug.h"
35 #include "Str.h"
36 
37 using namespace libdap ;
38 
39 // This function is used to allocate memory for, and initialize, a new XDR
40 // pointer. It sets the stream associated with the (XDR *) to STREAM.
41 //
42 // NB: STREAM is not one of the C++/libg++ iostream classes; it is a (FILE
43 // *).
44 
45 // These func's moved to xdrutil_ppc.* under the PPC as explained there
46 #ifndef __POWERPC__
47 XDR *
48 new_xdrstdio(FILE *stream, enum xdr_op xop)
49 {
50  XDR *xdr = new XDR;
51 
52  xdrstdio_create(xdr, stream, xop);
53 
54  return xdr;
55 }
56 
57 XDR *
58 set_xdrstdio(XDR *xdr, FILE *stream, enum xdr_op xop)
59 {
60  xdrstdio_create(xdr, stream, xop);
61 
62  return xdr;
63 }
64 
65 // Delete an XDR pointer allocated using the above function. Do not close the
66 // associated FILE pointer.
67 
68 void
69 delete_xdrstdio(XDR *xdr)
70 {
71  xdr_destroy(xdr);
72 
73  delete xdr; xdr = 0;
74 }
75 #endif
76 
77 // This function is used to en/decode Str and Url type variables. It is
78 // defined as extern C since it is passed via function pointers to routines
79 // in the xdr library where it is executed. This function is defined so
80 // that Str and Url have an en/decoder which takes exactly two arguments: an
81 // XDR * and a string reference.
82 //
83 // NB: this function is *not* used for arrays (i.e., it is not the function
84 // referenced by BaseType's _xdr_coder field when the object is a Str or Url.
85 // Also note that \e max_str_len is an obese number but that really does not
86 // matter; xdr_string() would never actually allocate that much memory unless
87 // a string that size was sent from the server.
88 // Returns: XDR's bool_t; TRUE if no errors are detected, FALSE
89 // otherwise. The formal parameter BUF is modified as a side effect.
90 
91 extern "C" bool_t
92 xdr_str(XDR *xdrs, string &buf)
93 {
94  DBG(cerr << "In xdr_str, xdrs: " << xdrs << endl);
95 
96  switch (xdrs->x_op) {
97  case XDR_ENCODE: { // BUF is a pointer to a (string *)
98  const char *out_tmp = buf.c_str();
99 
100  return xdr_string(xdrs, (char **)&out_tmp, max_str_len);
101  }
102 
103  case XDR_DECODE: {
104  char *in_tmp = NULL;
105 
106  bool_t stat = xdr_string(xdrs, &in_tmp, max_str_len);
107  if (!stat)
108  return stat;
109 
110  buf = in_tmp;
111 
112  free(in_tmp);
113 
114  return stat;
115  }
116 
117  default:
118  return 0;
119  }
120 }
121 
122 namespace libdap {
123 
142 xdrproc_t
144 {
145  switch( t )
146  {
147  case dods_int16_c:
148  return (xdrproc_t)XDR_INT16 ;
149  break ;
150  case dods_uint16_c:
151  return (xdrproc_t)XDR_UINT16 ;
152  break ;
153  case dods_int32_c:
154  return (xdrproc_t)XDR_INT32 ;
155  break ;
156  case dods_uint32_c:
157  return (xdrproc_t)XDR_UINT32 ;
158  break ;
159  case dods_float32_c:
160  return (xdrproc_t)XDR_FLOAT32 ;
161  break ;
162  case dods_float64_c:
163  return (xdrproc_t)XDR_FLOAT64 ;
164  break ;
165  case dods_byte_c:
166  case dods_str_c:
167  case dods_url_c:
168  case dods_array_c:
169  case dods_structure_c:
170  case dods_sequence_c:
171  case dods_grid_c:
172  default:
173  return NULL ;
174  break ;
175  }
176 }
177 
178 } // namespace libdap
179