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

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

/*
 * hippoio.c - input/output routines for hippo package.
 *
 * Copyright (C)  1991  The Board of Trustees of The Leland Stanford
 * Junior University.  All Rights Reserved.
 *
 * $Id: hio.c,v 5.0 1993/08/17 21:55:07 rensing Exp $
 *
 *  by Paul Rensing, Feb 28,1991
 */

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

GLOB_QUAL const char hippoio_c_rcsid[] = 
     "$Id: hio.c,v 5.0 1993/08/17 21:55:07 rensing Exp $";

static int constructRec( hippo_rec *rec, display dl[], ntuple ntl[] );


int h_write(const char *filenm, display dlist[], ntuple ntlist[] )
{
     int i;
     FILE *outfile;
     
     /* open the file */
     if ( (outfile = fopen(filenm,"wb")) == NULL)
     {
	  h_error("Could not open file");
	  return -1;
     }

     i = h_writeStream(outfile,dlist,ntlist); 

     fclose(outfile);
     
     return i;
}

int h_writeStream(FILE *file, display dlist[], ntuple ntlist[] )
{
     XDR xdrs;
     int rc = 0;
     
     /*
      * open the XDR stream.
      */
     xdrstdio_create(&xdrs, file, XDR_ENCODE);
     
     rc = h_writeXDR( &xdrs, dlist, ntlist );

     /*
      * destroy the XDR stream
      */
     xdr_destroy(&xdrs);
     
     return rc;
}
     
int h_writeMem(char *buf, int len, display dlist[], ntuple ntlist[] )
{
     XDR xdrs;
     int rc;
     
     /*
      * open the XDR stream.
      */
     xdrmem_create(&xdrs, buf, len, XDR_ENCODE);

     rc = h_writeXDR(&xdrs, dlist, ntlist );

     /*
      * destroy the XDR stream
      */
     xdr_destroy(&xdrs);
     
     return rc;
}

int h_writeXDR(XDR * xdrs, display dlist[], ntuple ntlist[] )
{
     hippo_rec record;
     int rc = 0;
     int i;
     /*
      * construct the hippo_rec to pass to XDR
      */
     if (constructRec( &record, dlist, ntlist ) != 0)
     {
	  h_error("Error constructing hippo record.");
	  return -1;
     }
          
     if (record.num_disp == 0 && record.num_nt == 0)
	  return 0;
     
     /* 
      * !!!!! displays have been modified, so don't crash now!!
      */

     /*
      * write the record.
      */
     if (!xdr_hippo_rec(xdrs, &record ))
     {
	  rc = -1;
	  h_error("Error writing XDR file");
     }
     

     /*
      * undo changes to displays
      */
     for (i=0; i<record.num_disp; i++)
     {
	  if ((int)record.disp_list[i]->tuple == -1)
	       record.disp_list[i]->tuple = NULL;
	  else if (! record.disp_list[i]->flags.ntByReference)
	  {
	       record.disp_list[i]->tuple =
		    record.nt_list[ (int) record.disp_list[i]->tuple ];
	  }
     }
     
     /*
      * free the lists in the record
      */
     if (record.nt_list != NULL) free(record.nt_list);
     if (record.disp_list != NULL) free(record.disp_list);

     return rc;
}


