ftp.nice.ch/pub/next/graphics/3d/StereoScope.3.7.N.bs.tar.gz#/StereoScope-3.7/readData.c

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

#include <stdio.h>
#define equal(s,w)	(strcmp(s,w) == 0)
#undef  min
#define min(a,b)	((a) < (b) ? (a) : (b))
#undef  max
#define max(a,b)	((a) > (b) ? (a) : (b))
#define ctrl(c)	        ((c) - 'A' + 1)	/* control character mapping */
#define plural(n)	((n) == 1 ? "" : "s")
#define thest(n)	((n) == 1 ? "-st" : ((n) == 2 ? "-nd" : ((n) == 3 ? "-rd" \
 : "-th")))
#define lastch(s)	s[strlen(s)-1]
#define no_ret(s)	{ register int xyz; /* varname is for lint */	      \
		          for (xyz=strlen(s)-1; xyz >= 0 && 		      \
				(s[xyz] == '\r' || s[xyz] == '\n'); )	      \
			     s[xyz--] = '\0';                                 \
			}
#define first_word(s,w) (strncmp(s,w, strlen(w)) == 0)
#define whitespace(c)	((c) == ' ' || (c) == '\t')
#define ok_char(c)	(isalnum(c) || (c) == '-' || (c) == '_' || (c) == '.')
#define quote(c)	((c) == '"' || (c) == '\'') 
#define onoff(n)	(n == 0 ? "OFF" : "ON")
/* find tab stops preceding or following a given column position 'a', where
 * the column position starts counting from 1, NOT 0!
 * The external integer "tabspacing" must be declared to use this. */
#define prev_tab(a,tb)	(((((a-1)/(tb)))*(tb))+1)
#define next_tab(a,tb)	(((((a-1)/(tb))+1)*(tb))+1)
#define MAXALLOC 100
#define MAXNUM	10000
#define MAXLINE	10000
#define NO -1;
#define YES !NO;

typedef enum {
  NEITHER = 0,
  LINES,
  POINTS,
  BOTH
  } pts_or_lines;

typedef struct {
  float **all;
  float **displayed;
  int   npts;
  pts_or_lines type;
  float	radius;
  float	shade;
} datapoints;

