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