/* shindou7.c - Don Yang (uguu.org) 02/19/07 */ #include #include #include #ifdef _WIN32 #include #include #endif static FILE *infile; static unsigned char *tmp, *buffer, *const_str = " \0!\0 \xef\xbc\x81I\1\xff\1\xa1\xaa"; static int u, a, state, buffer_size, read_size, i, i0, last_char_offset, c, utf8_kana, sjis_kana, eucjp_kana, encoding; static void Output(void *data, size_t size) { fwrite(data, size, 1, stdout); } static void ExtendBuffer(void) { if( read_size - buffer_size ) { read_size += fread(buffer + read_size, 1, buffer_size - read_size, infile); } else { tmp = buffer; buffer = malloc((buffer_size *= 2) + 3); memcpy(buffer, tmp, read_size); free(tmp); } } 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] & 224) - 192 ) { if( (buffer[i] & 240) - 224 ) { if( (buffer[i] & 248) - 240 ) { NextByte(); } else { if( (buffer[i + 1] & 192) - 128 || (buffer[i + 2] & 192) - 128 || (buffer[i + 2] & 192) - 128 ) { c--; } else { NextByte(); NextByte(); NextByte(); NextByte(); } } } else { if( (buffer[i + 1] & 192) - 128 || (buffer[i + 2] & 192) - 128 ) { c--; } else { NextByte(); NextByte(); NextByte(); } } } else { if( (buffer[i + 1] & 192) - 128 ) { c--; } else { NextByte(); NextByte(); } } } static void NextSJISChar(void) { c = 0; if( (buffer[i] > 128 && buffer[i] < 160) || (buffer[i] > 223 && buffer[i] < 240) ) { if( buffer[i + 1] < 64 || buffer[i + 1] > 252 ) { c--; } else { NextByte(); NextByte(); } } else { NextByte(); } } static void NextEUCJPChar(void) { c = 0; if( (buffer[i] > 160 && buffer[i] < 169) || buffer[i] == 173 || (buffer[i] > 175 && buffer[i] < 245) || (buffer[i] > 248 && buffer[i] < 253) ) { if( buffer[i + 1] < 161 || buffer[i + 1] > 254 ) { c--; } else { NextByte(); NextByte(); } } else { NextByte(); } } static void DetectFileEncoding(void) { read_size = utf8_kana = sjis_kana = eucjp_kana = 0; encoding = 28; for(; !feof(infile) && encoding & (encoding - 1);) { ExtendBuffer(); if( !(encoding & 3) ) { if( buffer[0] - 255 || buffer[1] - 254 ) { if( buffer[0] - 254 || buffer[1] - 255 ) { for(i = 0; i < read_size - 1; i += 2) { if( buffer[i] == 10 && !buffer[i + 1] ) { encoding = 1; goto finalize; } if( !buffer[i] && buffer[i + 1] == 10 ) { encoding = 2; goto finalize; } } if( buffer[0] - 239 || buffer[1] - 187 || buffer[2] - 191 ) { for(i = 0; i < read_size - 3 && encoding & 4;) { NextUTF8Char(); if( c < 0 ) encoding &= ~4; else if( (c > 0xe38180 && c < 0xe381c0) || (c > 0xe3827f && c < 0xe38294) ) utf8_kana++; } for(i = 0; i < read_size - 1 && encoding & 8;) { NextSJISChar(); if( c < 0 ) encoding &= ~8; else if( c > 33438 && c < 33522 ) sjis_kana++; } for(i = 0; i < read_size - 1 && encoding & 16;) { NextEUCJPChar(); if( c < 0 ) encoding &= ~16; else if( c > 42144 && c < 42228 ) eucjp_kana++; } } else { encoding = 4; } } else { encoding = 2; } } else { encoding = 1; } } } finalize: if( !(encoding & 3) ) { if( !(encoding & 4) ) utf8_kana = 0; if( !(encoding & 8) ) sjis_kana = 0; if( !(encoding & 16) ) eucjp_kana = 0; encoding = utf8_kana < sjis_kana ? sjis_kana < eucjp_kana ? 16 : 8 : utf8_kana < eucjp_kana ? 16 : encoding & 4 ? 4 : 8; } } static int IsAlpha(void) { return (c > 96 && c < 123) || (c > 64 && c < 91) || (c > 47 && c < 58) || c == 45 || c == 95 || c == 32 || c == 9; } static int IsExclaimationPunct(void) { return c == 33 || (encoding & 3 && c == 65281) || (encoding & 4 && c == 0xefbc81) || (encoding & 8 && c == 33097) || (encoding & 16 && c == 41386); } static int IsEOSPunct(void) { return c == 46 || c == 63 || (encoding & 3 && (c == 65311 || c == 65294 || c == 12290 || c == 65377)) || (encoding & 4 && (c == 0xefbc9f || c == 0xefbc8e || c == 0xe38082 || c == 0xefbda1)) || (encoding & 8 && (c == 33096 || c == 33092 || c == 33090)) || (encoding & 16 && (c == 41385 || c == 41381 || c == 41379)) || IsExclaimationPunct(); } static int IsPunct(void) { return c == 44 || c == 10 || (encoding & 3 && (c == 65292 || c == 12289 || c == 65380)) || (encoding & 4 && (c == 0xefbc8c || c == 0xe38081 || c == 0xefbda4)) || (encoding & 8 && (c == 33091 || c == 33089)) || (encoding & 16 && (c == 41380 || c == 41378)) || IsEOSPunct(); } static void ToUpperChar(void) { buffer[u] -= buffer[u] > 96 && buffer[u] < 123 ? 32 : 0; u++; } static void ToUpperUCS2LE(void) { for(u = i0; u < i; u++) ToUpperChar(); } static void ToUpperUCS2BE(void) { i0++; ToUpperUCS2LE(); i0--; } static void ToUpperUTF8(void) { for(u = i0; u < i;) { if( (buffer[u] & 248) - 240 ) { if( (buffer[u] & 240) - 224 ) { if( (buffer[u] & 224) - 192 ) ToUpperChar(); else u += 2; } else { u += 3; } } else { u += 4; } } } static void ToUpperJIS(void) { for(u = i0; u < i;) { if( (buffer[u] > 128 && buffer[i] < 160) || (buffer[u] > 223 && buffer[i] < 240) ) u++; else ToUpperChar(); } } static void WriteExclamationMark(void) { Output(const_str + (c > 255 ? encoding & 1 ? 9 : encoding & 2 ? 10 : encoding & 4 ? 5 : encoding & 8 ? 7 : 12 : encoding & 2 ? 1 : 2), (c > 255 ? encoding & 4 ? 3 : 2 : encoding & 3 ? 2 : 1)); } static void Flush(void) { if( i0 - last_char_offset ) Output(buffer + i0, last_char_offset - i0); if( IsExclaimationPunct() ) { WriteExclamationMark(); WriteExclamationMark(); } else if( IsPunct() && c - 10 ) { WriteExclamationMark(); } else { Output(buffer + last_char_offset, i - last_char_offset); } } static void FlushUCS2WithSpace(void) { for(; i0 < last_char_offset; i0 += 2) { Output(buffer + i0, 2); Output(const_str + (encoding & 1 ? 0 : 3), 2); } } static void FlushUTF8WithSpace(void) { for(; i0 < last_char_offset; Output(const_str, 1)) { u = (buffer[i0] & 248) - 240 ? (buffer[i0] & 240) - 224 ? (buffer[i0] & 224) - 192 ? 1 : 2 : 3 : 4; Output(buffer + i0, u); i0 += u; } } static void FlushJISWithSpace(void) { for(; i0 < last_char_offset; i0 += u) { u = buffer[i0] > 127 ? 2 : 1; Output(buffer + i0, u); Output(const_str, 1); } } static void Filter(void (*NextChar)(void), void (*ToUpper)(void), void (*FlushWithSpace)(void)) { i = i0 = state = 0; for(; !feof(infile) || i < read_size;) { last_char_offset = i; NextChar(); 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 - 1 ) { if( state - 2 ) { if( c > 127 ) { state = 2; } else if( IsAlpha() ) { state = 1; } else { Output(buffer + last_char_offset, i - last_char_offset); } i0 = last_char_offset; } else { if( IsAlpha() || IsPunct() ) { if( IsEOSPunct() ) FlushWithSpace(); Flush(); state = 0; } } } else { if( !IsAlpha() || IsPunct() ) { if( IsEOSPunct() ) ToUpper(); Flush(); state = 0; } } } } int main(int argc, char **argv) { #ifdef _WIN32 setmode(fileno(stdin), O_BINARY); setmode(fileno(stdout), O_BINARY); #endif buffer_size = 4096; buffer = malloc(buffer_size); if( argc > 1 ) { for(a = 1; a < argc; a++) { if( (infile = fopen(argv[a], "rb")) ) { DetectFileEncoding(); printf("%s: %s\n", argv[a], encoding - 1 ? encoding - 2 ? encoding - 4 ? encoding - 8 ? "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(); if( encoding & 3 ) { if( encoding & 1 ) Filter(NextUCS2LEChar, ToUpperUCS2LE, FlushUCS2WithSpace); else Filter(NextUCS2BEChar, ToUpperUCS2BE, FlushUCS2WithSpace); } else if( encoding & 4 ) { Filter(NextUTF8Char, ToUpperUTF8, FlushUTF8WithSpace); } else { if( encoding & 8 ) Filter(NextSJISChar, ToUpperJIS, FlushJISWithSpace); else Filter(NextEUCJPChar, ToUpperJIS, FlushJISWithSpace); } } free(buffer); return 0; }