ftp.nice.ch/pub/next/science/mathematics/hippoplotamus.2.0.s.tar.gz#/hippo2.0/hxdrio.c

This is hxdrio.c in view mode; [Download] [Up]

/*
 * hippoxdr.c - XDR routines for hippo I/O
 *
 * Copyright (C)  1991  The Board of Trustees of The Leland Stanford
 * Junior University.  All Rights Reserved.
 *
 * $Id: hxdrio.c,v 5.0 1993/08/17 21:57:00 rensing Exp $
 *
 *  by Paul Rensing
 */

#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include "hippo.h"
#include "hxdrio.h"
#include "hutil.h"

/*
 * for machines which are naturally big-endian, IEEE floating point,
 * define EASY_XDR so that the ntuple data array (which is the vast majority
 * of a hippo file) is written out without checking for conversion.
 */
#if defined(_IBMR2) || (defined(NeXT)&& !defined(__LITTLE_ENDIAN__)) || defined(sun) || defined(sgi)
#define EASY_XDR
#endif

GLOB_QUAL const char hippoxdr_c_rcsid[] = "$Id: hxdrio.c,v 5.0 1993/08/17 21:57:00 rensing Exp $";

GLOB_QUAL const char hippostruct_h_rcsid[] = STRUCT_VERSION;

#define CURRENT_VER_NUM 40018
/* version number of data being read/written */
static int fileVersion = CURRENT_VER_NUM;

static bool_t xdr_mywrapstring(XDR *xdrs, char **string);


static bool_t xdr_mywrapstring(XDR *xdrs, char **string)
{
#ifndef __STDC__
     static
#endif
     char blank[]=" ";
     char *pb = blank;
     int rc;
     
     if (xdrs->x_op == XDR_ENCODE && *string == NULL)
	  rc = xdr_wrapstring(xdrs,&pb);
     else
	  rc = xdr_wrapstring(xdrs,string);
     
     return rc;
}


bool_t xdr_floatarray(XDR *xdrs, caddr_t *objp, unsigned int *count,
		      unsigned int max)
{
#ifdef EASY_XDR
     switch (xdrs->x_op)
     {
     case XDR_FREE:
	  return xdr_array(xdrs, objp, count, max, sizeof(float), xdr_float);
	  break;
	  
     case XDR_DECODE:		/* ie. read */
	  if (! XDR_GETLONG(xdrs,(long *)count)) 
	       return FALSE;
	  
	  if ( *objp == NULL && *count != 0 &&
	      (*objp = (char *) malloc(*count*sizeof(float))) == NULL)
	       return FALSE;
	  
	  if (*count != 0)
	       return XDR_GETBYTES(xdrs,*objp,
				   (unsigned int) *count*sizeof(float));
	  break;
	  
     case XDR_ENCODE:
	  if (! XDR_PUTLONG(xdrs,(long *)count)) 
	       return FALSE;

	  if (*count > 0)
	       return XDR_PUTBYTES(xdrs,*objp,*count*sizeof(float));
	  break;
     }	  
     return 1;
#else
     return xdr_array(xdrs, objp, count, max, sizeof(float), xdr_float);
#endif
}

bool_t xdr_rectangle(XDR *xdrs, rectangle *objp)
{
     if (!xdr_float(xdrs, &objp->origin.x)) {
	  return FALSE;
     }
     if (!xdr_float(xdrs, &objp->origin.y)) {
	  return FALSE;
     }
     if (!xdr_float(xdrs, &objp->size.width)) {
	  return FALSE;
     }
     if (!xdr_float(xdrs, &objp->size.height)) {
	  return FALSE;
     }
     return TRUE;
}




bool_t xdr_func_id(XDR *xdrs, func_id *objp)
{
     func_id *next=NULL;
     bool_t more_data;
     
     while (1)
     {
	  more_data = (*objp != NULL);
	  if (!xdr_bool(xdrs, &more_data))
	  {
	       return FALSE;
	  }
	  
	  if (!more_data) break;
	  
	  if (xdrs->x_op == XDR_FREE)
	       next = &(*objp)->next;
	  
	  if (!xdr_reference(xdrs, (caddr_t *)objp, 
			     sizeof(func_id_t), xdr_func_id_t)) {
	       return FALSE;
	  }
	  objp = (xdrs->x_op == XDR_FREE) ?
	       next : &(*objp)->next;
     }
     *objp = NULL;
     
     return TRUE;
}     	       

