summaryrefslogtreecommitdiff
path: root/support/dktools/dk4strmo32.ctr
blob: 8a1037aaa58a0ffb08293a4d7570164a399250d4 (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
%%	options

copyright owner	=	Dirk Krause
copyright year	=	2015-xxxx
license		=	bsd


%%	header

/**	@file
	String output over dk4_stream_t API,
	32 bit character strings.

	CRT on Windows: Not used.
*/

#include "dk4strm.h"

#ifdef __cplusplus
extern "C" {
#endif

/**	Set output encoding for 32 bit character output.
	@param	strm	Stream to modify.
	@param	oe	New output encoding.
*/
void
dk4stream_set_output_encoding(dk4_stream_t *strm, int oe);

/**	Write one 32 bit character to stream.
	@param	strm	Stream to write to.
	@param	c32	Character to write.
	@param	erp	Error report, may be NULL.
	@return	1 on success, 0 on error.
	
	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if strm is null or not set up for writing,
	- DK4_E_ENCODING_FAILED<br>
	  if it is not possible to encode c32 to the output encoding for the
	  stream,
	- DK4_E_WRITE_FAILED<br>
	  if writing one ore multiple bytes to the stream failed,
	- DK4_E_FLUSH_FAILED<br>
	  if flushing data downwards failed.
*/
int
dk4stream_c32_putc(dk4_stream_t *strm, dk4_c32_t c32, dk4_er_t *erp);

/**	Write one 32 bit character string to stream.
	@param	strm	Stream to write to.
	@param	txt	Text string to write.
	@param	erp	Error report, may be NULL.
	@return	1 on success, 0 on error.
	
	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if strm is null or not set up for writing,
	- DK4_E_ENCODING_FAILED<br>
	  if it is not possible to encode c32 to the output encoding for the
	  stream,
	- DK4_E_WRITE_FAILED<br>
	  if writing one ore multiple bytes to the stream failed,
	- DK4_E_FLUSH_FAILED<br>
	  if flushing data downwards failed.
*/
int
dk4stream_c32_puts(dk4_stream_t *strm, const dk4_c32_t *txt, dk4_er_t *erp);

/**	Write byte order marker for those output encodings
	supporting a BOM.
	@param	strm	Stream to write the BOM to.
	@param	erp	Error report, may be NULL.
	@return	1 on success (BOM written or not required), 0 on error.
	
	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if strm is null or not set up for writing,
	- DK4_E_ENCODING_FAILED<br>
	  if it is not possible to encode c32 to the output encoding for the
	  stream,
	- DK4_E_WRITE_FAILED<br>
	  if writing one ore multiple bytes to the stream failed,
	- DK4_E_FLUSH_FAILED<br>
	  if flushing data downwards failed.
*/
int
dk4stream_write_bom_if_necessary(dk4_stream_t *strm, dk4_er_t *erp);

#ifdef __cplusplus
}
#endif

%%	module

#include "dk4strmo32.h"
#include "dk4mem.h"
#include "dk4ansi.h"
#include "dk4utf8.h"
#include "dk4utf16.h"
#include "dk4enc.h"



$!trace-include



void
dk4stream_set_output_encoding(dk4_stream_t *strm, int oe)
{
  $? "+ dk4stream_set_output_encoding %d", oe
  if (NULL != strm) {
    switch (oe) {
      case DK4_FILE_ENCODING_ASCII:
      case DK4_FILE_ENCODING_ANSI:
      case DK4_FILE_ENCODING_UTF8:
      case DK4_FILE_ENCODING_UTF16_LE:
      case DK4_FILE_ENCODING_UTF16_BE:
      case DK4_FILE_ENCODING_32_LE:
      case DK4_FILE_ENCODING_32_BE:
      {
        strm->oenc = oe;
      } break;
    }
  } $? "- dk4stream_set_output_encoding"
}



int
dk4stream_c32_putc(dk4_stream_t *strm, dk4_c32_t c32, dk4_er_t *erp)
{
  unsigned char	buf[16];
  dk4_c16_t	b16[4];
  size_t	sz;
  int		back	=	0;
  $? "+ dk4stream_c32_putc %lx", (unsigned long)c32
  if (NULL != strm) {					$? ". stream ok"
    if (0 != ((strm->fl) & DK4_STREAM_WRITE)) {		$? ". wr flag ok"
      switch (strm->oenc) {
        case DK4_FILE_ENCODING_ASCII: {			$? ". ASCII"
	  if (dkC32(0x00000100) > c32) {
	    back = dk4stream_write_byte(strm, (unsigned char)c32, erp);
	  } else {
            dk4error_set_simple_error_code(erp, DK4_E_ENCODING_FAILED);
	  }
	} break;
        case DK4_FILE_ENCODING_ANSI: {			$? ". ANSI"
	  if (0 != dk4ansi_encode(buf, c32)) {
	    back = dk4stream_write_byte(strm, buf[0], erp);
	  } else {
	    dk4error_set_simple_error_code(erp, DK4_E_ENCODING_FAILED);
	  }
	} break;
        case DK4_FILE_ENCODING_UTF8: {			$? ". UTF-8"
	  sz = sizeof(buf);
	  if (0 != dk4utf8_encode(buf, &sz, c32, NULL)) {
	    back = dk4stream_write(strm, (void *)buf, sz, erp);
	  } else {
	    dk4error_set_simple_error_code(erp, DK4_E_ENCODING_FAILED);
	  }
	} break;
        case DK4_FILE_ENCODING_UTF16_LE: {		$? ". UTF-16.LSB"
	  sz = DK4_SIZEOF(b16,dk4_c16_t);
	  if (0 != dk4utf16_encode(b16, &sz, c32, erp)) {	$? ". enc"
	    if ((1 == sz) || (2 == sz)) {
	      buf[0] = (unsigned char)( b16[0]       & 0x00FF);
	      buf[1] = (unsigned char)((b16[0] >> 8) & 0x00FF);
	      if (2 == sz) {				$? ". 2 words"
	        buf[2] = (unsigned char)( b16[1]       & 0x00FF);
		buf[3] = (unsigned char)((b16[1] >> 8) & 0x00FF);
		$? ". write 4 bytes to stream"
		back = dk4stream_write(strm, (void *)buf, 4, erp);
	      } else {					$? ". 1 word"
	        $? ". write 2 bytes to stream"
	        back = dk4stream_write(strm, (void *)buf, 2, erp);
	      }
	    } else {
	      dk4error_set_simple_error_code(erp, DK4_E_ENCODING_FAILED);
	    }
	  } else {						$? "! enc"
	    dk4error_set_simple_error_code(erp, DK4_E_ENCODING_FAILED);
	  }
	} break;
        case DK4_FILE_ENCODING_UTF16_BE: {		$? ". UTF-16.MSB"
	  sz = DK4_SIZEOF(b16,dk4_c16_t);
	  if (0 != dk4utf16_encode(b16, &sz, c32, erp)) {	$? ". enc"
	    if ((1 == sz) || (2 == sz)) {
	      buf[1] = (unsigned char)( b16[0]       & 0x00FF);
	      buf[0] = (unsigned char)((b16[0] >> 8) & 0x00FF);
	      if (2 == sz) {					$? ". 2 bytes"
	        buf[3] = (unsigned char)( b16[1]       & 0x00FF);
		buf[2] = (unsigned char)((b16[1] >> 8) & 0x00FF);
		back = dk4stream_write(strm, (void *)buf, 4, erp);
	      } else {						$? ". 1 byte"
	        back = dk4stream_write(strm, (void *)buf, 2, erp);
	      }
	    } else {						$? "! enc"
	      dk4error_set_simple_error_code(erp, DK4_E_ENCODING_FAILED);
	    }
	  } else {						$? "! enc"
	    dk4error_set_simple_error_code(erp, DK4_E_ENCODING_FAILED);
	  }
	} break;
        case DK4_FILE_ENCODING_32_LE: {		$? ". C32.LSB"
	  buf[0] = (unsigned char)( c32        & dkC32(0x000000FF));
	  buf[1] = (unsigned char)((c32 >>  8) & dkC32(0x000000FF));
	  buf[2] = (unsigned char)((c32 >> 16) & dkC32(0x000000FF));
	  buf[3] = (unsigned char)((c32 >> 24) & dkC32(0x000000FF));
	  back   = dk4stream_write(strm, (void *)buf, 4, erp);
	} break;
        case DK4_FILE_ENCODING_32_BE: {		$? ". C32.MSB"
	  buf[3] = (unsigned char)( c32        & dkC32(0x000000FF));
	  buf[2] = (unsigned char)((c32 >>  8) & dkC32(0x000000FF));
	  buf[1] = (unsigned char)((c32 >> 16) & dkC32(0x000000FF));
	  buf[0] = (unsigned char)((c32 >> 24) & dkC32(0x000000FF));
	  back   = dk4stream_write(strm, (void *)buf, 4, erp);
	} break;
	default: {
	  dk4error_set_simple_error_code(erp, DK4_E_BUG);
	} break;
      }
    } else{
      dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
    }
  } else {
    dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
  } $? "- dk4stream_c32_putc %d", back
  return back;
}



int
dk4stream_c32_puts(dk4_stream_t *strm, const dk4_c32_t *txt, dk4_er_t *erp)
{
  int		 back = 0;
  if ((NULL != strm) && (NULL != txt)) {
    if (0 != ((strm->fl) & DK4_STREAM_WRITE)) {
      back = 1;
      while ((dkC32(0) != *txt) && (1 == back)) {
        if (0 == dk4stream_c32_putc(strm, *(txt++), erp)) {
	  back = 0;
	}
      }
    } else {
      dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
    }
  } else {
    dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
  }
  return back;
}



int
dk4stream_write_bom_if_necessary(dk4_stream_t *strm, dk4_er_t *erp)
{
  int		 back = 0;
  $? "+ dk4stream_write_bom_if_necessary"
  if (NULL != strm) {
    if (0 != ((strm->fl) & DK4_STREAM_WRITE)) {
      switch (strm->oenc) {
#if VERSION_BEFORE_20141218
	/*	For UTF-8 files most text editors do not handle
		the BOM correctly, they show a glyph for unrepresentable
		characters.
		So I decided to write a BOM only for 16 bit and 32 bit
		character output.
	*/
        case DK4_FILE_ENCODING_UTF8:
#endif
        case DK4_FILE_ENCODING_UTF16_LE:
        case DK4_FILE_ENCODING_UTF16_BE:
        case DK4_FILE_ENCODING_32_LE:
        case DK4_FILE_ENCODING_32_BE:
	{ $? ". attempt to write bom"
	  back = dk4stream_c32_putc(strm, dkC32(0x0000FEFF), erp);
	} break;
	default: {
	  back = 1;
	} break;
      }
    } else {
      dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
    }
  } else {
    dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
  } $? "- dk4stream_write_bom_if_necessary %d", back
  return back;
}