/* shindou5_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 = 20 00 1 = 00 21 2 = 21 00 3 = 00 20 5 = 01 ff 6 = ff 01 8 = ef bc 81 10 = 81 49 12 = a1 aa */ " \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; encoding = UTF8 | SJIS | EUCJP; for(; !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] == 0x00 ) { encoding = UCS2LE; goto finalize; } if( buffer[i] == 0x00 && buffer[i + 1] == 0x0a ) { encoding = UCS2BE; goto finalize; } } if( buffer[0] == 0xef && buffer[1] == 0xbb && buffer[2] == 0xbf ) { encoding = UTF8; } else { if( encoding & UTF8 ) { for(i = 0; i < read_size - 3;) { NextUTF8Char(); if( c == -1 ) { encoding &= ~UTF8; break; } if( (c >= 0xe38181 && c <= 0xe381bf) || (c >= 0xe38280 && c <= 0xe38293) ) { utf8_kana++; } } } if( encoding & SJIS ) { for(i = 0; i < read_size - 1;) { NextSJISChar(); if( c == -1 ) { encoding &= ~SJIS; break; } if( c >= 0x829f && c <= 0x82f1 ) sjis_kana++; } } if( encoding & EUCJP ) { for(i = 0; i < read_size - 2;) { NextEUCJPChar(); if( c == -1 ) { encoding &= ~EUCJP; break; } 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; if( utf8_kana < sjis_kana ) { encoding = (sjis_kana < eucjp_kana) ? EUCJP : SJIS; } else { if( utf8_kana < eucjp_kana ) encoding = EUCJP; else encoding = 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) { if( c > 255 ) { if( encoding & UCS2BE ) { Output(const_str + 6, 2); /* ff 01 */ } else if( encoding & UCS2LE ) { Output(const_str + 5, 2); /* 01 ff */ } else if( encoding & UTF8 ) { Output(const_str + 8, 3); /* ef bc 81 */ } else if( encoding & SJIS ) { Output(const_str + 10, 2); /* 81 49 */ } else { Output(const_str + 12, 2); /* a1 aa */ } } else { if( encoding & UCS2BE ) { Output(const_str + 1, 2); /* 00 21 */ } else if( encoding & UCS2LE ) { Output(const_str + 2, 2); /* 21 00 */ } else { Output(const_str + 2, 1); /* 21 */ } } } static void WriteSpace(void) { if( encoding & (UCS2LE|UCS2BE) ) { if( encoding & UCS2LE ) { Output(const_str, 2); /* 20 00 */ } else { Output(const_str + 3, 2); /* 00 20 */ } } else { Output(const_str, 1); /* 20 */ } } static void Flush(void) { 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); } } static void FlushWithSpace(void (*NextChar)(void)) { for(i = i0; i < last_char_offset; i0 = i) { NextChar(); Output(buffer + i0, i - i0); WriteSpace(); } NextChar(); } static void Filter(void (*NextChar)(void)) { i = i0 = state = 0; for(; !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() ) FlushWithSpace(NextChar); Flush(); 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; }