bool_t xdr_func_id_t(XDR *xdrs, func_id_t *objp)
{
     char *tmp;

     /*
      * bug on RS/6000: &objp->name is not allowed because it is not
      * in memory
      */
     tmp = objp->name;
     
     if (!xdr_string(xdrs, &tmp, FUNCNAMELEN)) {
	  return FALSE;
     }
     
     /*
      * don't bother with the function pointer. Reconstruct it later
      * using the function name (objp->name).
      */
     
     if (!xdr_array(xdrs, (char **)&objp->paramBlk, 
		    (u_int *)&objp->blkSize, UINT_MAX, 
		    sizeof(double), xdr_double)) {
	  return FALSE;
     }
     
     if (!xdr_enum(xdrs, (int *)&objp->lineStyle)) {
	  return FALSE;
     }
     
     return TRUE;
}


bool_t xdr_ntuple_t(XDR *xdrs, ntuple_t *objp)
{
     unsigned int i, j;
     
     if (!xdr_int(xdrs, &objp->ndim))
	  return FALSE;
     if (!xdr_int(xdrs, &objp->rev))
	  return FALSE;
     
     /*
      * read in the data.
      */
     if (xdrs->x_op == XDR_DECODE)
     {
	  if ( (objp->data = (float **)malloc(objp->ndim * sizeof(float *)))
	      == NULL) 
	       return FALSE;
     }
     if (fileVersion < 30009)
     {
	  /* had better get here only for DECODE */
	  float *localData = NULL;

	  if (!xdr_floatarray(xdrs, (char **)&localData, &i, UINT_MAX))
	       return FALSE;
	  objp->memAlloc = i/objp->ndim; /* number of tuples */
	  objp->ndata = objp->memAlloc;
	  for (i=0; i<objp->ndim; i++)
	  {
	       objp->data[i] = (float *) malloc(objp->memAlloc*sizeof(float));
	       if (objp->data[i] == NULL) return FALSE;
	  }
	  for (i=0; i<objp->ndata; i++ )
	       for (j=0; j<objp->ndim; j++ )
		    objp->data[j][i] = localData[i*objp->ndim + j];
	  free(localData);
     }
     else
     {
	  for (i=0; i<objp->ndim; i++) 
	  {
	       if (xdrs->x_op == XDR_DECODE) objp->data[i] = NULL;
	       if (!xdr_floatarray(xdrs, (char **)&objp->data[i], 
				   (unsigned int *)&objp->ndata, UINT_MAX))
		    return FALSE;
	  }
	  if (xdrs->x_op == XDR_DECODE) objp->memAlloc = objp->ndata;
	  if (xdrs->x_op == XDR_FREE)
	       free(objp->data);
     }
     

     /*
      * handle labels, min/max, title
      */
     if (!xdr_int(xdrs, &objp->extremeBad)) {
	  return FALSE;
     }
     if (!xdr_floatarray(xdrs, (char **)&objp->nlow, (u_int *)&objp->ndim,
			 UINT_MAX))
	  return FALSE;
     if (!xdr_floatarray(xdrs, (char **)&objp->nhigh, (u_int *)&objp->ndim,
			 UINT_MAX))
	  return FALSE;

     if (!xdr_mywrapstring(xdrs, (char **)&objp->title )) {
	  return FALSE;
     }

     if (xdrs->x_op == XDR_DECODE)
     {
	  if ( (objp->label = (char **)malloc(objp->ndim * sizeof(char *)))
	      == NULL) 
	  {
	       return FALSE;
	  }
     }
     for (i=0; i<objp->ndim; i++) 
     {
	  if (xdrs->x_op == XDR_DECODE) objp->label[i] = NULL;
     	  if (!xdr_mywrapstring(xdrs, (char **)&objp->label[i] )) {
	       return FALSE;
	  }
     }
     if (xdrs->x_op == XDR_FREE)
	  free(objp->label);
	  
     return TRUE;
}




