#include <stdio.h>

char *getword(char *,char *);
char *getline(char *,char *);
int check_tex(char *);

int main(int argc,char ** argv)
{
    FILE *fp,*fw;
    char fn[256];
    char word[256],line[256];
    char *inputbuf = NULL,*inputback,*tmpptr;
    char *outputbuf,*outback,*bufbefore;
    int i,c;
    int fsize = 0,fsize_back = 0;

    fp = fopen(argv[1],"r");
/********************** 出力ファイルのオープン **********************/
    if((fw = fopen("ssJetSki.dat","w")) == NULL){
	printf("File open error!!%s \n","ssJetSki.dat");
	exit(1);
    }    
    for(i=1;i<argc;i++){
	strcpy(fn,argv[i]);
	
	if((fp = freopen(fn,"r",fp)) == NULL){/*ファイルのオープン */
	    printf("No such file!!\n");
	    exit(1);
	}

	while(!feof(fp)){	/* ファイルサイズを計る */
	    getc(fp);
	    fsize++; /* 各ファイルのサイズの合計 */
	}
	fsize--;
	fseek(fp,0L,SEEK_SET);
	printf("size = %d\n",fsize-fsize_back);
	inputback = inputbuf = (char *)realloc(inputback,fsize+1);
	inputbuf += fsize_back;

	while(!feof(fp)){	/*ファイルの内容をバッファにコピー*/
	    *inputbuf = getc(fp);
	    inputbuf++;
	}
	
	fsize_back = fsize;
    }
    outback = outputbuf = (char *)malloc(fsize);
#if 1   
/******* テクスチャの抜き出し ******/
    inputbuf = inputback;
    while(*inputbuf != '\0'){
	bufbefore = inputbuf;
	inputbuf = getline(inputbuf,line);
	tmpptr = getword(line,word);
	if(strcmp("unsigned",word)==0){
	    tmpptr = getword(tmpptr,word);
	    tmpptr = getword(tmpptr,word);
	    if(check_tex(word)){/* テクスチャが新しいもの */
		c = bufbefore-inputback;
		for(i=0;i<c;i++){ /* 直前までをバッファに */
		    *outputbuf++ = *inputback++;
		}
		while(1){
		    bufbefore = inputbuf;
		    inputbuf = getline(inputbuf,line);
		    if(strcmp("};",line)==0){
			bufbefore = inputbuf;
			c = bufbefore-inputback;
			for(i=0;i<c;i++){/*データバッファに*/
			    *outputbuf++ = *inputback++;
			}
			break;
		    }
		}
	    }else{
		c = bufbefore-inputback;
		for(i=0;i<c;i++){ /* 直前までをバッファに */
		    *outputbuf++ = *inputback++;
		}
		while(1){
		    bufbefore = inputbuf;
		    inputbuf = getline(inputbuf,line);
		    if(strcmp("};",line)==0){
			bufbefore = inputbuf;
			inputback = bufbefore;
			break;
		    }
		}
	    }
	}
    }
    *outputbuf = '\n';
#endif    
    fprintf(fw,"%s",outback); 
/*    fprintf(fw,"%s",inputback);*/
}

char *getword(char*buf,char *word)
{
    while(*buf==' '|| *buf=='\t' || *buf == '\n')buf++;
    while(*buf!= ' ' && *buf != '\t' && *buf != '\n' && *buf != '\0'){
	*word ++ = *buf++;
    }
    *word = '\0';
    return buf;
}

char *getline(char*buf,char *word)
{
    while(*buf==' '|| *buf=='\t' || *buf == '\n')buf++;
    while(*buf != '\n' && *buf != '\0'){
	*word ++ = *buf++;
    }
    *word = '\0';
    return buf;
}

int check_tex(char *word)
{
    int i;
    static int tex_num =0;
    static char tex_name[1024][256];

    for(i=0;i<tex_num;i++){
	if(strcmp(tex_name[i],word)==0){
	    return 0;
	}
    }
    strncpy(tex_name[tex_num],word,strlen(word)+1);
    tex_num++;
    return 1;
}
