/* shindou6_no_ucase.c - Don Yang (uguu.org) 02/23/07 */ #include #include #include #include #ifdef _WIN32 #include #include #endif typedef enum { UTF8 = 1, SJIS = 2, EUCJP = 4, UCS2LE = 8, UCS2BE = 16 } Encoding; static FILE *infile; static unsigned char *tmp, *buffer; static char *const_str = " \0!\0 \1\xff\1\xef\xbc\x81I\xa1\xaa"; static int a, state, buffer_size, read_size, i, i0, last_char_offset, c, utf8_kana, sjis_kana, eucjp_kana, encoding; 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); assert(buffer != NULL); memcpy(buffer, tmp, read_size); free(tmp); } read_size += fread(buffer + read_size, 1, buffer_size - read_size, infile); } static void NextByte(void) { c = (c << 8) | buffer[i++]; } static void NextUCS2BEChar(void) { c = 0; NextByte(); NextByte(); } static void NextUCS2LEChar(void) { c = buffer[i] | (buffer[i + 1] << 8); i += 2; } static void NextUTF8Char(void) { c = 0; if( (buffer[i] & 0xe0) != 0xc0 ) { if( (buffer[i] & 0xf0) != 0xe0 ) { if( (buffer[i] & 0xf8) != 0xf0 ) { NextByte(); } else if( (buffer[i + 1] & 0xc0) != 0x80 || (buffer[i + 2] & 0xc0) != 0x80 || (buffer[i + 2] & 0xc0) != 0x80 ) { c--; } else { NextByte(); NextByte(); NextByte(); NextByte(); } } else if( (buffer[i + 1] & 0xc0) != 0x80 || (buffer[i + 2] & 0xc0) != 0x80 ) { c--; } else { NextByte(); NextByte(); NextByte(); } } else if( (buffer[i + 1] & 0xc0) != 0x80 ) { c--; } else { NextByte(); NextByte(); } } static void NextSJISChar(void) { c = 0; if( (buffer[i] >= 0x81 && buffer[i] <= 0x9f) || (buffer[i] >= 0xe0 && buffer[i] <= 0xef) ) { if( buffer[i + 1] < 0x40 || buffer[i + 1] > 0xfc ) { c--; } else { NextByte(); NextByte(); } } else { NextByte(); } } static void NextEUCJPChar(void) { c = 0; if( (buffer[i] >= 0xa1 && buffer[i] <= 0xa8) || buffer[i] == 0xad || (buffer[i] >= 0xb0 && buffer[i] <= 0xf4) || (buffer[i] >= 0xf9 && buffer[i] <= 0xfc) || buffer[i] == 0x8e ) { if( buffer[i + 1] < 0xa1 || buffer[i + 1] > 0xfe ) { c--; } else { NextByte(); NextByte(); } } else if( buffer[i] == 0x8f ) { if( buffer[i + 1] < 0xa1 || buffer[i + 1] > 0xfe || buffer[i + 2] < 0xa1 || buffer[i + 2] > 0xfe ) { c--; } else { NextByte(); NextByte(); NextByte(); } } else { NextByte(); } } static void DetectFileEncoding(void) { read_size = utf8_kana = sjis_kana = eucjp_kana = 0; for(encoding = UTF8 | SJIS | EUCJP; !feof(infile) && (encoding & (encoding - 1));) { ExtendBuffer(); if( !(encoding & (UCS2LE|UCS2BE)) ) { if( buffer[0] == 0xff && buffer[1] == 0xfe ) { encoding = UCS2LE; } else if( buffer[0] == 0xfe && buffer[1] == 0xff ) { encoding = UCS2BE; } else { for(i = 0; i < read_size - 1; i += 2) { if( buffer[i] == 0x0a && !buffer[i + 1] ) { encoding = UCS2LE; goto finalize; } if( !buffer[i] && buffer[i + 1] == 0x0a ) { encoding = UCS2BE; goto finalize; } } if( buffer[0] == 0xef && buffer[1] == 0xbb && buffer[2] == 0xbf ) { encoding = UTF8; } else { for(i = 0; i < read_size - 3 && encoding & UTF8;) { NextUTF8Char(); if( c < 0 ) { encoding &= ~UTF8; } else if( (c >= 0xe38181 && c <= 0xe381bf) || (c >= 0xe38280 && c <= 0xe38293) ) { utf8_kana++; } } for(i = 0; i < read_size - 1 && encoding & SJIS;) { NextSJISChar(); if( c < 0 ) { encoding &= ~SJIS; } else if( c >= 0x829f && c <= 0x82f1 ) { sjis_kana++; } } for(i = 0; i < read_size - 2 && encoding & EUCJP;) { NextEUCJPChar(); if( c < 0 ) { encoding &= ~EUCJP; } else if( c >= 0xa4a1 && c <= 0xa4f3 ) { eucjp_kana++; } } } } } } finalize: if( !(encoding & (UCS2LE|UCS2BE)) ) { if( !(encoding & UTF8) ) utf8_kana = -1; if( !(encoding & SJIS) ) sjis_kana = -1; if( !(encoding & EUCJP) ) eucjp_kana = -1; encoding = (utf8_kana < sjis_kana) ? (sjis_kana < eucjp_kana) ? EUCJP : SJIS : (utf8_kana < eucjp_kana) ? EUCJP : encoding & UTF8 ? UTF8 : EUCJP; } } static int IsExclaimationPunct(void) { return c == 0x21 || (encoding & (UCS2LE|UCS2BE) && c == 0xff01) || (encoding & UTF8 && c == 0xefbc81) || (encoding & SJIS && c == 0x8149) || (encoding & EUCJP && c == 0xa1aa); } static int IsEOSPunct(void) { return c == 0x2e || c == 0x3f || (encoding & (UCS2LE|UCS2BE) && (c == 0xff1f || c == 0xff0e || c == 0x3002 || c == 0xff61)) || (encoding & UTF8 && (c == 0xefbc9f || c == 0xefbc8e || c == 0xe38082 || c == 0xefbda1)) || (encoding & SJIS && (c == 0x8148 || c == 0x8144 || c == 0x8142)) || (encoding & EUCJP && (c == 0xa1a9 || c == 0xa1a5 || c == 0xa1a3)) || IsExclaimationPunct(); } static int IsPunct(void) { return c == 0x2c || c == 0x0a || (encoding & (UCS2LE|UCS2BE) && (c == 0xff0c || c == 0x3001 || c == 0xff64)) || (encoding & UTF8 && (c == 0xefbc8c || c == 0xe38081 || c == 0xefbda4)) || (encoding & SJIS && (c == 0x8143 || c == 0x8141)) || (encoding & EUCJP && (c == 0xa1a4 || c == 0xa1a2)) || IsEOSPunct(); } static void WriteExclamationMark(void) { Output(const_str + (c > 255 ? encoding & UCS2BE ? 6 : encoding & UCS2LE ? 5 : encoding & UTF8 ? 8 : encoding & SJIS ? 10 : 12 : encoding & UCS2BE ? 1 : 2), c > 255 ? encoding & UTF8 ? 3 : 2 : encoding & (UCS2LE|UCS2BE) ? 2 : 1); } static void Filter(void (*NextChar)(void)) { 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 & UCS2BE ? 3 : 0), encoding & (UCS2LE|UCS2BE) ? 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; } } } int main(int argc, char **argv) { #ifdef _WIN32 setmode(fileno(stdin), O_BINARY); setmode(fileno(stdout), O_BINARY); #endif buffer_size = 0x1000; buffer = malloc(buffer_size); assert(buffer != NULL); if( argc > 1 ) { for(a = 1; a < argc; a++) { if( (infile = fopen(argv[a], "rb")) ) { DetectFileEncoding(); printf("%s: %s\n", argv[a], encoding != UCS2LE ? encoding != UCS2BE ? encoding != UTF8 ? encoding != SJIS ? "EUC-JP" : "Shift_JIS" : "UTF-8" : "UCS-2BE" : "UCS-2LE"); fclose(infile); } else { printf("%s: can not open file\n", argv[a]); } } } else { infile = stdin; DetectFileEncoding(); switch( encoding ) { case UCS2BE: Filter(NextUCS2BEChar); break; case UCS2LE: Filter(NextUCS2LEChar); break; case UTF8: Filter(NextUTF8Char); break; case SJIS: Filter(NextSJISChar); break; case EUCJP: Filter(NextEUCJPChar); break; default: assert(0); } } free(buffer); return 0; }