-
-
Notifications
You must be signed in to change notification settings - Fork 711
/
mbstowcs.c
107 lines (95 loc) 路 2.9 KB
/
mbstowcs.c
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
/*!The Treasure Box Library
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright (C) 2009-present, TBOOX Open Source Group.
*
* @author ruki
* @file mbstowcs.c
* @ingroup libc
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "stdlib.h"
#ifdef TB_CONFIG_LIBC_HAVE_MBSTOWCS
# include "setlocale.h"
# include <stdlib.h>
#endif
#ifdef TB_CONFIG_MODULE_HAVE_CHARSET
# include "../../charset/charset.h"
#endif
/* //////////////////////////////////////////////////////////////////////////////////////
* macros
*/
// select the implementation of mbstowcs
#ifdef TB_CONFIG_FORCE_UTF8
# if defined(TB_CONFIG_MODULE_HAVE_CHARSET)
# define TB_MBSTOWCS_IMPL_CHARSET
# elif defined(TB_CONFIG_LIBC_HAVE_MBSTOWCS)
# define TB_MBSTOWCS_IMPL_LIBC
# endif
#else
# if defined(TB_CONFIG_LIBC_HAVE_MBSTOWCS)
# define TB_MBSTOWCS_IMPL_LIBC
# elif defined(TB_CONFIG_MODULE_HAVE_CHARSET)
# define TB_MBSTOWCS_IMPL_CHARSET
# endif
#endif
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
#ifdef TB_MBSTOWCS_IMPL_LIBC
inline static tb_size_t tb_mbstowcs_libc(tb_wchar_t* s1, tb_char_t const* s2, tb_size_t n)
{
// set local locale
tb_setlocale();
// convert it
n = mbstowcs(s1, s2, n);
// set default locale
tb_resetlocale();
// ok
return n;
}
#endif
#ifdef TB_MBSTOWCS_IMPL_CHARSET
inline static tb_size_t tb_mbstowcs_charset(tb_wchar_t* s1, tb_char_t const* s2, tb_size_t n)
{
// check
tb_assert_and_check_return_val(s1 && s2, 0);
// init
tb_size_t e = (sizeof(tb_wchar_t) == 4) ? TB_CHARSET_TYPE_UTF32 : TB_CHARSET_TYPE_UTF16;
tb_long_t r = tb_charset_conv_cstr(TB_CHARSET_TYPE_UTF8, e | TB_CHARSET_TYPE_LE, s2,
(tb_byte_t*)s1, n * sizeof(tb_wchar_t));
if (r > 0) r /= sizeof(tb_wchar_t);
// strip
if (r >= 0) s1[r] = L'\0';
// ok?
return r >= 0 ? r : -1;
}
#endif
/* //////////////////////////////////////////////////////////////////////////////////////
* interfaces
*/
tb_size_t tb_mbstowcs(tb_wchar_t* s1, tb_char_t const* s2, tb_size_t n)
{
#if defined(TB_MBSTOWCS_IMPL_CHARSET)
return tb_mbstowcs_charset(s1, s2, n);
#elif defined(TB_MBSTOWCS_IMPL_LIBC)
return tb_mbstowcs_libc(s1, s2, n);
#else
tb_trace_noimpl();
return -1;
#endif
}