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	String conversion from ASCII to dkChar.
13
14	CRT on Windows: Not used.
15*/
16
17
18
19#ifndef DK4CONF_H_INCLUDED
20#if DK4_BUILDING_DKTOOLS4
21#include "dk4conf.h"
22#else
23#include <dktools-4/dk4conf.h>
24#endif
25#endif
26
27#ifndef DK4TYPES_H_INCLUDED
28#if DK4_BUILDING_DKTOOLS4
29#include <libdk4base/dk4types.h>
30#else
31#include <dktools-4/dk4types.h>
32#endif
33#endif
34
35#ifndef DK4ERROR_H_INCLUDED
36#if DK4_BUILDING_DKTOOLS4
37#include <libdk4base/dk4error.h>
38#else
39#include <dktools-4/dk4error.h>
40#endif
41#endif
42
43
44
45
46#ifdef __cplusplus
47extern "C" {
48#endif
49
50/**	Recode string from ASCII to dkChar
51	@param	dstb	Destination buffer.
52	@param	szdstb	Size of destination buffer (number of dkChar).
53	@param	enc	Encoding for dkChar.
54	@param	src	Source string to convert.
55	@param	erp	Error report, may be NULL.
56	@return	1 on success, 0 on error.
57
58	Error codes:
59	- DK4_E_INVALID_ARGUMENTS<br>
60	  if src or dstb is NULL or szdstb is 0,
61	- DK4_E_BUFFER_TOO_SMALL<br>
62	  if dstb is too small,
63	- DK4_E_SYNTAX<br>
64	  with the number of successfully recoded characters in nelem if a
65	  non-recodable character was found.
66*/
67int
68dk4recode_ascii_to_dk(
69  dkChar *dstb, size_t szdstb, int enc, const char *src, dk4_er_t *erp
70);
71
72#ifdef __cplusplus
73}
74#endif
75
76
77
78%%	module
79
80#include "dk4conf.h"
81
82#ifndef	DK4USE_H_INCLUDED
83#include <libdk4base/dk4use.h>
84#endif
85
86#include <libdk4c/dk4enc.h>
87#include <libdk4c/dk4rec21.h>
88#include <libdk4c/dk4rec01.h>
89#include <libdk4c/dk4rec03.h>
90#include <libdk4c/dk4rec07.h>
91#include <libdk4c/dk4rec13.h>
92#include <libdk4base/dk4str8.h>
93#include <libdk4base/dk4unused.h>
94
95#if	DK4_HAVE_ASSERT_H
96#ifndef	ASSERT_H_INCLUDED
97#include <assert.h>
98#define	ASSERT_H_INCLUDED 1
99#endif
100#endif
101
102
103#if DK4_USE_PRAGMA_WARNING_DISABLE
104#pragma warning( push )
105#pragma warning( disable: 4100 )
106#endif
107
108int
109dk4recode_ascii_to_dk(
110  dkChar	*dstb,
111  size_t	 szdstb,
112#if DK4_CHAR_SIZE > 1
113  int		 DK4_ARG_UNUSED(enc),
114#else
115  int		 enc,
116#endif
117  const char	*src,
118  dk4_er_t	*erp
119)
120{
121  int		 back = 0;
122#if	DK4_USE_ASSERT
123  assert(NULL != dstb);
124  assert(0 < szdstb);
125  assert(NULL != src);
126#endif
127  if ((NULL != dstb) && (0 < szdstb) && (NULL != src)) {
128#if DK4_CHAR_SIZE > 1
129    DK4_UNUSED_ARG(enc)
130#if DK4_CHAR_SIZE > 2
131    back = dk4recode_ascii_to_c32(dstb, szdstb, src, erp);
132#else
133    back = dk4recode_ascii_to_utf16(dstb, szdstb, src, erp);
134#endif
135#else
136    switch (enc) {
137      case DK4_ENCODING_UTF8: {
138        back = dk4recode_ascii_to_utf8(dstb, szdstb, src, erp);
139      } break;
140      case DK4_ENCODING_WIN1252: {
141        back = dk4recode_ascii_to_ansi(dstb, szdstb, src, erp);
142      } break;
143      default: {
144        back = dk4str8_cpy_s(dstb, szdstb, src, erp);
145      } break;
146    }
147#endif
148  } else {
149    dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
150  }
151  return back;
152}
153
154#if _WIN32 && defined(_MSC_VER) && (_MSC_VER >= 1700)
155#pragma warning( pop )
156#endif
157
158