[net.sources] Graphics source in C: hsalgs/update_obj.c

ken@turtleva.UUCP (Ken Turkowski) (12/22/83)

echo x - hsalgs/update_obj.c
cat >hsalgs/update_obj.c <<'!Funky!Stuff!'
/* update_obj.c - change entries in a .obj file */

#include <stdio.h>

#define LINE_LENGTH	160
#define TRUE		1
#define FALSE		0
#define NULLCHAR	'\0'

main(argc,argv)
int argc;    char **argv;
{   char *keywd,line[LINE_LENGTH],remainder[LINE_LENGTH];
    char term[LINE_LENGTH],fields[LINE_LENGTH],*filename;
    double atof(),red,grn,blu,shininess,transmittance,trns_pwr;
    short i,shin_done,trans_done,keywd_fnd,shin_fnd,trans_fnd;
    FILE *input,*output;

    if (argc < 4)  error("usage:  update_obj filename keyword [n fields]");

    filename = argv[1];
    keywd = argv[2];

    if (strcmp(keywd,"color") == 0)
    {   red = atof(argv[3]);    grn = atof(argv[4]);    blu = atof(argv[5]);
    	if (argc > 6) {  shininess = atof(argv[6]);   shin_done = TRUE;  }
    	if (argc > 7) {  transmittance = atof(argv[7]);  trans_done = TRUE;  }
    	if (argc > 8) trns_pwr = atof(argv[8]);
    }
    else for (i=3; i<argc; i++) /* make rest of command line into 1 string */
	 {   strcat(fields,argv[i]);   strcat(fields," ");   }

    if ((input = fopen(filename,"r")) == NULL)
	error("update_obj can't open input file - %s",filename);
    
    output = fopen("update_obj.tmp","w");	/* temporary output file */

    while (fgets(line,LINE_LENGTH,input) != NULL)
    {   get_term(line,term,remainder);
	
	if (strcmp(keywd,"color") == 0)
	{   if (strcmp(term,"color") == 0)
	    {   fprintf(output,"color\t\t%g %g %g\n",red,grn,blu);
		keywd_fnd = TRUE;   }
	    else if ((strcmp(term,"shininess") == 0) && shin_done)
	    {   fprintf(output,"shininess\t%g\n",shininess);
		shin_fnd = TRUE;   }
	    else if ((strcmp(term,"transmittance") == 0) && trans_done)
	    {   fprintf(output,"transmittance\t%g %g\n",transmittance,trns_pwr);
		trans_fnd = TRUE;   }
	    else fprintf(output,"%s",line);
	}
	else if (strcmp(keywd,term) == 0)
	{   fprintf(output,"%s\t\t%s\n",keywd,fields);    keywd_fnd = TRUE;   }
	else if (strlen(line) > 2) fprintf(output,"%s",line);
    }
    if (strcmp(keywd,"color") == 0)
    {   if (!keywd_fnd)  fprintf(output,"color\t\t%g %g %g\n",red,grn,blu);
	if ((!shin_fnd ) && (shin_done))
	    fprintf(output,"shininess\t%g\n",shininess);
	if ((!trans_fnd) && (trans_done))
	    fprintf(output,"transmittance\t%g %g\n",transmittance,trns_pwr);
    }
    else if (!keywd_fnd) fprintf(output,"%s\t\t%s\n",keywd,fields);

    close(input);    close(output);
    system(sprintf(line,"mv update_obj.tmp %s",filename));
}





/* +++++++++++++++++++++ GET_TERM +++++++++++++++++++++++++++++++++ */
get_term(instrg,term,remainder)		/* remove first term from string */
char *instrg,*term,*remainder;  /* blanks, tabs, nulls, commas are separators */
{   short i,index1,index2;
    index1 = 0;				/* find first non-separator */
    while ((instrg[index1] == ' ') 	|| (instrg[index1] == '\t') ||
	   (instrg[index1] == NULLCHAR) || (instrg[index1] == ',' )) index1++;
    index2 = index1;			/* find next separator */
    while ((instrg[index2] != ' ') 	&& (instrg[index2] != '\t') &&
	   (instrg[index2] != NULLCHAR) && (instrg[index2] != ',' ) &&
	   (instrg[index2] != '\n'))				     index2++;
    for (i=index1; i<index2; i++)  term[i-index1] = instrg[i];
    term[i-index1] = NULLCHAR;
    while ((instrg[i] != NULLCHAR) && (instrg[i] != '\n'))
	{  remainder[i-index2] = instrg[i];  i++;  }
    remainder[i-index2] = NULLCHAR;
}



!Funky!Stuff!