/****************************************************************/
/*	メッセージコンバーター					*/
/*		( gcc msgcnv.c -o msgcnv )			*/
/*						by S.Takahata	*/
/****************************************************************/
#include	<stdio.h>
#include	<string.h>

#define		MAXMUM	1000

typedef struct{
    unsigned short	name;
    unsigned char	label[6];
}LABEL;

static LABEL	cnvdat[MAXMUM];
static int	num;
static FILE	*fpr, *fpw;

static int moji_label(unsigned short s);

void main(int argc, char **argv)
{
    int		i;
    unsigned char	in_file[40], out_file[44], label_file[40], title[40];
    
    if(argc < 2){
	printf("Input Error !!\n");
	printf("msgcnv [入力ファイル名] (-l[ラベル名]) (-h[変換ファイル名])\n");
	exit(1);
    }
    
    strcpy(in_file, argv[1]);
    strcpy(out_file, argv[1]);
    strcat(out_file, ".OUT");
    strcpy(label_file, "msg.LBL");
    title[0] = '\0';
    if(argc >= 3){
	for(i = 2; i < argc; i++){
	    if(*argv[i] != '-'){
		printf("Input Error !!\n");
		printf("msgcnv [入力ファイル名] (-l[ラベル名]) (-h[変換ファイル名])\n");
		exit(1);
	    }
	    switch(*(argv[i]+1)){
		case 'l' :
		case 'L' :
		    strcpy(title, argv[i]+2);
		    strcat(title, "\tEQU\t$\n");
		    break;
		case 'h' :
		case 'H' :
		    strcpy(label_file, argv[i]+2);
		    break;
		default :
		    printf("Input Error !!\n");
		    printf("msgcnv [入力ファイル名] (-l[ラベル名]) (-h[変換ファイル名])\n");
		    exit(1);
	    }
	}
    }

    if((fpr = fopen(label_file, "r")) == NULL){
	printf("File Open Error !! [%s]\n", label_file);
	exit(1);
    }

    num = fread(cnvdat, sizeof(LABEL), MAXMUM, fpr);
    
    for(i=0; i<num; i++)
	cnvdat[i].label[5] = '\0';
    
    fclose(fpr);

    if((fpr = fopen(in_file, "r")) == NULL){
	printf("File Open Error !! [%s]\n", in_file);
	exit(1);
    }
    if((fpw = fopen(out_file, "w")) == NULL){
	printf("File Open Error !! [%s]\n", out_file);
	exit(1);
    }

    printf("開始！！ <<< %s >>>\n", in_file);
    
    fprintf(fpw, "%s", title);
#if 0
    printf("%s", title);
#endif
    mojicheck();
    
    fclose(fpr);
    fclose(fpw);

    printf("終了！！\n");
}

mojicheck()
{
    static unsigned char	number[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'},
    		Mspc[2] = "　", Msp2[2] = "□";
    int		i, j, c, no;
    unsigned char	str[256], suu[3];
    unsigned short	s, *spc, *sp2;

    spc = (unsigned short *)Mspc;
    sp2 = (unsigned short *)Msp2;
	
    i = j = 0;
    while((c = getc(fpr)) != EOF){
	if(i == 0){
	    if(c < 0x20){
		fprintf(fpw,";\n");
#if 0
		puts(";");
#endif
		i = j = 0;
	    }
	    else{
		strcpy(str, "\t\tBYTE\t");
		s = (unsigned short)c << 8;
		c = getc(fpr);
		s = s | (unsigned short)c;
		if((no = moji_label(s)) >= 0){
		    if(cnvdat[no].name != *spc){
			if(j > 1){
			    strcat(str, cnvdat[moji_label(*sp2)].label);
			    strcat(str, ",");
			    suu[0] = number[(j/10)%10];
			    suu[1] = number[j%10];
			    suu[2] = '\0';
			    strcat(str, suu);
			    strcat(str, ",");
			}
			else if(j == 1){
			    strcat(str, cnvdat[moji_label(*spc)].label);
			    strcat(str, ",");
			}
			strcat(str, cnvdat[no].label);
			j = 0;
			i ++;
		    }
		    else
			j ++;
		}
	    }
	}
	else{
	    if(c < 0x20){
		strcat(str,"\n");
		fprintf(fpw,"%s",str);
#if 0
		printf("%s",str);
#endif
		i = j = 0;
	    }
	    else{
		s = (unsigned short)c << 8;
		c = getc(fpr);
		s = s | (unsigned short)c;
		if((no = moji_label(s)) >= 0){
		    if(cnvdat[no].name != *spc){
			if(j > 1){
			    strcat(str, ",");
			    strcat(str, cnvdat[moji_label(*sp2)].label);
			    strcat(str, ",");
			    suu[0] = number[j%10];
			    suu[1] = number[(j/10)%10];
			    suu[2] = '\0';
			    strcat(str, suu);
			}
			else if(j == 1){
			    strcat(str, ",");
			    strcat(str, cnvdat[moji_label(*spc)].label);
			}
			strcat(str, ",");
			strcat(str, cnvdat[no].label);
			j = 0;
			i ++;
		    }
		    else
			j ++;
		}
	    }
	}
    }
}

static int moji_label(unsigned short s)
{
    int		i;

    for(i = 0; i < num; i ++){
	if(s == cnvdat[i].name)	return i;
    }
    return -1;
}
