|
全部書いてしまっていいものなのか?とりあえず、もう書かれてしまったので、参考までに、自作の関数を載せておきます。
void UTF8toSJIS(FILE *fin,FILE *fout)
{
int c,cb;
unsigned char mb[2],utf8[3];
unsigned short wc;
while((c=fgetc(fin))!=EOF) if(c>>7) {
if(c>>6) {
utf8[0]=GET4BITS(c);
if((c=fgetc(fin))==EOF) {
fprintf(stderr,MSG_WRN_INVLCHR,(int)utf8[0]);
break;
}
utf8[1]=GET6BITS(c);
if((c=fgetc(fin))==EOF) {
fprintf(stderr,MSG_WRN_INVLCHR,(((int)utf8[0]<<6)|((int)utf8[1])));
break;
}
utf8[2]=GET6BITS(c);
wc=utf8[0];
wc<<=6;
wc+=utf8[1];
wc<<=6;
wc+=utf8[2];
if(!wc) fprintf(stderr,MSG_WRN_INVLCHR,((int)utf8[0]<<12)|((int)utf8[1]<<6)|((int)utf8[2]));
mb[1]=0;
wctomb(mb,wc);
if((mb[0]>0x7f)&&(!mb[1])) cb=1; else cb=2;
if(!fwrite(mb,cb,1,fout)) {
fprintf(stderr,MSG_ERR_OUTPUT);
break;
}
} else {
utf8[0]=GET5BITS(c);
if((c=fgetc(fin))==EOF) {
fprintf(stderr,MSG_WRN_INVLCHR,(int)utf8[0]);
break;
}
utf8[1]=GET6BITS(c);
wc=utf8[0];
wc<<=6;
wc+=utf8[1];
if(!wc) fprintf(stderr,MSG_WRN_INVLCHR,((int)utf8[0]<<4)|((int)utf8[1]));
wctomb(mb,wc);
if(!fwrite(mb,2,1,fout)) {
fprintf(stderr,MSG_ERR_OUTPUT);
break;
}
}
} else if(fputc(c,fout)==EOF) {
fprintf(stderr,MSG_ERR_OUTPUT);
break;
}
}
|