/*  Copyright by Juergen Dollinger 1995 */
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
/* #define SIZE 1024 */
#define SIZE 256

int getline(char line[]){
	int c,i=0;
	while((c=getchar())!=EOF && c!='\n') {line[i]=c; i++; }
	line[i]='\0';
	return c!=EOF;
}


typedef struct Zelle{
char color;
char which;
char wall;
char e;
char ne;
char nw;
char w;
char sw;
char se;
char random;
} zelle;

zelle cell[SIZE][SIZE];

int printgif(FILE* outpipe){
	int count=1,i,j;
	fprintf(outpipe,"P3 %d %d 1 \n",SIZE,SIZE);
	count=1;
	for(i=0;i<SIZE;i++)
		for(j=0;j<SIZE;j++){
			if(cell[i][j].color=='1') fprintf(outpipe,"1 0 0 ");
			else if(cell[i][j].color=='2') fprintf(outpipe,"0 1 0 ");
			else if(cell[i][j].color=='3') fprintf(outpipe,"0 0 1 ");
			else if(cell[i][j].color=='0') fprintf(outpipe,"0 0 0 ");
			else {
				fprintf(outpipe,"0 0 0 "); 
				printf("error color=%d",cell[i][j].color);
				}
			if(count++%20==0)
				fprintf(outpipe,"\n");
		}
	fprintf(outpipe," \n");
	pclose(outpipe);
}

main(){
int time;
char line[80];
char automatonname[]="gas2";
char outfilename[19];
char command[24];
FILE* outpipe;
while(getline(line))
	if(line[0]>='0' && line[0]<='9')
		{ time=atoi(line); 
		if(time!=0){ printgif(outpipe);
		}
		strcpy(outfilename,automatonname);
		strcat(outfilename,".");
		if(time<10000)strcat(outfilename,"0");
		if(time<1000)strcat(outfilename,"0");
		if(time<100)strcat(outfilename,"0");
		if(time<10)strcat(outfilename,"0");
		strcat(outfilename,line);
		strcat(outfilename,".gif");
		strcpy(command,"ppmtogif >");
		strcat(command,outfilename);
		fprintf(stderr,"%s\n",outfilename);
		outpipe=(FILE*)popen(command, "w");
		}
	else if(line[0]=='['){
		int x,y,i;
		/* 
		Format:
		0
		[0, 0] = 0, 1, 1, 0, 0, 0, 0, 0, 0, 0
		[0, 1] = 0, 0, 1, 0, 0, 0, 0, 0, 0, 0*/
		i=1; x=0;
		while(line[i]!=','){x=10*x+ (line[i] - '0');i++;}
		i+=2; y=0;
		while(line[i]!=']'){y=10*y+ (line[i] - '0');i++;}
		i+=4;
		if(line[i]!=',')
			cell[x][y].color=line[i]; 
		i+=3;
		cell[x][y].which=line[i] -'0';
		i+=3;
		cell[x][y].wall=line[i] -'0';
		i+=3;
		cell[x][y].e=line[i] -'0';
		i+=3;
		cell[x][y].ne=line[i] -'0';
		i+=3;
		cell[x][y].nw=line[i] -'0';
		i+=3;
		cell[x][y].w=line[i] -'0';
		i+=3;
		cell[x][y].sw=line[i] -'0';
		i+=3;
		cell[x][y].se=line[i] -'0';
		i+=3;
		cell[x][y].random=0;
		while(line[i]>='0'&&line[i]<='9'){cell[x][y].random=10*cell[x][y].random +(line[i] -'0');i++;}

		}
	else
		printf("ignoring comment");
	/* endif */
/* endwhile (Keine Eingabe mehr)*/
printgif(outpipe);/* letztes file schreiben als gif */
{ /* letztes file schreiben zum Weiterarbeiten */
int i,j;
FILE * savefile;
savefile = fopen("tmp.cla", "w");
fprintf(savefile,"0\n");
for(i=0;i<SIZE;i++)
	for(j=0;j<SIZE;j++){
		fprintf(savefile,"[%d, %d] = %c, %d, %d, %d, %d, %d, %d, %d, %d, %d\n",i,j,cell[i][j].color, cell[i][j].which, cell[i][j].wall, cell[i][j].e, cell[i][j].ne, cell[i][j].nw, cell[i][j].w, cell[i][j].sw, cell[i][j].se, cell[i][j].random);
	}
fprintf(savefile," \n");
pclose(savefile);
}

} /* end main */