bool_t xdr_ntuple(XDR *xdrs, ntuple *objp)
{
     if (!xdr_pointer(xdrs, (char **)objp, sizeof(ntuple_t), xdr_ntuple_t))
	  return FALSE;
     return TRUE;
}


bool_t xdr_bins_t(XDR *xdrs, bins_t *objp)
{
     {
	  char *p = (char *) &objp->flags;
	  u_int c = sizeof(objp->flags);
	  if (!xdr_bytes(xdrs, &p, &c, c))
	       return FALSE;
     }

     /*
      * xAxis data
      */
     if (!xdr_int(xdrs, &objp->xAxis.nBins)) {
	  return FALSE;
     }
     if (!xdr_float(xdrs, &objp->xAxis.low)) {
	  return FALSE;
     }
     if (!xdr_float(xdrs, &objp->xAxis.high)) {
	  return FALSE;
     }
     if (!xdr_vector(xdrs, (char *)objp->xAxis.moments, 3, 
		     sizeof(float), xdr_float)) 
     {
	  return FALSE;
     }

     /*
      * yAxis data
      */
     if (!xdr_int(xdrs, &objp->yAxis.nBins))
	  return FALSE;
     if (!xdr_float(xdrs, &objp->yAxis.low))
	  return FALSE;
     if (!xdr_float(xdrs, &objp->yAxis.high))
	  return FALSE;
     if (!xdr_vector(xdrs, (char *)objp->yAxis.moments, 3,
		     sizeof(float), xdr_float)) 
	  return FALSE;

     if (!xdr_int(xdrs, &objp->binAlloc))
	  return FALSE;
     if (!xdr_int(xdrs, &objp->ndata))
	  return FALSE;
     if (objp->flags.fixed || xdrs->x_op == XDR_FREE)
     {
	  if (!xdr_floatarray(xdrs, (char **)&objp->data,
			      (u_int *)&objp->binAlloc, UINT_MAX))
	       return FALSE;
	  if (!xdr_floatarray(xdrs, (char **)&objp->variance,
			      (u_int *)&objp->binAlloc, UINT_MAX))
	       return FALSE;
     }
     else if (xdrs->x_op == XDR_DECODE)
     {
	  objp->ndata = 0;
	  objp->data = NULL;
	  objp->variance = NULL;
	  objp->binAlloc = 0;
     }
     
     if (fileVersion < 30008)
     {
	  int i,j;
	  int oldTot[3][3];		/* for reading pre 3.8 version */
     
	  if (!xdr_vector(xdrs, (char *)oldTot,
			  9, sizeof(int), xdr_int)) 
	       return FALSE;
	  for (i=0; i<3; i++)
	       for (j=0; j<3; j++)
		    objp->totals[i][j] = oldTot[i][j];
     }
     else
     {
	  if (!xdr_vector(xdrs, (char *)objp->totals,
			  9, sizeof(int), xdr_float)) 
	       return FALSE;
     }

     if (!xdr_float(xdrs, &objp->binMin))
	  return FALSE;
     if (!xdr_float(xdrs, &objp->binMax))
	  return FALSE;

     return TRUE;
}


bool_t xdr_axis_t(XDR *xdrs, axis_t *objp)
{
     if (!xdr_float(xdrs, &objp->low)) {
	  return FALSE;
     }
     if (!xdr_float(xdrs, &objp->high)) {
	  return FALSE;
     }
     if (!xdr_mywrapstring(xdrs, (char **)&objp->label )) {
	  return FALSE;
     }

     {
          char *p = (char *) &objp->flags;
	  u_int c = sizeof(objp->flags);
	  if (!xdr_bytes(xdrs, &p, &c, c))
	       return FALSE;
     }

     if (!xdr_float(xdrs, &objp->tickLength)) {
	  return FALSE;
     }
     if (!xdr_float(xdrs, &objp->scaleFontSize)) {
	  return FALSE;
     }
     if (!xdr_float(xdrs, &objp->firstTick)) {
	  return FALSE;
     }
     if (!xdr_float(xdrs, &objp->tickStep)) {
	  return FALSE;
     }
     if (!xdr_int(xdrs, &objp->numMinorTicks)) {
	  return FALSE;
     }
     
     return TRUE;
}