main()
{
	FILE *infile;
	char line[128], thisline[128], *aPtr, *word();
	char cmd, fileName[128];
	int nwords,i, j, count, tlines, ncols, nlines, max_path, expected_cols = 3;
	float x[3], y,  *scratch, *fpt, *path, radius, shade, argv[4];
	float *xx[3];
	datapoints **toshow;
	datapoints *thesedata;
 	datapoints **all_data = toshow = (datapoints **)calloc(MAXALLOC, sizeof(datapoints *));
	void float_copy();
	long pts_read = 0;
	long num_allocs = 0;
	tlines = 0;
  	max_path = 100;
	
	path = (float *) calloc(2*max_path, sizeof(float));
	scratch = (float *) calloc(MAXNUM, sizeof(float));
				/* release existing data */
	while ((thesedata = *all_data) != (datapoints *) NULL) {
    	free(thesedata->displayed);
    	count = 0;
   		while(thesedata->all[count] != (float *) NULL) {
     		free(thesedata->all[count]);
    		count++;
    		}
    	free(thesedata->all);
   		free(thesedata);
    	*all_data++ = (datapoints *) NULL;
  		}
		
	all_data = toshow;		/* read new data */
	
	printf("Enter File Name: ");
	gets(fileName);
	infile = fopen(fileName,"r");
	pts_read = 0;
	
	while(fgets(line,128,infile) != NULL)	{
		if(lastch(line) == '\n') lastch(line) = '\0';
		if(isalpha(line[0]))	{
			if (pts_read != 0)	{
/* create the datapoints structure and store the values */
    thesedata = *all_data++ = (datapoints *) malloc(sizeof(datapoints));
	if (++num_allocs > MAXALLOC)	{
		sprintf(thisline, "Number Distinct Groups > MAXALLOC");
/*		[self showError:thisline];	*/
		return NO;
		}
    switch(cmd) {
    case 'n':
      thesedata->type = NEITHER;
      break;
    case 'l':
      thesedata->type = LINES;
      break;
    case 'p':
      thesedata->type = POINTS;
	  thesedata->radius = radius;
	  thesedata->shade = shade;
/*
 *		Store radius, shade, label here in thesedata->radius, etc.
*/
      break;
    case 'b':
      thesedata->type = BOTH;
      break;
    }
    thesedata->all = (float **)calloc((size_t) expected_cols + 1,
			      (size_t) sizeof(float *));
    for(ncols = 0; ncols < expected_cols; ncols++) {
      thesedata->all[ncols] = (float *)calloc((size_t) nlines, sizeof(float));
      float_copy(nlines, thesedata->all[ncols], 1, scratch + ncols,
		 expected_cols);
    }
    thesedata->all[expected_cols] = (float *)NULL;
				/* make sure path is large enough */
    if (2*nlines >= max_path)
      realloc(path, (2*nlines)*sizeof(float));
    thesedata->npts = nlines;
    memmove(thesedata->displayed = (float **) calloc(3, sizeof(float *)),
	    thesedata->all, 3 * sizeof(float *));	  
			}
			cmd = tolower(line[0]);
			nwords = max(words(line),3);
			for(i=1;i<3;i++)	argv[i] = 0;
/*			printf("Type is %c",cmd,nwords);	*/
			for(i=1;i<=nwords;i++)	{
				if((aPtr = word(i,line)) != NULL)	{
					sscanf(aPtr,"%f",&argv[i]);
/*					printf(" ,arg[%d] = %f",i,argv[i]); */
					}
			}
			printf("\n");
			switch (cmd)	{
				case 'n':
					break;
				case 'l':
					break;
				case 'p':
					radius = argv[1];
					shade = argv[2];
					break;
				case 'b':
				default:
					break;
			}
			fpt = scratch;
			pts_read = 0;
			nlines = 0;
			continue;
		}
		sscanf(line,"%f %f %f",&x[0], &x[1], &x[2]);
		printf("            %f %f %f\n",x[0],x[1],x[2]);
		nlines++;
		for(i=0;i<3;i++)	{
			*fpt++ = x[i];
			if( ++pts_read == MAXNUM)	{
				sprintf(thisline, "Storage space for points exceeded.");
				return NO;
				}
			}
	}
			if (pts_read != 0)	{
/* create the datapoints structure and store the values */
    thesedata = *all_data++ = (datapoints *) malloc(sizeof(datapoints));
	if (++num_allocs > MAXALLOC)	{
		sprintf(thisline, "Number Distinct Groups > MAXALLOC");
/*		[self showError:thisline];	*/
		return NO;
		}
    switch(cmd) {
    case 'n':
      thesedata->type = NEITHER;
      break;
    case 'l':
      thesedata->type = LINES;
      break;
    case 'p':
      thesedata->type = POINTS;
	  thesedata->radius = radius;
	  thesedata->shade = shade;
/*
 *		Store radius, shade, label here in thesedata->radius, etc.
*/
      break;
    case 'b':
      thesedata->type = BOTH;
      break;
    }
    thesedata->all = (float **)calloc((size_t) expected_cols + 1,
			      (size_t) sizeof(float *));
    for(ncols = 0; ncols < expected_cols; ncols++) {
      thesedata->all[ncols] = (float *)calloc((size_t) nlines, sizeof(float));
      float_copy(nlines, thesedata->all[ncols], 1, scratch + ncols,
		 expected_cols);
    }
    thesedata->all[expected_cols] = (float *)NULL;
				/* make sure path is large enough */
    if (2*nlines >= max_path)
      realloc(path, (2*nlines)*sizeof(float));
    thesedata->npts = nlines;
    memmove(thesedata->displayed = (float **) calloc(3, sizeof(float *)),
	    thesedata->all, 3 * sizeof(float *));	  
			}
	 free(scratch);
/*  [self display];	*/
/*	Debuggin output
*/
	{	FILE *debug;
		int j;
		float *xx[3];
		
		printf("Opening debug file");

	if((debug = fopen("debug","w+")) == NULL)	{
		printf("Can't open debug file");
		return NO;
		}
	all_data = toshow;
	while ((thesedata = *all_data++) != (datapoints *) NULL) {
   		fprintf(debug,"Type is : %d ",thesedata->type);
		if (thesedata->type == POINTS)	
			fprintf(debug," with radius = %f, and shade = %f",
				thesedata->radius, thesedata->shade);
			fprintf(debug,"\n");
		for(j=0;j<expected_cols;j++)
			xx[j] = thesedata->displayed[j];
		for(i=0;i<thesedata->npts;i++)	{
		fprintf(debug,"%d ) : ",i);
			for(j=0;j<expected_cols;j++)
				fprintf(debug," %10.6f",*xx[j]++);
		fprintf(debug,"\n");
		}
  	}
	fclose(debug);
	}
/**/
	return YES;
}

int words(str)
	char *str;
{
	int count=0;
	while(*str != '\0')	{
		while(ok_char(*str)) str++; /* skip characters */
		while(whitespace(*str)) str++; /* skip whitespace */
		if(ok_char(*str))	count++;
	}
	return count;
}

char *word(i,str)
	int i;
	char *str;
{
	int count=0;
	
	if(i > words(str)) return NULL;
	while(*str != '\0')	{
		while(ok_char(*str)) str++;	/* skip characters */
		while(whitespace(*str)) str++; /* skip whitespace */
		if(ok_char(*str))
			if(++count == i)	return str;
	}
	return NULL;
}

void float_copy (register int n, register float * to, register int
		   to_inc, register float * from, register int from_inc)
{
  if (n > 0)
    while (n--) {
      *to = *from;
      to += to_inc;
      from += from_inc;
    }
}

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