#include <stdio.h>
#include <string.h>

char f_name1[500][18];
char f_name2[500][14];
char f_seg[500][16];
char f_sadd[500][12];
char f_eadd[500][15];
char f_kei[500][5];

char f_dami[210];

char filname[20],file_name[20],f_title[2000];
int err_flg;

main(argc,argv)
int argc;
char *argv[];
{
	FILE *fpr;
	int c,i,j,m,n;

	err_flg=0;
	
	if(argc!=2){
		printf("Parameter error !!!\n");
		exit(1);
	}
	if((fpr=fopen(argv[1],"rb"))==0){
		printf("can't open %s\n",argv[1]);
		exit(1);
	}
	
	strcpy(file_name,argv[1]);
	
	for(i=0;*argv[1]!='.';i++){
		filname[i]=(*(argv[1])++);
	}
	
	m=0;
	for(i=0;i<3;i++){
		while((f_title[m++]=fgetc(fpr))!='\n'){}
	}
	
	i=0;
	j=0;
	n=1;
	c=fgetc(fpr);
	while(c!=EOF){
		if(c!=' '){
			switch(i){
				case 0 :
					f_name1[n][j++]=c;
					while((c=fgetc(fpr))!=' ' && c!=EOF){
						f_name1[n][j++]=c;
					}
					f_name1[n][j]=NULL;
					i++;
					j=0;
					break;
				case 1 :
					f_name2[n][j++]=c;
					while((c=fgetc(fpr))!=' ' && c!=EOF){
						f_name2[n][j++]=c;
					}
					f_name2[n][j]=NULL;
					i++;
					j=0;
					break;
				case 2 :
					f_seg[n][j++]=c;
					while((c=fgetc(fpr))!=' ' && c!=EOF){
						f_seg[n][j++]=c;
					}
					f_seg[n][j]=NULL;
					i++;
					j=0;
					break;
				case 3 :
					f_sadd[n][j++]=c;
					while((c=fgetc(fpr))!=' ' && c!=EOF){
						f_sadd[n][j++]=c;
					}
					f_sadd[n][j]=NULL;
					i++;
					j=0;
					break;
				case 4 :
					f_eadd[n][j++]=c;
					while((c=fgetc(fpr))!=' ' && c!=EOF){
						f_eadd[n][j++]=c;
					}
					f_eadd[n][j]=NULL;
					i++;
					j=0;
					break;
				case 5 :
					f_kei[n][j++]=c;
					while((c=fgetc(fpr))!=' ' && c!='\n' && c!=12 && c!=EOF){
						f_kei[n][j++]=c;
					}
					f_kei[n][j]=NULL;
					if(c==12){
						fgets(f_dami,200,fpr);
						fgets(f_dami,200,fpr);
						fgets(f_dami,200,fpr);
						fgets(f_dami,200,fpr);
					}
					j=0;
					i=0;
					n++;
					break;
			}
		}
		c=fgetc(fpr);
	}
	
	fclose(fpr);
	
	sort_sub(n);
	
	strcat(filname,".hex");
	
	overlap_bank(n);
	
	file_input(m,n);
	
	if(err_flg!=0){
		error_exit();
	}
	
/*	
	for(i=1;i<n;i++)
		printf("%15s %11s %s %s %s %s\n",f_name1[i],f_name2[i],f_seg[i],f_sadd[i],f_eadd[i],f_kei[i]);
*/
}

sort_sub(n)
int n;
{
	int i,j,k;
	i=1;
	while(i<n-1){
		j=i+1;
		k=i;
		while(j<n){
			if(strcmp(f_sadd[k],f_sadd[j])>0){
				k=j;
			}
			j++;
		}
		strcpy(f_name1[0],f_name1[i]);
		strcpy(f_name1[i],f_name1[k]);
		strcpy(f_name1[k],f_name1[0]);
		
		strcpy(f_name2[0],f_name2[i]);
		strcpy(f_name2[i],f_name2[k]);
		strcpy(f_name2[k],f_name2[0]);
		
		strcpy(f_seg[0],f_seg[i]);
		strcpy(f_seg[i],f_seg[k]);
		strcpy(f_seg[k],f_seg[0]);
		
		strcpy(f_sadd[0],f_sadd[i]);
		strcpy(f_sadd[i],f_sadd[k]);
		strcpy(f_sadd[k],f_sadd[0]);
		
		strcpy(f_eadd[0],f_eadd[i]);
		strcpy(f_eadd[i],f_eadd[k]);
		strcpy(f_eadd[k],f_eadd[0]);
		
		strcpy(f_kei[0],f_kei[i]);
		strcpy(f_kei[i],f_kei[k]);
		strcpy(f_kei[k],f_kei[0]);
		
		i++;
	}
}

overlap_bank(n)
int n;
{
	char s_bank[3];
	char e_bank[3];
	int i,j,bank_flg;
	
	i=1;
	while(i<n){
		bank_flg=0;
		for(j=0;j<2;j++){
			s_bank[j]=f_sadd[i][j];
			e_bank[j]=f_eadd[i][j];
		}
		s_bank[j]=NULL;
		e_bank[j]=NULL;
		if(strcmp(s_bank,e_bank)!=0){
			err_sub1(i);
			bank_flg++;
		}
		if(bank_flg==0 && i<n-1){
			overlap_add(i);
		}
		i++;
	}
}

overlap_add(i)
int i;
{
	int j;
	
	j=i+1;
	if(strcmp(f_eadd[i],f_sadd[j])>=0){
		err_sub2(i,j);
	}
}

err_sub1(i)
int i;
{
	printf("Error No.1 %-15s\tSTART:%s END:%s !!!\n\n",f_name1[i],f_sadd[i],f_eadd[i]);
	
	err_flg++;
}

err_sub2(i,j)
int i,j;
{
	printf("Error No.2 %-15s\tSTART:%s END:%s !!!\n",f_name1[i],f_sadd[i],f_eadd[i]);
	printf("           %-15s\tSTART:%s END:%s !!!\n\n",f_name1[j],f_sadd[j],f_eadd[j]);
	
	err_flg++;
}

error_exit()
{
	char filname2[23];
	
	strcpy(filname2,"rm ");
	strcat(filname2,filname);
	printf("%s\n",filname2);
	system(filname2);
}

file_input(m,n)
int m,n;
{
	FILE *fpw;
	int i;
	char c;
	
	printf("%s\n",file_name);
	
	if((fpw=fopen(file_name,"wb"))==0){
		printf("can't open %s\n",file_name);
		exit(1);
	}
	
	for(i=0;i<m;i++){
		fputc(f_title[i],fpw);
	}
	
	i=1;
	while(i<n){
		fprintf(fpw,"   ");
		fprintf(fpw,"%-17s%-13s%-4s",f_name1[i],f_name2[i],f_seg[i]);
		fprintf(fpw,"           ");
		fprintf(fpw,"%-11s%-14s%-4s\n",f_sadd[i],f_eadd[i],f_kei[i]);
		i++;
	}
	
	fclose(fpw);
}