static int constructRec(hippo_rec *rec, display dlist[],
			ntuple ntlist[] )
{
     int i, j;
     int num_nt;
     int inlist;
     
     rec->num_nt = 0;
     if (ntlist != NULL)
	  while (ntlist[rec->num_nt] != NULL) rec->num_nt++;

     rec->num_disp = 0;
     if (dlist != NULL)
	  while (dlist[rec->num_disp] != NULL) rec->num_disp++;
     
     /*
      * check that there is something to write.
      */
     if (rec->num_disp == 0 && rec->num_nt == 0)
	  return 0;
     
     rec->disp_list = NULL;
     if (rec->num_disp > 0 && (rec->disp_list = (display *) 
			       malloc( rec->num_disp * sizeof(display))
			       )==NULL)
     {
	  h_error("Could not allocate memory for temporary display list");
	  return -1;
     }

     if ((rec->nt_list = (ntuple *) 
	  malloc((rec->num_disp+rec->num_nt) * sizeof(ntuple) ))==NULL)
     {
	  h_error("Could not allocate memory for temporary ntuple array");
	  return -1;
     }
   
     /* 
      * copy given list of n-tuples. Check for duplicates.
      */
     num_nt = 0;
     for (i=0; i<rec->num_nt; i++)
     {
	  inlist = 0;
	  for (j=0; j<num_nt; j++)
	       inlist |= (ntlist[i] == rec->nt_list[j]);
	  if (!inlist)
	       rec->nt_list[num_nt++] = ntlist[i];
     }
     rec->num_nt = num_nt;
     
     /*
      * now, add any n-tuples not in list which are needed by displays.
      * compile list of disp->tuple to ntuple index.
      * disp->tuple == NULL --> -1.
      */
     for (i=0; i<rec->num_disp; i++)
     {
	  rec->disp_list[i] = dlist[i];
	  if (! rec->disp_list[i]->flags.ntByReference)
	  {
	       if (rec->disp_list[i]->tuple == NULL || 
		   rec->disp_list[i]->bins.flags.fixed) 
	       {
		    rec->disp_list[i]->tuple = (ntuple) -1;
	       }
	       else
	       {
		    for (j=0; j<rec->num_nt; j++) 
		    {
			 if (rec->disp_list[i]->tuple == rec->nt_list[j])
			 {
			      rec->disp_list[i]->tuple = (ntuple) j;
			      break;
			 }
		    }
		    if (j >= rec->num_nt)
		    {
			 rec->nt_list[rec->num_nt] = rec->disp_list[i]->tuple;
			 rec->disp_list[i]->tuple = (ntuple) rec->num_nt++;
		    }
	       }
	  }
     }

     strncpy( rec->magic, MAGIC, 4);
     /*
      * don't use RCS ID directly because could not check in
      */
     rec->s_version = STRUCT_VERSION+1;

     return 0;
}


int h_read(const char *filenm, display **dlist, ntuple **ntlist )
{
     int i;
     FILE *infile;
     
     /* open the file */
     if ( (infile = fopen(filenm,"rb")) == NULL)
     {
	  h_error("Could not open input file");
	  return -1;
     }

     i = h_readStream(infile,dlist,ntlist);

     fclose(infile);
     
     return i;
}

int h_readStream(FILE *file, display **dlist, ntuple **ntlist )
{
     XDR xdrs;
     int rc;
     
     /*
      * open the XDR stream.
      */
     xdrstdio_create(&xdrs, file, XDR_DECODE);

     rc = h_readXDR(&xdrs, dlist, ntlist );
     /*
      * destroy the XDR stream
      */
     xdr_destroy(&xdrs);
     
     return rc;
}

int h_readMem(char *buf, int len, display **dlist, ntuple **ntlist )
{
     XDR xdrs;
     int rc;
     
     /*
      * open the XDR stream.
      */
     xdrmem_create(&xdrs, buf, len, XDR_DECODE);

     rc = h_readXDR(&xdrs, dlist, ntlist );
     /*
      * destroy the XDR stream
      */
     xdr_destroy(&xdrs);
     
     return rc;
}