bool_t xdr_bind_strt_t(XDR *xdrs, bind_strt_t *objp)
{
     if (!xdr_int(xdrs, &objp->x)) {
	  return FALSE;
     }
     if (!xdr_int(xdrs, &objp->y)) {
	  return FALSE;
     }
     if (!xdr_int(xdrs, &objp->z)) {
	  return FALSE;
     }
     if (!xdr_int(xdrs, &objp->weight)) {
	  return FALSE;
     }
     if (!xdr_int(xdrs, &objp->xerror)) {
	  return FALSE;
     }
     if (!xdr_int(xdrs, &objp->yerror)) {
	  return FALSE;
     }
     return TRUE;
}

bool_t xdr_3Dstruct(XDR * xdrs, display_t * objp)
{
    if (!xdr_float(xdrs, &objp->theta))
	return FALSE;
    if (!xdr_float(xdrs, &objp->phi))
	return FALSE;
    if (!xdr_float(xdrs, &objp->dist))
	return FALSE;

    if (fileVersion >= 40018) 
    {
	if (!xdr_int(xdrs, &objp->doCube))
	    return FALSE;
	if (!xdr_int(xdrs, &objp->doWireFrame))
	    return FALSE;
	if (!xdr_int(xdrs, &objp->doFill))
	    return FALSE;
	if (!xdr_int(xdrs, &objp->doMesh))
	    return FALSE;
	if (!xdr_int(xdrs, &objp->doScatter))
	    return FALSE;
	if (!xdr_int(xdrs, &objp->doSpeedy))
	    return FALSE;
	if (!xdr_vector(xdrs,(char *)objp->color, 5, sizeof(float),xdr_float))
	    return FALSE;
    } 
    else 
    {
	objp->doCube = 1;
	objp->doSpeedy = 0;
	if (objp->graphtype == THREEDSCATTER) 
	{
	    objp->doWireFrame = 0;
	    objp->doFill = 0;
	    objp->doMesh = 0;
	    objp->doScatter = 1;
	}
	else 
	{
	    objp->doWireFrame = 0;
	    objp->doFill = 0;
	    objp->doMesh = 1;
	    objp->doScatter = 0;
	}
	objp->color[0] = 0.6;
	objp->color[0] = 0.2;
	objp->color[0] = 0.0;
	objp->color[0] = 0.4;
	objp->color[0] = 0.9;
    }
    return TRUE;
}

