#11 Article 3260 Posted at 1989/07/14 23:28:32 by NOZOMU (MAP041) [PRG.ML1]
Subject: MX1.C 評価版
ほへほへ、何とか約束通り今日中にやってきました。
今回は、テキスト形式の発表ということで、あまり厳重なチェックはしてません。
また、移植性の問題から、タイムスタンプをヘッダーから持って来る事もして
いません。 (うぅ~~~む、物は言いようである。)
で、テキストコンバートする際の文字コードが、かなり飛び飛びになっていますので
他の言語等に移植する際は気を付けて下さい。
んでは、mx1.cまいります。 ヘッダー、フッターなどの改良案等ありましたら、
すかさずこのボードまでよろしくぅ~~~。
/*
ML1 data Xform utility 'mx1.c' appreciate ver1.00
Copyright 1989 (C) by Yoshinori Saito (MAP041 NOZOMU)
*/
#include <stdio.h>
#include <string.h>
char cnv[130]={"\
!#$%&()*+-./0123\
456789:;<=>?ABCD\
EFGHIJKLMNOPQRST\
UVWXYZ[]^_abcdef\
ghijklmnopqrstuv\
wxyz{|}~。「」、・ヲァィ\
ゥェォャュョッーアイウエオカキク\
ケコサシスセソタチツテトナニヌネ\
"};
int buf_ptr;
unsigned char bit_fild;
unsigned char buf[16000];
struct FILE_STAT
{
unsigned char file_name[80];
short size;
short lines;
};
void main(int,char *[]);
void xfer_txt(char *);
int restore_bit_data(int);
void xfer_bin(char *);
int find_head(FILE *,struct FILE_STAT *);
void store_bit_data(int,int);
void main(argc,argv)
int argc;
char *argv[];
{
char strtmp0[256],strtmp1[256];
if (argc==1)
{
printf("ML1 data Xform utility 'mx1.x' appreciate ver1.0\n\n");
printf("Usage : mx1 [logfile] or mx1 file.ml1\n");
return;
}
strcpy(strtmp0,argv[1]);
strlwr(strtmp0);
strcpy(strtmp1,&strtmp0[strlen(strtmp0)-4]);
if (strcmp(strtmp1,".ml1")==0) xfer_txt(strtmp0);
else xfer_bin(strtmp0);
}
void xfer_txt(f_name)
char *f_name;
{
FILE *ifp,*ofp;
unsigned char strtmp0[256],strtmp1[256],strtmp2[256],strtmp3[256];
short size,lines;
int i,l;
if ((ifp=fopen(f_name,"rb"))==(FILE *)NULL)
{
fprintf(stderr,"%s can't found.\n",f_name);
return;
}
strcpy(strtmp0,f_name);
strcpy(&strtmp0[strlen(strtmp0)-4],".mx1");
if ((ofp=fopen(strtmp0,"wt"))==(FILE *)NULL)
{
fprintf(stderr,"%s can't open.\n",strtmp0);
return;
}
fread(strtmp1,1,96,ifp);
strncpy(strtmp2,strtmp1+18,12);
strncpy(strtmp3,strtmp1+64,32);
size=strtmp1[30]*256+strtmp1[31];
lines=size*8/7/78+2;
if (size%78!=0) ++lines;
fseek(ifp,0,0);
fread(buf,1,size,ifp);
buf_ptr=0;
bit_fild=0x80;
l=0;
sprintf(strtmp0,"@@@ %s by %s : %s (%dlines) @@@",f_name,strtmp2,strtmp3,lines);
printf("%s\n",strtmp0);
fprintf(ofp,"%s\n",strtmp0);
while (buf_ptr<size)
{
fprintf(ofp,"%c",cnv[restore_bit_data(7)]);
if (++l==78)
{
l=0;
fprintf(ofp,"\n");
}
};
for (i=l;i<78;++i) fprintf(ofp,"%c",cnv[0]);
fprintf(ofp,"\n");
sprintf(strtmp0,"@@@ %s by %s : %s (end of data) @@@",f_name,strtmp2,strtmp3,lines);
fprintf(ofp,"%s\n",strtmp0);
fclose(ifp);
fclose(ofp);
return;
}
int restore_bit_data(bit)
int bit;
{
int i,n=0;
for (i=0;i<bit;++i)
{
n<<=1;
if (buf[buf_ptr]&bit_fild)
n++;
if ((bit_fild>>=1)==0)
{
buf_ptr++;
bit_fild=0x80;
}
}
return n;
}
void xfer_bin(f_name)
char *f_name;
{
FILE *ifp,*ofp;
unsigned char strtmp0[256];
struct FILE_STAT fs;
int i,j;
if ((ifp=fopen(f_name,"rt"))==(FILE *)NULL)
{
fprintf(stderr,"%s can't found.\n",f_name);
return;
}
for (;;)
{
if (find_head(ifp,&fs)==0)
{
fclose(ifp);
return;
}
buf_ptr=0;
bit_fild=0x80;
for (i=1;(i<fs.lines-1)&&(!feof(ifp));++i)
{
fgets(strtmp0,256,ifp);
for (j=0;j<78;++j)
store_bit_data(7,(int)(strchr(cnv,strtmp0[j])-cnv));
}
fs.size=buf[30]*256+buf[31];
fgets(strtmp0,256,ifp);
if ((ofp=fopen(fs.file_name,"wb"))==(FILE *)NULL)
{
fprintf(stderr,"%s can't open.\n",fs.file_name);
return;
}
fwrite(buf,1,fs.size,ofp);
fclose(ofp);
}
}
int find_head(ifp,file_stat)
FILE *ifp;
struct FILE_STAT *file_stat;
{
unsigned char strtmp0[256],strtmp1[256],strtmp2[256],strtmp3[256];
int i,l;
do
{
fgets(strtmp0,256,ifp);
if (feof(ifp)) return 0;
}
while (sscanf(strtmp0,"@@@ %s by %s : ",strtmp1,strtmp2)!=2);
if (strlen(strtmp1)<5) return 0;
if (strcmp(strrchr(strtmp1,'.'),".ml1")) return 0;
if (strlen(strtmp2)>12) return 0;
if (strchr(strtmp0,':')==(char *)NULL) return 0;
strcpy(strtmp3,strchr(strtmp0,':')+2);
*(strrchr(strtmp3,'(')-1)=0;
if (sscanf(strrchr(strtmp0,'(')+1,"%hd",&file_stat->lines)==0) return 0;
printf("File name : %s\n",strtmp1);
printf("Painter : %s\n",strtmp2);
printf("Comment : %s\n",strtmp3);
printf("Lines : %d\n",file_stat->lines);
strcpy(file_stat->file_name,strtmp1);
return 1;
}
void store_bit_data(bit,num)
int bit,num;
{
int bit_mask=1<<bit-1;
int i;
for (i=0;i<bit;++i)
{
if (num&bit_mask)
buf[buf_ptr]|=bit_fild;
else
buf[buf_ptr]&=bit_fild^0xff;
bit_mask>>=1;
if ((bit_fild>>=1)==0)
{
buf_ptr++;
bit_fild=0x80;
}
}
}