int h_readXDR(XDR *xdrs, display **dlist, ntuple **ntlist )
{
     hippo_rec record;
     int i,j;
     func_id p;
	  
     memset( &record, 0, sizeof(hippo_rec) );
     
     /*
      * read the record.
      */
     if (!xdr_hippo_rec(xdrs, &record ))
     {
	  h_error("Error reading XDR file");
	  return -1;
     }
     
     /*
      * allocate output records.
      */
     if ((*ntlist=(ntuple *) 
	  malloc( (record.num_nt+1) * sizeof(ntuple)))==NULL)
     {
	  h_error("Could not allocate memory for ntuple array");
	  return -1;
     }

     if ((*dlist=(display *) 
	  malloc( (record.num_disp+1) * sizeof(display)))==NULL)
     {
	  h_error("Could not allocate memory for display array");
	  return -1;
     }
     
     
     /*
      * copy lists and undo changes to displays.
      */
     for (i=0; i<record.num_disp; i++)
     {
	  (*dlist)[i] = record.disp_list[i];
	  if ( !(*dlist)[i]->flags.ntByReference )
	  {
	       j = (int) (*dlist)[i]->tuple;
	       if ( j < 0 || j >= record.num_nt ) {
		    (*dlist)[i]->tuple = NULL;
	       } else {
		    (*dlist)[i]->tuple = record.nt_list[j];
	       }
	  }
	  
	  /*
	   * resolve function references.
	   */
	  for (p=(*dlist)[i]->nt_cut; p!=NULL; p=p->next )
	       if( (p->funcPtr = (int (*)())h_fNameSrch(p->name)) == NULL)
		    h_error("Cut function name is not in registry");

	  for (p=(*dlist)[i]->bin_func; p!=NULL; p=p->next )
	       if( (p->funcPtr = (int (*)())h_fNameSrch(p->name)) == NULL)
		    h_error("Cut function name is not in registry");

	  for (p=(*dlist)[i]->plot_func; p!=NULL; p=p->next )
	       if( (p->funcPtr = (int (*)())h_fNameSrch(p->name)) == NULL)
		    h_error("Cut function name is not in registry");
     }
     (*dlist)[record.num_disp] = NULL;
     for (i=0; i<record.num_nt; i++)
     {
	  (*ntlist)[i] = record.nt_list[i];
     }
     (*ntlist)[record.num_nt] = NULL;
     
     /*
      * free the lists in the record
      */
     if (record.nt_list != NULL) free(record.nt_list);
     if (record.disp_list != NULL) free(record.disp_list);
     if (record.s_version != NULL) free(record.s_version);
     
     return 0;
}


int h_isHippoFile( const char *filename )
{
     FILE *file;
     char mag[5];
     
     if ( (file = fopen(filename, "rb")) == NULL )
     	return 0;		/* return FALSE if can't open file */
	
     fread(mag, sizeof(char), 4, file );
     mag[4] = '\0';		/* just in case */
     fclose(file);
     
     return strncmp( mag, MAGIC, strlen(MAGIC) ) == 0;
}


/*
 * Special function to write out the tuple which corresponds with
 * what is being plotted by a particular display, ie. with the cuts applied.
 */
int h_writeNtForDisp( char *filename, display disp )
{
     ntuple ntlist[2];
     ntuple dnt = h_getNtuple( disp );
     func_id cutlist = disp->nt_cut;
     int i, fSize = 0, n;
     float *f = NULL;
     
     ntlist[0] = h_new( h_getNtDim(dnt) );
     if (ntlist[0] == NULL)
     {
	  h_error("Could not create new ntuple");
	  return -1;
     }

     h_setNtTitle(ntlist[0], h_getNtTitle(dnt) );
     for (i=0; i<h_getNtDim(dnt); i++)
	  h_setNtLabel(ntlist[0], i, h_getNtLabel(dnt,i) );
	  
     n = h_getNtNdata(dnt);
     for (i = 0; i < n; i++ )
     {
	  if (doCuts(dnt->data, i, cutlist)) continue;
	  f = h_getNtData( dnt, i, f, &fSize );
	  h_arrayFill( ntlist[0], f );
     }
     
     ntlist[1] = NULL;
     i = h_write(filename, NULL, ntlist );
     h_freeNt( ntlist[0] );
     free(f);
     
     return i;
}

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