DataMuseum.dk

Presents historical artifacts from the history of:

DKUUG/EUUG Conference tapes

This is an automatic "excavation" of a thematic subset of
artifacts from Datamuseum.dk's BitArchive.

See our Wiki for more about DKUUG/EUUG Conference tapes

Excavated with: AutoArchaeologist - Free & Open Source Software.


top - download
Index: ┃ R T

⟦b1f689d77⟧ TextFile

    Length: 5599 (0x15df)
    Types: TextFile
    Names: »ReadIndPoly.c«

Derivation

└─⟦a0efdde77⟧ Bits:30001252 EUUGD11 Tape, 1987 Spring Conference Helsinki
    └─ ⟦this⟧ »EUUGD11/euug-87hel/sec1/offconv/ReadIndPoly.c« 

TextFile


/*
 *
 * Description
 *	Read an indexed_poly data file.
 *
 * Output
 *
 * Input
 *	pProp		Pointer to property structure in which to store data
 *	fname		Full path/file name of file to be read.
 *
 * Diagnostics
 *	Returns 0 if successful, -1 if unsuccessful for any reason.
 *
 * Author
 *	Randi J. Rost
 *	Digital Equipment Corp.
 *	Workstation Systems Engineering
 *	Palo Alto, CA
 *
 * History
 *	17-Nov-86	Created
 *
 */


#include <stdio.h>
#include <sys/file.h>
#include "off.h"

#define MAX_DATA_ITEMS		30

