summaryrefslogtreecommitdiff
path: root/support/dktools/test-dk4tsp08.ctr
blob: 91a7233c36ae59aa87c3baa0f28250881336c337 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157

#include "dk4conf.h"
#include "dk4enc.h"
#include "dk4tsp08.h"
#include "dk4strm.h"
#include "dk4strmf.h"
#include "dk4strmo32.h"
#include "dk4ansi.h"
#include <string.h>


$!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;
}