#include #include #define MAX_LEN 2048 static unsigned char *colon = ":"; static unsigned char *space = " "; static unsigned char *title_prefix = "
 "; static unsigned char *title_suffix = "
\n\n"; static unsigned char *conversation_prefix = ""; static unsigned char *conversation_middle = ":"; static unsigned char *conversation_suffix = "
\n\n"; static unsigned char *togaki_prefix = "
 "; static unsigned char *togaki_suffix = "

\n\n"; static unsigned char *carriage_return = "
\n\n"; void write_line( unsigned char *s, FILE *fp ) { int i; for ( i = 0; i < strlen( s ) - 2; i++ ) { fputc( s[i], fp ); } } int main( int argc, char **argv ) { unsigned char buf[MAX_LEN], *ptr, *colon_ptr, *middle; unsigned char *suffix; char conversation, successive = 0; int i; FILE *infp, *outfp; suffix = ""; if (( infp = fopen( argv[1], "r" )) == NULL ) { fprintf( stderr, "No input file %s\n\n", argv[1] ); exit( 0 ); } if (( outfp = fopen( argv[2], "w" )) == NULL ) { fprintf( stderr, "Cannot create file %s\n\n", argv[2] ); exit( 0 ); } while ( !feof( infp ) ) { fgets( buf, MAX_LEN, infp ); ptr = buf; /* スペースの削除 */ while (( ptr[0] == space[0] ) && ( ptr[1] == space[1] )) { ptr += 2; } /* printf( "\n%d:%s", buf[0], buf ); continue; */ /* タイトルの認識 */ if ( ptr[0] == '-' ) { if ( successive ) { fputs( suffix, outfp ); } fputs( title_prefix, outfp ); write_line( ptr, outfp ); fputs( title_suffix, outfp ); suffix = ""; successive = 0; continue; } /* 改行だけの行を認識 */ if ( ptr[0] == 13 ) { if ( successive ) { fputs( suffix, outfp ); suffix = ""; successive = 0; } fputs( carriage_return, outfp ); continue; } /* :があるかどうか探す */ conversation = 0; for ( i = 0; i < strlen( ptr ) - 2; i++ ) { if (( ptr[i] == colon[0] ) && ( ptr[i+1] == colon[1] )) { conversation = 1; ptr[i] = ptr[i+1] = '\0'; middle = &(ptr[i]) + 2; break; } } if ( conversation ) { /* 会話行 */ if ( successive ) { fputs( suffix, outfp ); } fputs( conversation_prefix, outfp ); fputs( ptr, outfp ); fputs( conversation_middle, outfp ); write_line( middle, outfp ); suffix = conversation_suffix; successive = 1; } else { /* ト書き行、ないし会話行・ト書き行の続き */ if ( successive ) { fputs( ptr, outfp ); } else { /* ここからト書きが始まる */ fputs( togaki_prefix, outfp ); write_line( ptr, outfp ); suffix = togaki_suffix; successive = 1; } } } if ( successive ) { fputs( suffix, outfp ); } close( infp ); close( outfp ); return 0; }