/*************************************************************************** jistosj - A simple JIS to SJIS character code translator Written by Kazuyuki HANAAHARA (hanahara@cs.kobe-u.ac.jp) ***************************************************************************/ #include #include #include /* * ShiftIn, ShiftOut. * (These macros are not referred in the following) */ #define SI1 "\033$B" #define SI2 "\033$@" #define SO1 "\033(B" #define SO2 "\033(J" /* * JIS 2-bytes code to SJIS 2-bytes code. * Result is overwritten. */ static unsigned char *jtoscode( kanji ) unsigned char *kanji; { if( (kanji[0] & 0x01) == 0 ) kanji[1] += 0x7d; else kanji[1] += 0x1f; if( kanji[1] >= 0x7f ) kanji[1]++; kanji[0] = (unsigned char)((((short int)kanji[0] - 0x21) >> 1) + 0x81); if( kanji[0] > 0x9f ) kanji[0] += 0x40; return( kanji ); } /* * Convert JIS to SJIS stream */ static FILE *jtosfile( fpj, fps ) FILE *fpj, *fps; { int c, kcode, p, i; unsigned char buf[3]; for( kcode = p = 0; ; ){ /* * Fill 3-chars buffer. */ while( p < 3 && (c = fgetc( fpj )) != EOF ) buf[p++] = c; if( c == EOF ) break; /* EOF detected. */ /* * Check Shift-In/Shift-Out escape sequence. */ if( buf[0] == '\033' ){ if(buf[1] == '$' && (buf[2] == 'B' || buf[2] == '@')){ /*--- Shift-In ---*/ kcode = 1; p = 0; /* Buffer empty. */ continue; } if(buf[1] == '(' && (buf[2] == 'B' || buf[2] == 'J')){ /*--- Shift-Out ---*/ kcode = 0; p = 0; /* Buffer empty. */ continue; } } /* * Write to output stream. */ if( kcode == 0 ){ /*--- Write one character. ---*/ fputc( buf[0], fps ); /*--- Buffer-shift. ---*/ buf[0] = buf[1]; buf[1] = buf[2]; p--; /* Space for next input character. */ } else{ /*--- Write one kanji(= two bytes). ---*/ jtoscode( buf ); /* JIS to SJIS */ fputc( buf[0], fps ); /* First byte */ fputc( buf[1], fps ); /* Second byte */ /*--- Two characters have been written. ---*/ buf[0] = buf[2]; p -= 2; /* Space for next two input characters. */ } } /* * Write the rest in the buffer. */ if( kcode == 0 ){ /*--- Just write. ---*/ for( i = 0; i < p; i++ ) fputc( buf[i], fps ); } else{ /*--- Write as SJIS. ---*/ jtoscode( buf ); fputc( buf[0], fps ); fputc( buf[1], fps ); } return( fps ); } main( argc, argv ) int argc; char *argv[]; { FILE *fpj, *fps; if( argc > 3 ){ fprintf( stderr, "jistosj [jisfile [sjisfile] ]\n" ); exit( 0 ); } /* * Default input/output stream. */ fpj = stdin; fps = stdout; /* * Determine input/output stream. */ if( argc >= 2 ){ if( (fpj = fopen( argv[1], "r" )) == NULL ){ fprintf( stderr, "Fail to open '%s'.\n", argv[1] ); exit( 0 ); } if( argc == 3 ){ if( (fps = fopen( argv[2], "w" )) == NULL ){ fprintf( stderr, "Fail to open '%s'.\n", argv[2] ); exit( 0 ); } } } jtosfile( fpj, fps ); if( fps != stdout ) fclose( fps ); if( fpj != stdin ) fclose( fpj ); }