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: dk4fput.ctr
12 */
13 
14 /**	@file dk4fput.c The dk4fput module.
15 */
16 
17 
18 #include "dk4conf.h"
19 #include <libdk4c/dk4fput.h>
20 
21 #if DK4_HAVE_IO_H
22 #ifndef IO_H_INCLUDED
23 #include <io.h>
24 #define IO_H_INCLUDED 1
25 #endif
26 #endif
27 
28 #if DK4_HAVE_FCNTL_H
29 #ifndef FCNTL_H_INCLUDED
30 #include <fcntl.h>
31 #define	FCNTL_H_INCLUDED 1
32 #endif
33 #endif
34 
35 #ifndef	DK4REC26_H_INCLUDED
36 #include <libdk4c/dk4rec26.h>
37 #endif
38 
39 #include <libdk4base/dk4unused.h>
40 
41 #if DK4_HAVE_ASSERT_H
42 #ifndef	ASSERT_H_INCLUDED
43 #include <assert.h>
44 #define	ASSERT_H_INCLUDED 1
45 #endif
46 #endif
47 
48 
49 
50 
51 
52 
53 #if _WIN32 && (DK4_CHAR_SIZE > 1) && DK4_HAVE__SETMODE && defined(_MSC_VER)
54 
55 /**	Flag: Standard output initialized to write dkChar.
56 */
57 static int dk4fput_stdout_initialized = 0;
58 
59 /**	Standard output mode at startup (should be restored before exiting).
60 */
61 static int dk4fput_stdout_oldmode = _O_TEXT;
62 
63 /**	Flag: Standard error output initialized to write dkChar.
64 */
65 static int dk4fput_stderr_initialized = 0;
66 
67 /**	Standard error mode at startup (should be restored before exiting).
68 */
69 static int dk4fput_stderr_oldmode = _O_TEXT;
70 
71 #endif
72 
73 
74 
75 void
dk4fput_initialize_stdout(void)76 dk4fput_initialize_stdout(void)
77 {
78 #if _WIN32 && (DK4_CHAR_SIZE > 1) && DK4_HAVE__SETMODE && defined(_MSC_VER)
79   if (0 == dk4fput_stdout_initialized) {
80     dk4fput_stdout_oldmode = _setmode(_fileno(stdout), _O_U16TEXT);
81     if (0 == _isatty(_fileno(stdout))) {
82       fputwc((wchar_t)0xFEFF, stdout);
83     }
84     dk4fput_stdout_initialized = 1;
85   }
86 #endif
87 }
88 
89 
90 
91 void
dk4fput_initialize_stdout_no_bom(void)92 dk4fput_initialize_stdout_no_bom(void)
93 {
94 #if _WIN32 && (DK4_CHAR_SIZE > 1) && DK4_HAVE__SETMODE && defined(_MSC_VER)
95   if (0 == dk4fput_stdout_initialized) {
96     dk4fput_stdout_oldmode = _setmode(_fileno(stdout), _O_U16TEXT);
97     dk4fput_stdout_initialized = 1;
98   }
99 #endif
100 }
101 
102 
103 void
dk4fput_cleanup_stdout(void)104 dk4fput_cleanup_stdout(void)
105 {
106 #if _WIN32 && (DK4_CHAR_SIZE > 1) && DK4_HAVE__SETMODE && defined(_MSC_VER)
107   if (0 != dk4fput_stdout_initialized) {
108     fflush(stdout);
109     (void)_setmode(_fileno(stdout), dk4fput_stdout_oldmode);
110   }
111 #endif
112 }
113 
114 
115 
116 void
dk4fput_initialize_stderr(void)117 dk4fput_initialize_stderr(void)
118 {
119 #if _WIN32 && (DK4_CHAR_SIZE > 1) && DK4_HAVE__SETMODE && defined(_MSC_VER)
120   if (0 == dk4fput_stderr_initialized) {
121     dk4fput_stderr_oldmode = _setmode(_fileno(stderr), _O_U16TEXT);
122     if (0 == _isatty(_fileno(stderr))) {
123       fputwc((wchar_t)0xFEFF, stderr);
124     }
125     dk4fput_stderr_initialized = 1;
126   }
127 #endif
128 }
129 
130 
131 
132 void
dk4fput_initialize_stderr_no_bom(void)133 dk4fput_initialize_stderr_no_bom(void)
134 {
135 #if _WIN32 && (DK4_CHAR_SIZE > 1) && DK4_HAVE__SETMODE && defined(_MSC_VER)
136   if (0 == dk4fput_stderr_initialized) {
137     dk4fput_stderr_oldmode = _setmode(_fileno(stderr), _O_U16TEXT);
138     dk4fput_stderr_initialized = 1;
139   }
140 #endif
141 }
142 
143 
144 
145 void
dk4fput_cleanup_stderr(void)146 dk4fput_cleanup_stderr(void)
147 {
148 #if _WIN32 && (DK4_CHAR_SIZE > 1) && DK4_HAVE__SETMODE && defined(_MSC_VER)
149   if (0 != dk4fput_stderr_initialized) {
150     fflush(stderr);
151     (void)_setmode(_fileno(stdout), dk4fput_stderr_oldmode);
152   }
153 #endif
154 }
155 
156 
157 void
dk4fput_initialize_file(FILE * fipo)158 dk4fput_initialize_file(
159 #if _WIN32 && (DK4_CHAR_SIZE > 1) && DK4_HAVE__SETMODE && defined(_MSC_VER)
160   FILE *fipo
161 #else
162   FILE * DK4_ARG_UNUSED(fipo)
163 #endif
164 )
165 {
166 #if _WIN32 && (DK4_CHAR_SIZE > 1) && DK4_HAVE__SETMODE && defined(_MSC_VER)
167 #if	DK4_USE_ASSERT
168   assert(NULL != fipo);
169 #endif
170   if (NULL != fipo) {
171     (void)_setmode(_fileno(fipo), _O_U16TEXT);
172     fputwc(0xFEFF, fipo);
173   }
174 #else
175   DK4_UNUSED_ARG(fipo)
176 #endif
177 }
178 
179 
180 
181 void
dk4fput_initialize_file_no_bom(FILE * fipo)182 dk4fput_initialize_file_no_bom(
183 #if _WIN32 && (DK4_CHAR_SIZE > 1) && DK4_HAVE__SETMODE && defined(_MSC_VER)
184   FILE *fipo
185 #else
186   FILE * DK4_ARG_UNUSED(fipo)
187 #endif
188 )
189 {
190 #if _WIN32 && (DK4_CHAR_SIZE > 1) && DK4_HAVE__SETMODE && defined(_MSC_VER)
191 #if	DK4_USE_ASSERT
192   assert(NULL != fipo);
193 #endif
194   if (NULL != fipo) {
195     (void)_setmode(_fileno(fipo), _O_U16TEXT);
196   }
197 #else
198   DK4_UNUSED_ARG(fipo)
199 #endif
200 }
201 
202 
203 
204 int
dk4fputc(dkChar c,FILE * fipo,dk4_er_t * erp)205 dk4fputc(dkChar c, FILE *fipo, dk4_er_t *erp)
206 {
207 #if DK4_CHAR_SIZE > 1
208 #if DK4_CHAR_SIZE == DK4_SIZEOF_WCHAR_T
209 #if DK4_HAVE_FPUTWC
210   int	back = 0;
211 
212 #if	DK4_USE_ASSERT
213   assert(NULL != fipo);
214 #endif
215   if (NULL != fipo) {
216     errno = 0;
217     if (WEOF != fputwc(c, fipo)) {
218       back = 1;
219     } else {
220       dk4error_set_idetails(erp, DK4_E_WRITE_FAILED, errno);
221     }
222   } else {
223     dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
224   }
225   return back;
226 #else
227   dk4error_set_simple_error_code(erp, DK4_E_NOT_SUPPORTED);
228   return 0;
229 #endif
230 #else
231   dk4error_set_simple_error_code(erp, DK4_E_NOT_SUPPORTED);
232   return 0;
233 #endif
234 #else
235   int	back = 0;
236 #if	DK4_USE_ASSERT
237   assert(NULL != fipo);
238 #endif
239   if (NULL != fipo) {
240     errno = 0;
241     if (EOF != fputc(c, fipo)) {
242       back = 1;
243     } else {
244       dk4error_set_idetails(erp, DK4_E_WRITE_FAILED, errno);
245     }
246   } else {
247     dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
248   }
249   return back;
250 #endif
251 }
252 
253 
254 
255 int
dk4fputs(const dkChar * s,FILE * fipo,dk4_er_t * erp)256 dk4fputs(const dkChar *s, FILE *fipo, dk4_er_t *erp)
257 {
258 #if DK4_CHAR_SIZE > 1
259 #if DK4_CHAR_SIZE == DK4_SIZEOF_WCHAR_T
260 #if DK4_HAVE_FPUTWS
261   int	back = 0;
262 #if	DK4_USE_ASSERT
263   assert(NULL != fipo);
264   assert(NULL != s);
265 #endif
266   if ((NULL != s) && (NULL != fipo)) {
267     errno = 0;
268     if (WEOF != fputws(s, fipo)) {
269       back = 1;
270     } else {
271       dk4error_set_idetails(erp, DK4_E_WRITE_FAILED, errno);
272     }
273   } else {
274     dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
275   }
276   return back;
277 #else
278   dk4error_set_simple_error_code(erp, DK4_E_NOT_SUPPORTED);
279   return 0;
280 #endif
281 #else
282   dk4error_set_simple_error_code(erp, DK4_E_NOT_SUPPORTED);
283   return 0;
284 #endif
285 #else
286   int	back = 0;
287 #if	DK4_USE_ASSERT
288   assert(NULL != fipo);
289   assert(NULL != s);
290 #endif
291   if ((NULL != s) && (NULL != fipo)) {
292     errno = 0;
293     if (EOF != fputs(s, fipo)) {
294       back = 1;
295     } else {
296       dk4error_set_idetails(erp, DK4_E_WRITE_FAILED, errno);
297     }
298   } else {
299     dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
300   }
301   return back;
302 #endif
303 }
304 
305