OFFReadIndexedPoly(pProp, fname)
    OFFProperty	*pProp;
    char	*fname;

    {
    FILE	*ascfd;
    int		binfd;
    int		code;
    char	*ptr, *ptr2;
    int		i, j;
    long	npts, npolys, nconnects;
    long	*lptr;
    char	format[MAX_DATA_ITEMS][10];
    int		padding[MAX_DATA_ITEMS];
    int		size[MAX_DATA_ITEMS];
    int		datasize = 0;
    int		type = OFF_BINARY;
    char	ch;
    char	bigstr[OFF_BIGSTR];
    int		nostrings = 1;
    int		strlength;
    int		endpad;
    long	junk;


/*  Try opening the file as if it were binary first  */
    binfd = open(fname, O_RDONLY, 0);

/*  If error opening file, punt  */
    if (binfd < 0)
	{
	fprintf(stderr, "OFFReadIndexedPoly: cannot open data file %s\n",
		fname);
	return(-1);
	}

/*  Read first word of file to determine file type  */
    read(binfd, &code, sizeof(long));

    if (code != OFF_INDEXED_POLY_MAGIC)
	{
    /*  Close the file  */
	close(binfd);

    /*  Try to open it as an ascii data file  */
	ascfd = fopen(fname, "r");

    /*  If error opening file, punt  */
	if (ascfd == NULL)
	    {
	    fprintf(stderr, "OFFReadIndexedPoly: cannot open data file %s\n",
		    fname);
	    return(-1);
	    }
	type = OFF_ASCII;
	}

/*  Read in the number of points and polygons for the object  */
    if (type == OFF_ASCII)
	fscanf(ascfd,"%d %d %d\n", &npts, &npolys, &nconnects);
    else
	{
	read(binfd, &npts, sizeof(long));
	read(binfd, &npolys, sizeof(long));
	read(binfd, &nconnects, sizeof(long));
	}
    pProp->PropCount = npts;

/*  Compute data size  */
    for (i = 0; i < strlen(pProp->DataFormat); i++)
	{
	switch (pProp->DataFormat[i])
	    {
	    case 'i': size[i] = sizeof(long);
		      padding[i] = ((datasize % 4) == 0) ?
				0 : 4 - datasize % 4;
		      strcpy(format[i], "%d");
		      break;
	    case 'f': size[i] = sizeof(float);
		      padding[i] = ((datasize % 4) == 0) ?
				0 : 4 - datasize % 4;
		      strcpy(format[i], "%f");
		      break;
	    case 'd': size[i] = sizeof(double);
		      padding[i] = ((datasize % 4) == 0) ?
				0 : 4 - datasize % 4;
		      strcpy(format[i], "%F");
		      break;
	    case 'h': size[i] = sizeof(short);
		      padding[i] = ((datasize % 2) == 0) ? 0 : 1;
		      strcpy(format[i], "%hd");
		      break;
	    case 'b': size[i] = sizeof(char);
		      padding[i] = 0;
		      strcpy(format[i], "%d");
		      break;
	    case 's': size[i] = sizeof(char *);
		      padding[i] = ((datasize % 4) == 0) ?
				0 : 4 - datasize % 4;
		      strcpy(format[i], "%s");
		      nostrings = 0;
		      break;
	    default:  fprintf(stderr, "OFFReadIndexedPoly: data format not ");
		      fprintf(stderr, "valid for indexed poly type\n");
		      return (-1);
	    }
	datasize += padding[i] + size[i];
	}

    endpad = ((datasize % 4) == 0) ? 0 : 4 - datasize % 4;
    datasize += endpad;

/*  Allocate memory for the points and vertices  */
    pProp->PropData = (char *) malloc(sizeof(long) * 3
		+ datasize * npts
		+ sizeof(short) * npolys 
		+ sizeof(short) * nconnects);

    ptr = pProp->PropData;
    lptr = (long *) ptr;
    *lptr++ = npts;
    *lptr++ = npolys;
    *lptr++ = nconnects;
    ptr = (char *) lptr;

    if (type == OFF_ASCII)	/* Read info from the ascii file */
	{
    /*  Read in all the points  */
	for(i = 0; i < npts; i++)
	    {
	    for(j = 0; j < strlen(pProp->DataFormat); j++)
		{
		ptr += padding[j];
		if (pProp->DataFormat[j] == 's')
		    {
		    fscanf(ascfd, format[j], bigstr);
		    lptr = (long *) ptr;
		    *lptr = (long) malloc(strlen(bigstr) + 1);
		    strcpy(*lptr, bigstr);
		    }
		else if (pProp->DataFormat[j] == 'b')
		    { fscanf(ascfd, format[j], &ch); *ptr = ch; }
		else
		    fscanf(ascfd, format[j], ptr);

		ptr += size[j];
		}

	    ptr += endpad;
	    }

    /*  Read in all the polygon counts and connectivity info */
	ptr2 = ptr + sizeof(short) * npolys;
	for(i = 0; i < npolys; i++)
	    {
	    fscanf(ascfd,"%hd", (short *) ptr);
	    for (j = 0; j < *((short *) ptr); j++)
		{ fscanf(ascfd,"%hd", (short *) ptr2); ptr2+= sizeof(short); }
	    ptr += sizeof(short);
	    }

	}

    else	/* Read info from the binary file */

	{
	if (nostrings)
	    {
	    read(binfd, ptr, datasize * npts);	 /* Read object vertex array */
	    ptr += datasize * npts;
	    }
	else
	    {
	    for(i = 0; i < npts; i++)
		{
		for(j = 0; j < strlen(pProp->DataFormat); j++)
		    {
		    ptr += padding[j];
		    if (padding[j] != 0) read(binfd, ptr, padding[j]);
		    if (strcmp(format[j], "%s") != 0)
			read(binfd, ptr, size[j]);
		    else
			{
			read(binfd, &strlength, sizeof(char *));
			lptr = (long *) ptr;
			*lptr = (long) malloc(strlength);
			read(binfd, bigstr, strlength);
			strcpy(*lptr, bigstr);
			if ((strlength % 4) != 0)
			    read(binfd, &junk, 4 - strlength % 4);
			}
		    ptr += size[j];
		    }
		if (endpad != 0) read(binfd, ptr, endpad);
		ptr += endpad;
		}
	    }

    /*  Read poly count array */
	read(binfd, ptr, sizeof(short) * (npolys + nconnects));
	}

/*  Close the data file  */
    if (type == OFF_ASCII)
	fclose(ascfd);
    else
	close(binfd);

    return(0);
    }