/* shindou9_no_ucase.c - Don Yang (uguu.org) 02/24/07 */ #include #include #include static FILE *infile; static int state, buffer_size, read_size, i, i0, last_char_offset, c, utf8_kana, sjis_kana, eucjp_kana, encoding, byte_offset; static unsigned char *tmp, *buffer, *const_str = " \0!\0 \1\xff\1\xef\xbc\x81I\xa1\xaa" " 'PoPoPo\4""0?PoPo\3\20\17Po\2\1" "QO\220,\2""0?\220,\2\1" "qxq.\2}}q.\2`$q.\2,q.\2^^q.\2__q.q.\3\1", *parser, *p; static void Output(void *buffer, int size) { fwrite(buffer, size, 1, stdout); } static void ExtendBuffer(void) { if( read_size == buffer_size ) { tmp = buffer; buffer = malloc((buffer_size *= 2) + 3); memcpy(buffer, tmp, read_size); free(tmp); } read_size += fread(buffer + read_size, 1, buffer_size - read_size, infile); } static void NextChar(void) { c = byte_offset = 0; for(p = parser; *p > 4 && c != -1; p++) { if( buffer[i + byte_offset] < (*p ^ 208) || buffer[i + byte_offset] > (p[1] ^ 208) ) { if( byte_offset ) { c = -1; } else { for(; *p > 4; p++); } } else { byte_offset++; p++; } } for(byte_offset = 0; c != -1 && byte_offset < *p; byte_offset++) c = (c << 8) | buffer[i++]; if( encoding & 8 ) c = (c >> 8) | ((c & 255) << 8); } static void DetectFileEncoding(void) { read_size = utf8_kana = sjis_kana = eucjp_kana = 0; for(encoding = 7; !feof(infile) && (encoding & (encoding - 1));) { ExtendBuffer(); if( !(encoding & 24) ) { if( *buffer == 0xff && buffer[1] == 0xfe ) { encoding = 8; } else if( *buffer == 0xfe && buffer[1] == 0xff ) { encoding = 16; } else { for(i = 0; i < read_size - 1; i += 2) { if( buffer[i] == 0x0a && !buffer[i + 1] ) { encoding = 8; goto finalize; } if( !buffer[i] && buffer[i + 1] == 0x0a ) { encoding = 16; goto finalize; } } if( *buffer == 0xef && buffer[1] == 0xbb && buffer[2] == 0xbf ) { encoding = 1; } else { parser = const_str + 14; for(i = 0; i < read_size - 3 && encoding & 1;) { NextChar(); if( c < 0 ) { encoding &= ~1; } else if( (c >= 0xe38181 && c <= 0xe381bf) || (c >= 0xe38280 && c <= 0xe38293) ) { utf8_kana++; } } parser += 22; for(i = 0; i < read_size - 1 && encoding & 2;) { NextChar(); if( c < 0 ) { encoding &= ~2; } else if( c >= 0x829f && c <= 0x82f1 ) { sjis_kana++; } } parser += 11; for(i = 0; i < read_size - 2 && encoding & 4;) { NextChar(); if( c < 0 ) { encoding &= ~4; } else if( c >= 0xa4a1 && c <= 0xa4f3 ) { eucjp_kana++; } } } } } } finalize: if( !(encoding & 24) ) { if( !(encoding & 1) ) utf8_kana = -1; if( !(encoding & 2) ) sjis_kana = -1; if( !(encoding & 4) ) eucjp_kana = -1; encoding = utf8_kana < sjis_kana ? sjis_kana < eucjp_kana ? 4 : 2 : utf8_kana < eucjp_kana ? 4 : encoding & 1 ? 1 : 4; } } static int IsExclaimationPunct(void) { return c == 0x21 || (encoding & 24 && c == 0xff01) || (encoding & 1 && c == 0xefbc81) || (encoding & 2 && c == 0x8149) || (encoding & 4 && c == 0xa1aa); } static int IsEOSPunct(void) { return c == 0x2e || c == 0x3f || (encoding & 24 && (c == 0xff1f || c == 0xff0e || c == 0x3002 || c == 0xff61)) || (encoding & 1 && (c == 0xefbc9f || c == 0xefbc8e || c == 0xe38082 || c == 0xefbda1)) || (encoding & 2 && (c == 0x8148 || c == 0x8144 || c == 0x8142)) || (encoding & 4 && (c == 0xa1a9 || c == 0xa1a5 || c == 0xa1a3)) || IsExclaimationPunct(); } static int IsPunct(void) { return c == 0x2c || c == 0x0a || (encoding & 24 && (c == 0xff0c || c == 0x3001 || c == 0xff64)) || (encoding & 1 && (c == 0xefbc8c || c == 0xe38081 || c == 0xefbda4)) || (encoding & 2 && (c == 0x8143 || c == 0x8141)) || (encoding & 4 && (c == 0xa1a4 || c == 0xa1a2)) || IsEOSPunct(); } static void WriteExclamationMark(void) { Output(const_str + (c > 255 ? encoding & 16 ? 6 : encoding & 8 ? 5 : encoding & 1 ? 8 : encoding & 2 ? 10 : 12 : encoding & 16 ? 1 : 2), c > 255 ? encoding & 1 ? 3 : 2 : encoding & 24 ? 2 : 1); } int main(int argc, char **argv) { buffer_size = 0x1000; buffer = malloc(buffer_size); if( argc > 1 ) { for(++argv; *argv; ++argv) { Output(*argv, strlen(*argv)); Output(": ", 2); if( (infile = fopen(*argv, "rb")) ) { DetectFileEncoding(); puts(encoding != 8 ? encoding != 16 ? encoding != 1 ? encoding != 2 ? "EUC-JP" : "Shift_JIS" : "UTF-8" : "UCS-2BE" : "UCS-2LE"); fclose(infile); } else { puts("can not open"); } } } else { infile = stdin; DetectFileEncoding(); parser = const_str + ( encoding != 1 ? encoding != 2 ? encoding != 4 ? 34 : 47 : 36 : 14 ); for(i = i0 = state = 0; !feof(infile) || i < read_size;) { last_char_offset = i; NextChar(); if( c < 0 ) i++; if( i < read_size - 3 ) { memmove(buffer, buffer + i0, read_size - i0); read_size -= i0; i -= i0; last_char_offset -= i0; i0 = 0; ExtendBuffer(); } if( state ) { if( IsPunct() ) { if( IsEOSPunct() ) { for(i = i0; i < last_char_offset; i0 = i) { NextChar(); Output(buffer + i0, i - i0); Output(const_str + (encoding & 16 ? 3 : 0), encoding & 24 ? 2 : 1); } NextChar(); } if( i0 < last_char_offset ) Output(buffer + i0, last_char_offset - i0); if( IsExclaimationPunct() ) { WriteExclamationMark(); WriteExclamationMark(); } else if( IsPunct() && c != 0x0a ) { WriteExclamationMark(); } else { Output(buffer + last_char_offset, i - last_char_offset); } state = 0; } } else if( c < 0 || IsPunct() ) { Output(buffer + last_char_offset, i - last_char_offset); } else { state = 1; i0 = last_char_offset; } } } free(buffer); return 0; }