libdap  Updated for version 3.17.0
XDRUtils.cc
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  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 "config.h"
00034 
00035 #include "XDRUtils.h"
00036 #include "debug.h"
00037 #include "Str.h"
00038 
00039 using namespace libdap ;
00040 
00041 // This function is used to allocate memory for, and initialize, a new XDR
00042 // pointer. It sets the stream associated with the (XDR *) to STREAM.
00043 //
00044 // NB: STREAM is not one of the C++/libg++ iostream classes; it is a (FILE
00045 // *).
00046 
00047 //  These func's moved to xdrutil_ppc.* under the PPC as explained there
00048 #ifndef __POWERPC__
00049 XDR *
00050 new_xdrstdio(FILE *stream, enum xdr_op xop)
00051 {
00052     XDR *xdr = new XDR;
00053 
00054     xdrstdio_create(xdr, stream, xop);
00055 
00056     return xdr;
00057 }
00058 
00059 XDR *
00060 set_xdrstdio(XDR *xdr, FILE *stream, enum xdr_op xop)
00061 {
00062     xdrstdio_create(xdr, stream, xop);
00063 
00064     return xdr;
00065 }
00066 
00067 // Delete an XDR pointer allocated using the above function. Do not close the
00068 // associated FILE pointer.
00069 
00070 void
00071 delete_xdrstdio(XDR *xdr)
00072 {
00073     xdr_destroy(xdr);
00074 
00075     delete xdr; xdr = 0;
00076 }
00077 #endif
00078 
00079 // This function is used to en/decode Str and Url type variables. It is
00080 // defined as extern C since it is passed via function pointers to routines
00081 // in the xdr library where it is executed. This function is defined so
00082 // that Str and Url have an en/decoder which takes exactly two arguments: an
00083 // XDR * and a string reference.
00084 //
00085 // NB: this function is *not* used for arrays (i.e., it is not the function
00086 // referenced by BaseType's _xdr_coder field when the object is a Str or Url.
00087 // Also note that \e max_str_len is an obese number but that really does not
00088 // matter; xdr_string() would never actually allocate that much memory unless
00089 // a string that size was sent from the server.
00090 // Returns: XDR's bool_t; TRUE if no errors are detected, FALSE
00091 // otherwise. The formal parameter BUF is modified as a side effect.
00092 
00093 extern "C" bool_t
00094 xdr_str(XDR *xdrs, string &buf)
00095 {
00096     DBG(cerr << "In xdr_str, xdrs: " << xdrs << endl);
00097 
00098     switch (xdrs->x_op) {
00099     case XDR_ENCODE: { // BUF is a pointer to a (string *)
00100             const char *out_tmp = buf.c_str();
00101 
00102             return xdr_string(xdrs, (char **)&out_tmp, max_str_len);
00103         }
00104 
00105     case XDR_DECODE: {
00106             char *in_tmp = NULL;
00107 
00108             bool_t stat = xdr_string(xdrs, &in_tmp, max_str_len);
00109             if (!stat)
00110                 return stat;
00111 
00112             buf = in_tmp;
00113 
00114             free(in_tmp);
00115 
00116             return stat;
00117         }
00118 
00119     default:
00120         return 0;
00121     }
00122 }
00123 
00124 namespace libdap {
00125 
00144 xdrproc_t
00145 XDRUtils::xdr_coder( const Type &t )
00146 {
00147     switch( t )
00148     {
00149         case dods_int16_c:
00150             return (xdrproc_t)XDR_INT16 ;
00151             break ;
00152         case dods_uint16_c:
00153             return (xdrproc_t)XDR_UINT16 ;
00154             break ;
00155         case dods_int32_c:
00156             return (xdrproc_t)XDR_INT32 ;
00157             break ;
00158         case dods_uint32_c:
00159             return (xdrproc_t)XDR_UINT32 ;
00160             break ;
00161         case dods_float32_c:
00162             return (xdrproc_t)XDR_FLOAT32 ;
00163             break ;
00164         case dods_float64_c:
00165             return (xdrproc_t)XDR_FLOAT64 ;
00166             break ;
00167         case dods_byte_c:
00168         case dods_str_c:
00169         case dods_url_c:
00170         case dods_array_c:
00171         case dods_structure_c:
00172         case dods_sequence_c:
00173         case dods_grid_c:
00174         default:
00175             break ;
00176     }
00177 
00178     return NULL;
00179 }
00180 
00181 } // namespace libdap
00182