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
108
109
110
111
112
113
114
115
116
117
118
119
120
|
/*
* wchar.c - wide character support
*
* Copyright (c) 2014 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#include <wchar.h>
#include <malloc.h>
#include <string.h>
size_t wcslen(const wchar_t *s)
{
size_t len = 0;
while (*s++)
len++;
return len;
}
size_t wcsnlen(const wchar_t * s, size_t count)
{
const wchar_t *sc;
for (sc = s; count-- && *sc != L'\0'; ++sc)
/* nothing */;
return sc - s;
}
wchar_t *strdup_wchar(const wchar_t *src)
{
int len = wcslen(src);
wchar_t *tmp, *dst;
if (!(dst = malloc((len + 1) * sizeof(wchar_t))))
return NULL;
tmp = dst;
while ((*dst++ = *src++))
/* nothing */;
return tmp;
}
int mbtowc(wchar_t *pwc, const char *s, size_t n)
{
if (!s)
return 0; /* we don't mantain a non-trivial shift state */
if (n < 1)
return -1;
*pwc = *s;
return 1;
}
int wctomb(char *s, wchar_t wc)
{
*s = wc & 0xFF;
return 1;
}
char *strcpy_wchar_to_char(char *dst, const wchar_t *src)
{
char *ret = dst;
while (*src)
wctomb(dst++, *src++);
*dst = 0;
return ret;
}
wchar_t *strcpy_char_to_wchar(wchar_t *dst, const char *src)
{
wchar_t *ret = dst;
while (*src)
mbtowc(dst++, src++, 1);
*dst = 0;
return ret;
}
wchar_t *strdup_char_to_wchar(const char *src)
{
wchar_t *dst = malloc((strlen(src) + 1) * sizeof(wchar_t));
if (!dst)
return NULL;
strcpy_char_to_wchar(dst, src);
return dst;
}
char *strdup_wchar_to_char(const wchar_t *src)
{
char *dst = malloc((wcslen(src) + 1));
if (!dst)
return NULL;
strcpy_wchar_to_char(dst, src);
return dst;
}
|