1 /*
2 Copyright (C) 2015-2021, Dirk Krause
3 SPDX-License-Identifier: BSD-3-Clause
4 */
5 
6 /*
7 	WARNING: This file was generated by the dkct program (see
8 	http://dktools.sourceforge.net/ for details).
9 	Changes you make here will be lost if dkct is run again!
10 	You should modify the original source and run dkct on it.
11 	Original source: dk4strmo08.ctr
12 */
13 
14 /**	@file dk4strmo08.c The dk4strmo08 module.
15 */
16 
17 
18 #include "dk4conf.h"
19 #include <libdk4c/dk4strm.h>
20 #include <libdk4c/dk4strmo08.h>
21 #include <libdk4c/dk4strmo32.h>
22 #include <libdk4c/dk4enc.h>
23 #include <libdk4c/dk4ansi.h>
24 #include <libdk4c/dk4utf8.h>
25 
26 #if DK4_HAVE_ASSERT_H
27 #ifndef	ASSERT_H_INCLUDED
28 #include <assert.h>
29 #define	ASSERT_H_INCLUDED 1
30 #endif
31 #endif
32 
33 
34 
35 
36 
37 
38 int
dk4stream_c08_puts(dk4_stream_t * strm,const char * txt,int ie,dk4_er_t * erp)39 dk4stream_c08_puts(dk4_stream_t *strm, const char *txt, int ie, dk4_er_t *erp)
40 {
41   dk4_utf8_decoder_t	dec;
42   dk4_c32_t	 	c32;
43   int		 	back = 0;
44   char		 	c;
45   unsigned char	 	uc;
46 #if	DK4_USE_ASSERT
47   assert(NULL != strm);
48   assert(NULL != txt);
49 #endif
50   if ((NULL != strm) && (NULL != txt)) {
51     if (0 != ((strm->fl) & DK4_STREAM_WRITE)) {
52       switch (ie) {
53         case DK4_ENCODING_PLAIN:
54 	case DK4_ENCODING_WIN1252:
55 	case DK4_ENCODING_UTF8:
56 	{
57 	  if (strm->oenc == ie) {
58 	    back = 1;
59 	    while (('\0' != *txt) && (1 == back)) {
60 	      if (0 == dk4stream_write_byte(strm, *(txt++), erp)) {
61 	        back = 0;
62 	      }
63 	    }
64 	  } else {
65 	    switch (ie) {
66 	      case DK4_ENCODING_PLAIN: {
67 	        back = 1;
68 	        while (('\0' != *txt) && (1 == back)) {
69 		  c = *(txt++);
70 		  uc = (unsigned char)c;
71 		  c32 = (dk4_c32_t)uc;
72 		  if (0 == dk4stream_c32_putc(strm, c32, erp)) {
73 		    back = 0;
74 		  }
75 		}
76 	      } break;
77 	      case DK4_ENCODING_WIN1252: {
78 	        back = 1;
79 		while (('\0' != *txt) && (1 == back)) {
80 		  if (0 != dk4ansi_decode(&c32, (unsigned char)(*(txt++)))) {
81 		    if (0 == dk4stream_c32_putc(strm, c32, erp)) {
82 		      back = 0;
83 		    }
84 		  } else {
85 		    back = 0;
86 		    dk4error_set_simple_error_code(erp, DK4_E_DECODING_FAILED);
87 		  }
88 		}
89 	      } break;
90 	      case DK4_ENCODING_UTF8: {
91 	        back = 1;
92 		dk4utf8_init(&dec);
93 		while (('\0' != *txt) && (1 == back)) {
94 		  switch (dk4utf8_add(&dec, (unsigned char)(*(txt++)))) {
95 		    case DK4_EDSTM_FINISHED: {
96 		      c32 = dk4utf8_get(&dec);
97 		      dk4utf8_init(&dec);
98 		      if (0 == dk4stream_c32_putc(strm, c32, erp)) {
99 		        back = 0;
100 		      }
101 		    } break;
102 		    case DK4_EDSTM_ERROR: {
103 		      back = 0;
104 		      dk4error_set_simple_error_code(erp,DK4_E_DECODING_FAILED);
105 		    } break;
106 		  }
107 		}
108 		if (0 == dk4utf8_is_empty(&dec)) {
109 		  back = 0;
110 		  dk4error_set_simple_error_code(erp, DK4_E_DECODING_FAILED);
111 		}
112 	      } break;
113 	    }
114 	  }
115 	} break;
116 	default: {
117 	  dk4error_set_simple_error_code(erp, DK4_E_SYNTAX);
118 	} break;
119       }
120     } else {
121       dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
122     }
123   } else {
124     dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
125   }
126   return back;
127 }
128 
129