00001 // XDRUtils.cc 00002 00003 // -*- mode: c++; c-basic-offset:4 -*- 00004 00005 // This file is part of libdap, A C++ implementation of the OPeNDAP Data 00006 // Access Protocol. 00007 00008 // Copyright (c) 2002,2003 OPeNDAP, Inc. 00009 // Author: Patrick West <pwest@ucar.edu> 00010 // 00011 // This library is free software; you can redistribute it and/or 00012 // modify it under the terms of the GNU Lesser General Public 00013 // License as published by the Free Software Foundation; either 00014 // version 2.1 of the License, or (at your option) any later version. 00015 // 00016 // This library is distributed in the hope that it will be useful, 00017 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00019 // Lesser General Public License for more details. 00020 // 00021 // You should have received a copy of the GNU Lesser General Public 00022 // License along with this library; if not, write to the Free Software 00023 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00024 // 00025 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112. 00026 00027 // (c) COPYRIGHT URI/MIT 1994-1999 00028 // Please read the full copyright statement in the file COPYRIGHT_URI. 00029 // 00030 // Authors: 00031 // pwest Patrick West <pwest@ucar.edu> 00032 00033 #include "XDRUtils.h" 00034 #include "debug.h" 00035 #include "Str.h" 00036 00037 // This function is used to allocate memory for, and initialize, a new XDR 00038 // pointer. It sets the stream associated with the (XDR *) to STREAM. 00039 // 00040 // NB: STREAM is not one of the C++/libg++ iostream classes; it is a (FILE 00041 // *). 00042 00043 // These func's moved to xdrutil_ppc.* under the PPC as explained there 00044 #ifndef __POWERPC__ 00045 XDR * 00046 new_xdrstdio(FILE *stream, enum xdr_op xop) 00047 { 00048 XDR *xdr = new XDR; 00049 00050 xdrstdio_create(xdr, stream, xop); 00051 00052 return xdr; 00053 } 00054 00055 XDR * 00056 set_xdrstdio(XDR *xdr, FILE *stream, enum xdr_op xop) 00057 { 00058 xdrstdio_create(xdr, stream, xop); 00059 00060 return xdr; 00061 } 00062 00063 // Delete an XDR pointer allocated using the above function. Do not close the 00064 // associated FILE pointer. 00065 00066 void 00067 delete_xdrstdio(XDR *xdr) 00068 { 00069 xdr_destroy(xdr); 00070 00071 delete xdr; xdr = 0; 00072 } 00073 #endif 00074 00075 // This function is used to en/decode Str and Url type variables. It is 00076 // defined as extern C since it is passed via function pointers to routines 00077 // in the xdr library where it is executed. This function is defined so 00078 // that Str and Url have an en/decoder which takes exactly two argumnets: an 00079 // XDR * and a string reference. 00080 // 00081 // NB: this function is *not* used for arrays (i.e., it is not the function 00082 // referenced by BaseType's _xdr_coder field when the object is a Str or Url. 00083 // Also note that \e max_str_len is an obese number but that really does not 00084 // matter; xdr_string() would never actually allocate that much memory unless 00085 // a string that size was sent from the server. 00086 // Returns: XDR's bool_t; TRUE if no errors are detected, FALSE 00087 // otherwise. The formal parameter BUF is modified as a side effect. 00088 00089 extern "C" bool_t 00090 xdr_str(XDR *xdrs, string &buf) 00091 { 00092 DBG(cerr << "In xdr_str, xdrs: " << xdrs << endl); 00093 00094 switch (xdrs->x_op) { 00095 case XDR_ENCODE: { // BUF is a pointer to a (string *) 00096 const char *out_tmp = buf.c_str(); 00097 00098 return xdr_string(xdrs, (char **)&out_tmp, max_str_len); 00099 } 00100 00101 case XDR_DECODE: { 00102 char *in_tmp = NULL; 00103 00104 bool_t stat = xdr_string(xdrs, &in_tmp, max_str_len); 00105 if (!stat) 00106 return stat; 00107 00108 buf = in_tmp; 00109 00110 free(in_tmp); 00111 00112 return stat; 00113 } 00114 00115 default: 00116 #if 0 00117 // Removed; build fails on FC6. jhrg 8/28/07 00118 assert(false); 00119 #endif 00120 return 0; 00121 } 00122 } 00123 00142 xdrproc_t 00143 XDRUtils::xdr_coder( const Type &t ) 00144 { 00145 switch( t ) 00146 { 00147 case dods_int16_c: 00148 return (xdrproc_t)XDR_INT16 ; 00149 break ; 00150 case dods_uint16_c: 00151 return (xdrproc_t)XDR_UINT16 ; 00152 break ; 00153 case dods_int32_c: 00154 return (xdrproc_t)XDR_INT32 ; 00155 break ; 00156 case dods_uint32_c: 00157 return (xdrproc_t)XDR_UINT32 ; 00158 break ; 00159 case dods_float32_c: 00160 return (xdrproc_t)XDR_FLOAT32 ; 00161 break ; 00162 case dods_float64_c: 00163 return (xdrproc_t)XDR_FLOAT64 ; 00164 break ; 00165 case dods_byte_c: 00166 case dods_str_c: 00167 case dods_url_c: 00168 case dods_array_c: 00169 case dods_structure_c: 00170 case dods_sequence_c: 00171 case dods_grid_c: 00172 default: 00173 return NULL ; 00174 break ; 00175 } 00176 } 00177