home *** CD-ROM | disk | FTP | other *** search
- /*
- * 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: hippoio.c,v 3.12 1992/04/10 22:48:42 rensing Rel $
- *
- * by Paul Rensing, Feb 28,1991
- */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "hippo.h"
-
- #ifdef VM
- #include "h_util.h"
- #include "h_xdr.h"
- #else
- #include "hippoutil.h"
- #include "hippoxdr.h"
- #endif
-
- GLOB_QUAL const char hippoio_c_rcsid[] =
- "$Id: hippoio.c,v 3.12 1992/04/10 22:48:42 rensing Rel $";
-
- 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]->ntuple == -1)
- record.disp_list[i]->ntuple = NULL;
- else if (! record.disp_list[i]->flags.ntByReference)
- {
- record.disp_list[i]->ntuple =
- record.nt_list[ (int) record.disp_list[i]->ntuple ];
- }
- }
-
- /*
- * 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->ntuple to ntuple index.
- * disp->ntuple == 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]->ntuple == NULL ||
- rec->disp_list[i]->bins.flags.fixed)
- {
- rec->disp_list[i]->ntuple = (ntuple) -1;
- }
- else
- {
- for (j=0; j<rec->num_nt; j++)
- {
- if (rec->disp_list[i]->ntuple == rec->nt_list[j])
- {
- rec->disp_list[i]->ntuple = (ntuple) j;
- break;
- }
- }
- if (j >= rec->num_nt)
- {
- rec->nt_list[rec->num_nt] = rec->disp_list[i]->ntuple;
- rec->disp_list[i]->ntuple = (ntuple) rec->num_nt++;
- }
- }
- }
- }
-
- rec->magic = MAGIC;
- rec->s_version = STRUCT_VERSION;
-
-
- 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;
-
- record.magic = MAGIC;
- record.s_version = STRUCT_VERSION;
- record.num_nt = record.num_disp = 0;
- record.nt_list = NULL;
- record.disp_list = NULL;
-
- /*
- * 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]->ntuple;
- if ( j < 0 || j >= record.num_nt ) {
- (*dlist)[i]->ntuple = NULL;
- } else {
- (*dlist)[i]->ntuple = record.nt_list[j];
- }
- }
-
- /*
- * resolve function references.
- */
- for (p=(*dlist)[i]->nt_cut; p!=NULL; p=p->next )
- if( (p->funcPtr = 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 = 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 = 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);
-
- return 0;
- }
-