1%%	options
2
3copyright owner	=	Dirk Krause
4copyright year	=	2015-xxxx
5SPDX-License-Identifier:	BSD-3-Clause
6
7
8
9%%	header
10
11/**	@file
12	Simple conversion from char to wchar_t.
13
14	CRT on Windows: Not used.
15*/
16
17#ifndef DK4CONF_H_INCLUDED
18#if DK4_BUILDING_DKTOOLS4
19#include "dk4conf.h"
20#else
21#include <dktools-4/dk4conf.h>
22#endif
23#endif
24
25#ifndef DK4TYPES_H_INCLUDED
26#if DK4_BUILDING_DKTOOLS4
27#include <libdk4base/dk4types.h>
28#else
29#include <dktools-4/dk4types.h>
30#endif
31#endif
32
33#ifndef DK4ERROR_H_INCLUDED
34#if DK4_BUILDING_DKTOOLS4
35#include <libdk4base/dk4error.h>
36#else
37#include <dktools-4/dk4error.h>
38#endif
39#endif
40
41#ifdef __cplusplus
42extern "C" {
43#endif
44
45/**	Recode string from ASCII (0x00 to 0x7F) to wchar_t.
46	Use this function only for real ASCII source strings with characters
47	in range 0x00 to 0x7F (i.e. to convert numbers, dates, or
48	english text).
49	@param	dstb	Destination buffer.
50	@param	szdstb	Size of destination buffer (number of dkChar).
51	@param	src	Source string to convert.
52	@param	erp	Error report, may be NULL.
53	@return	1 on success, 0 on error.
54
55	Error codes:
56	- DK4_E_INVALID_ARGUMENTS<br>
57	  if dstb or src is NULL or szdstb is 0,
58	- DK4_E_BUFFER_TOO_SMALL<br>
59	  if the dstb buffer is too small.
60*/
61int
62dk4recode_char_to_wchar_t(
63  wchar_t *dstb, size_t szdstb, const char *src, dk4_er_t *erp
64);
65
66#ifdef __cplusplus
67}
68#endif
69
70
71%%	module
72
73#include "dk4conf.h"
74#include <libdk4c/dk4rec23.h>
75#include <libdk4base/dk4str8.h>
76
77#if DK4_HAVE_STRING_H
78#ifndef STRING_H_INCLUDED
79#include <string.h>
80#define	STRING_H_INCLUDED 1
81#endif
82#endif
83
84#if	DK4_HAVE_ASSERT_H
85#ifndef	ASSERT_H_INCLUDED
86#include <assert.h>
87#define	ASSERT_H_INCLUDED 1
88#endif
89#endif
90
91
92$!trace-include
93
94
95int
96dk4recode_char_to_wchar_t(
97  wchar_t *dstb, size_t szdstb, const char *src, dk4_er_t *erp
98)
99{
100  int		 back = 0;
101#if	DK4_USE_ASSERT
102  assert(NULL != dstb);
103  assert(0 < szdstb);
104  assert(NULL != src);
105#endif
106  if ((NULL != dstb) && (NULL != src) && (0 < szdstb)) {
107    *dstb = L'\0';
108    if (dk4str8_len(src) < szdstb) {
109      while ('\0' != *src) {
110        *(dstb++) = (wchar_t)((unsigned char)(*(src++)));
111      }
112      *dstb = L'\0';
113      back = 1;
114    } else {
115      dk4error_set_simple_error_code(erp, DK4_E_BUFFER_TOO_SMALL);
116    }
117  } else {
118    dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
119  }
120  return back;
121}
122
123
124