libdap++ Updated for version 3.8.2

XDRUtils.cc

Go to the documentation of this file.
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 using namespace libdap ;
00038 
00039 // This function is used to allocate memory for, and initialize, a new XDR
00040 // pointer. It sets the stream associated with the (XDR *) to STREAM.
00041 //
00042 // NB: STREAM is not one of the C++/libg++ iostream classes; it is a (FILE
00043 // *).
00044 
00045 //  These func's moved to xdrutil_ppc.* under the PPC as explained there
00046 #ifndef __POWERPC__
00047 XDR *
00048 new_xdrstdio(FILE *stream, enum xdr_op xop)
00049 {
00050     XDR *xdr = new XDR;
00051 
00052     xdrstdio_create(xdr, stream, xop);
00053 
00054     return xdr;
00055 }
00056 
00057 XDR *
00058 set_xdrstdio(XDR *xdr, FILE *stream, enum xdr_op xop)
00059 {
00060     xdrstdio_create(xdr, stream, xop);
00061 
00062     return xdr;
00063 }
00064 
00065 // Delete an XDR pointer allocated using the above function. Do not close the
00066 // associated FILE pointer.
00067 
00068 void
00069 delete_xdrstdio(XDR *xdr)
00070 {
00071     xdr_destroy(xdr);
00072 
00073     delete xdr; xdr = 0;
00074 }
00075 #endif
00076 
00077 // This function is used to en/decode Str and Url type variables. It is
00078 // defined as extern C since it is passed via function pointers to routines
00079 // in the xdr library where it is executed. This function is defined so
00080 // that Str and Url have an en/decoder which takes exactly two arguments: an
00081 // XDR * and a string reference.
00082 //
00083 // NB: this function is *not* used for arrays (i.e., it is not the function
00084 // referenced by BaseType's _xdr_coder field when the object is a Str or Url.
00085 // Also note that \e max_str_len is an obese number but that really does not
00086 // matter; xdr_string() would never actually allocate that much memory unless
00087 // a string that size was sent from the server.
00088 // Returns: XDR's bool_t; TRUE if no errors are detected, FALSE
00089 // otherwise. The formal parameter BUF is modified as a side effect.
00090 
00091 extern "C" bool_t
00092 xdr_str(XDR *xdrs, string &buf)
00093 {
00094     DBG(cerr << "In xdr_str, xdrs: " << xdrs << endl);
00095 
00096     switch (xdrs->x_op) {
00097     case XDR_ENCODE: { // BUF is a pointer to a (string *)
00098             const char *out_tmp = buf.c_str();
00099 
00100             return xdr_string(xdrs, (char **)&out_tmp, max_str_len);
00101         }
00102 
00103     case XDR_DECODE: {
00104             char *in_tmp = NULL;
00105 
00106             bool_t stat = xdr_string(xdrs, &in_tmp, max_str_len);
00107             if (!stat)
00108                 return stat;
00109 
00110             buf = in_tmp;
00111 
00112             free(in_tmp);
00113 
00114             return stat;
00115         }
00116 
00117     default:
00118         return 0;
00119     }
00120 }
00121 
00122 namespace libdap {
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 
00178 } // namespace libdap
00179