/* ------------------------------------------------------ This is a bdf/text koi<->iso converter, quite buggy, but work sometimes. Copyright (c)1997 Eugene Bobin --------------------------------------------------------*/ #include #include #include #include #define MAXSTR 256 static unsigned char table[2][128] = { /* --- ISO -> KOI --- */ { 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xb3, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xe1, 0xe2, 0xf7, 0xe7, 0xe4, 0xe5, 0xf6, 0xfa, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf2, 0xf3, 0xf4, 0xf5, 0xe6, 0xe8, 0xe3, 0xfe, 0xfb, 0xfd, 0xff, 0xf9, 0xf8, 0xfc, 0xe0, 0xf1, 0xc1, 0xc2, 0xd7, 0xc7, 0xc4, 0xc5, 0xd6, 0xda, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd2, 0xd3, 0xd4, 0xd5, 0xc6, 0xc8, 0xc3, 0xde, 0xdb, 0xdd, 0xdf, 0xd9, 0xd8, 0xdc, 0xc0, 0xd1, 0xb0, 0xa3, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf }, /* --- KOI -> ISO --- */ { 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xf1, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xf0, 0xf1, 0xf2, 0xa1, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, 0xee, 0xd0, 0xd1, 0xe6, 0xd4, 0xd5, 0xe4, 0xd3, 0xe5, 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xef, 0xe0, 0xe1, 0xe2, 0xe3, 0xd6, 0xd2, 0xec, 0xeb, 0xd7, 0xe8, 0xed, 0xe9, 0xe7, 0xea, 0xce, 0xb0, 0xb1, 0xc6, 0xb4, 0xb5, 0xc4, 0xb3, 0xc5, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xcf, 0xc0, 0xc1, 0xc2, 0xc3, 0xb6, 0xb2, 0xcc, 0xcb, 0xb7, 0xc8, 0xcd, 0xc9, 0xc7, 0xca } }; /*- 0 --- 1 --- 2 --- 3 --- 4 --- 5 --- 6 --- 7 --- 8 --- 9 --- A --- B --- C --- D --- E --- F -- */ struct bdf_char { unsigned char num; unsigned char data[2048]; }; unsigned char change_code(unsigned char code, int direction ) { if( code < 0x80 ) return code; else return table[direction][code-128]; } static int compare_chars( struct bdf_char *i, struct bdf_char *j ) { if( i->num > j->num ) { return (1); } if( i->num < j->num ) { return (-1); } return (0); } void usage() { printf("\ BDF & Text KOI <-> ISO convertor, Copyright (c) Eugene Bobin, gene@ftim.ustu.ru\n\ Usage: bdfrc [direction]\n\ or: bdfrc bdf [direction]\n\ where: direction either K - koi8r -> iso8859-5 [default]\n\ I - iso8859-5 -> koi8r\n" ); } int main(int argc, char *argv[] ) { FILE *in; FILE *out; char str[MAXSTR+1]; unsigned char code; int direction = 0; /* koi -> iso */ if( argc < 3 || argc > 4 ) { usage(); exit(1); } if( strpbrk( argv[3], "KkIi") == NULL) { printf("Error: Wrong direction\n\n"); usage(); exit(2); } if( argv[3][0] == 'i' || argv[3][0] == 'I' ) direction = 1 ; if( (in = fopen(argv[1], "r")) == NULL) { printf("File open problem: %s\n", argv[1] ); exit(1); } if( (out = fopen(argv[2], "w")) == NULL) { printf("File open problem: %s\n", argv[2] ); exit(1); } if( strstr( argv[1], ".bdf" ) == NULL ) { char c; while( (c = (unsigned char)getc(in)) != EOF && !feof(in) ) putc( change_code( c, direction ), out); } else { struct bdf_char chars[256]; int i = 0; char in_char = 0; while( fgets(str, MAXSTR, in) != NULL ) { if( in_char == 0 && strstr( str, "STARTCHAR" ) != NULL ) { /* strcpy( chars[i].data, str ); */ in_char = 1; } else if( in_char == 1 && strstr( str, "ENCODING" ) != NULL ) { unsigned char c; strtok( str, " "); code = change_code( (unsigned char)atoi( strtok( NULL, " " )), direction ); chars[i].num = code; sprintf( str, "ENCODING %d\n", (int) code ); /* strcat( chars[i].data, str ); */ } else if( in_char == 1 && strstr( str, "ENDCHAR" ) != NULL ) { strcat(chars[i].data, str ); i++; in_char = 0; continue; } else if( in_char == 0 && strstr( str, "ENDFONT" ) != NULL ) { int j = 0; qsort( &chars, i, sizeof( struct bdf_char ), compare_chars ); for( j = 0 ; j <= i; j++) { fwrite( chars[j].data, sizeof( unsigned char), strlen( chars[j].data), out); } fputs( "ENDFONT\n", out ); fclose( in); fclose(out); exit(0); } if( in_char == 1 ) { strcat( chars[i].data, str ); } else { fputs( str, out ); } } } fclose( in); fclose(out); printf("Warning: There's no ENDFONT tag, font might be incomplete.\n"); return 0; }