bool_t xdr_display_t(XDR *xdrs, display_t *objp)
{
     /*
      * write the ntuple pointer as an integer. Integer is index into 
      * the list of ntuple in a record.
      */
     if (!xdr_int(xdrs, (int *)&objp->tuple)) {
	  return FALSE;
     }
     if (!xdr_int(xdrs, &objp->nt_rev)) {
	  return FALSE;
     }
     if (!xdr_mywrapstring(xdrs, (char **)&objp->ntFile ))
     {
	  return FALSE;
     }

     if (!xdr_int(xdrs, &objp->dim)) {
	  return FALSE;
     }
     if (!xdr_enum(xdrs, (int *)&objp->graphtype)) {
	  return FALSE;
     }
     if (!xdr_enum(xdrs, (int *)&objp->drawtype)) {
	  return FALSE;
     }

     {
          char *p = (char *) &objp->flags;
	  u_int c = sizeof(objp->flags);
	  if (!xdr_bytes(xdrs, &p, &c, c))
	       return FALSE;
     }

     if (!xdr_mywrapstring(xdrs, (char **)&objp->title ))
     {
	  return FALSE;
     }


     if (!xdr_bins_t(xdrs, &objp->bins))
     {
	  return FALSE;
     }
     
     if (!xdr_axis_t(xdrs, &objp->xAxis)) {
	  return FALSE;
     }
     if (!xdr_axis_t(xdrs, &objp->yAxis)) {
	  return FALSE;
     }
     if (!xdr_axis_t(xdrs, &objp->zAxis)) {
	  return FALSE;
     }

     if (!xdr_enum(xdrs, (int *)&objp->plotSymbol)) {
	  return FALSE;
     }
     if (!xdr_float(xdrs, &objp->symbolSize)) {
	  return FALSE;
     }
     if (!xdr_enum(xdrs, (int *)&objp->lineStyle)) {
	  return FALSE;
     }
     if (!xdr_bind_strt_t(xdrs, &objp->binding)) {
	  return FALSE;
     }
     if (!xdr_rectangle(xdrs, &objp->drawRect)) {
	  return FALSE;
     }
     if (!xdr_rectangle(xdrs, &objp->marginRect)) {
	  return FALSE;
     }
     if (!xdr_func_id(xdrs, &objp->nt_cut)) {
	  return FALSE;
     }
     if (!xdr_func_id(xdrs, &objp->bin_func)) {
	  return FALSE;
     }
     if (!xdr_func_id(xdrs, &objp->plot_func)) {
	  return FALSE;
     }
     if (fileVersion > 40012)
     {
	  if (!xdr_enum(xdrs, (int *)&objp->fSumLineStyle)) 
	       return FALSE;
     }
     
     if (!xdr_func_id(xdrs, &objp->binToColor)) {
	  return FALSE;
     }
     
     /* 
      * 3D data
      */
     if (fileVersion > 40012 && !xdr_3Dstruct(xdrs, objp)) 
          return FALSE;

     return TRUE;
}




bool_t xdr_display(XDR *xdrs, display *objp)
{
     if (!xdr_pointer(xdrs, (char **)objp, sizeof(display_t), 
		      xdr_display_t)) {
	  return FALSE;
     }
     return TRUE;
}



bool_t xdr_hippo_rec(XDR *xdrs, hippo_rec *objp)
{
     int l;
     
     /*
      * read/write the magic number. Pad (with NULL) or truncate to 4 bytes.
      */
     switch (xdrs->x_op)
     {
     case XDR_DECODE:		/* ie. read */
	  if (! XDR_GETBYTES(xdrs,objp->magic,4*sizeof(char)))
	       return FALSE;
	  break;
	  
     case XDR_ENCODE:
	  if (! XDR_PUTBYTES(xdrs,objp->magic,4*sizeof(char)))
	       return FALSE;
	  break;
     case XDR_FREE:
	  break;
     }	  
     l = strlen(MAGIC);
     if (l>4) l=4;
     if (xdrs->x_op == XDR_DECODE && strncmp(objp->magic,MAGIC,l) != 0)
	  return FALSE;

     /*
      * read/write the structure version.
      *
      */
     if (!xdr_mywrapstring(xdrs, (char **)&objp->s_version ))
	  return FALSE;

     /*
      * check the version number.
      */
     if (xdrs->x_op == XDR_DECODE)
     {
	  int majorVer, minorVer;
	  int i = sscanf(objp->s_version,"%*s %*s %d.%d",&majorVer,&minorVer);
	  fileVersion = majorVer*10000 + minorVer;
	  if (i < 1) fileVersion = 30000;
	  if (fileVersion < 30005)
	  {
	       char msg[80];
	       sprintf(msg,"Old hippo file version: %s",objp->s_version);
	       h_error(msg);
	       h_error("No displays will be read");
	  }
     }
     else
	  fileVersion = CURRENT_VER_NUM;
     
     /*
      * ntuples
      */
     if (!xdr_array(xdrs, (char **) &objp->nt_list,
		    (u_int *)&objp->num_nt, UINT_MAX,
		    sizeof(ntuple), xdr_ntuple ))
     {
	  return FALSE;
     }

     /*
      * displays
      */
     if (fileVersion < 30005)
	  objp->num_disp = 0;
     else
	  if (!xdr_array(xdrs, (char **)&objp->disp_list,
			 (u_int *)&objp->num_disp, UINT_MAX,
			 sizeof(display), xdr_display ))
	  {
	       return FALSE;
	  }
     
     return TRUE;
}

These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.