#include "dk4conf.h" #include "dk4enc.h" #include "dk4tsp08.h" #include "dk4strm.h" #include "dk4strmf.h" #include "dk4strmo32.h" #include "dk4ansi.h" #include $!trace-include #define LINE_VERSION 1 /** Buffer for input from file. */ static char fbuf[4096]; #if LINE_VERSION static char lbuf[4096]; #endif #if LINE_VERSION int line_handler(void *obj, char *line, dk4_um_t lineno, dk4_er_t *erp) { size_t sz; int back = DK4_TSP_RES_ERROR; sz = strlen(line); if (0 < sz) { if (0 != dk4stream_write( (dk4_stream_t *)obj, line, sz, erp)) { back = DK4_TSP_RES_OK; } } else { back = DK4_TSP_RES_OK; } return back; } #else int char_handler(void *obj, char c, dk4_text_stream_position_t *pos, dk4_er_t *erp) { dk4_c32_t c32; int back = DK4_TSP_RES_ERROR; if (dk4ansi_decode(&c32, (unsigned char)c)) { if (dk4stream_c32_putc((dk4_stream_t *)obj, c32, erp)) { back = DK4_TSP_RES_OK; } } return back; } #endif int main(void) { dk4_tsp08_t tsp; /* Text stream processor. */ dk4_er_t er; /* Error report */ dk4_stream_t *outstream; /* Output stream */ size_t nread; /* Number of bytes read */ #if DK4_ON_WINDOWS int oldmode; /* Output text or binary mode */ int oldinmode; /* Input text or binary mode */ #endif int res; /* Result for text stream processing */ int oldres; /* Previous result */ int cc; /* Flag: Can continue */ $!trace-init test-dk4tsp08.deb $? "+ main" #if DK4_ON_WINDOWS oldmode = _setmode(_fileno(stdout), _O_BINARY); oldinmode = _setmode(_fileno(stdin), _O_BINARY); #endif dk4error_init(&er); outstream = dk4stream_open_for_file( stdout, DK4_STREAM_WRITE, 0, 0, &er); if (NULL != outstream) { dk4stream_set_output_encoding(outstream, DK4_FILE_ENCODING_ASCII); #if 1 (void)dk4stream_write_bom_if_necessary(outstream, NULL); #endif #if LINE_VERSION res = dk4tsp08_setup_line( &tsp, (void *)outstream, line_handler, lbuf, sizeof(lbuf), DK4_ENCODING_ASCII, DK4_ENCODING_UTF8, &er ); #else res = dk4tsp08_setup_char( &tsp, (void *)outstream, char_handler, DK4_ENCODING_UTF8, DK4_ENCODING_UTF8, &er ); #endif if (DK4_TSP_RES_OK == res) { oldres = DK4_TSP_RES_OK; cc = 1; while (1 == cc) { nread = fread(fbuf, 1, sizeof(fbuf), stdin); if (0 < nread) { res = dk4tsp08_add_bytes(&tsp, (unsigned char *)fbuf, nread); switch (res) { case DK4_TSP_RES_OK: { } break; case DK4_TSP_RES_ERROR: { if (DK4_TSP_RES_OK == oldres) { oldres = DK4_TSP_RES_ERROR; $? "! STATE CHANGE: ERROR" } } break; case DK4_TSP_RES_FATAL: { if (DK4_TSP_RES_FATAL != oldres) { oldres = DK4_TSP_RES_FATAL; $? "! STATE CHANGE: FATAL" } } break; } } else { cc = 0; } } res = dk4tsp08_finish(&tsp); switch (res) { case DK4_TSP_RES_OK: { } break; case DK4_TSP_RES_ERROR: { if (DK4_TSP_RES_OK == oldres) { oldres = DK4_TSP_RES_ERROR; $? "! STATE CHANGE: ERROR" } } break; case DK4_TSP_RES_FATAL: { if (DK4_TSP_RES_FATAL != oldres) { oldres = DK4_TSP_RES_FATAL; $? "! STATE CHANGE: FATAL" } } break; } } else { $? "! failed to initialize text processing" } dk4stream_close(outstream, NULL); } else { $? "! failed to create output stream" } fflush(stdout); #if DK4_ON_WINDOWS _setmode(_fileno(stdin), oldinmode); _setmode(_fileno(stdout), oldmode); #endif $? "- main" $!trace-end return 0; }