|
1 /*- |
|
2 * Portions Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).All rights reserved. |
|
3 * |
|
4 * Copyright (c) 1995 Alex Tatmanjants <alex@elvisti.kiev.ua> |
|
5 * at Electronni Visti IA, Kiev, Ukraine. |
|
6 * All rights reserved. |
|
7 * |
|
8 * Redistribution and use in source and binary forms, with or without |
|
9 * modification, are permitted provided that the following conditions |
|
10 * are met: |
|
11 * 1. Redistributions of source code must retain the above copyright |
|
12 * notice, this list of conditions and the following disclaimer. |
|
13 * 2. Redistributions in binary form must reproduce the above copyright |
|
14 * notice, this list of conditions and the following disclaimer in the |
|
15 * documentation and/or other materials provided with the distribution. |
|
16 * |
|
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND |
|
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE |
|
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
|
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
|
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
|
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
|
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
|
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
|
27 * SUCH DAMAGE. |
|
28 */ |
|
29 |
|
30 #include <sys/cdefs.h> |
|
31 __FBSDID("FreeBSD: src/lib/libc/string/strxfrm.c,v 1.15 2002/09/06 11:24:06 tjr Exp "); |
|
32 __FBSDID("$FreeBSD: src/lib/libc/string/wcsxfrm.c,v 1.3 2004/04/07 09:47:56 tjr Exp $"); |
|
33 |
|
34 #include <stdlib.h> |
|
35 #include <string.h> |
|
36 #include <wchar.h> |
|
37 #ifndef __SYMBIAN32__ |
|
38 #include "libc_collate.h" |
|
39 #endif //__SYMBIAN32__ |
|
40 |
|
41 #if (defined(__SYMBIAN32__) && (defined(__WINSCW__) || defined(__WINS__))) |
|
42 #include "libc_wsd_defs.h" |
|
43 #endif |
|
44 |
|
45 #ifndef __SYMBIAN32__ |
|
46 static char *__mbsdup(const wchar_t *); |
|
47 #endif //__SYMBIAN32__ |
|
48 |
|
49 #ifdef __SYMBIAN32__ |
|
50 extern char LC_COLLATE_LocaleName[30]; |
|
51 #ifdef EMULATOR |
|
52 |
|
53 char *GET_WSD_VAR_NAME(LC_COLLATE_LocaleName, g)(); |
|
54 #define LC_COLLATE_LocaleName (GET_WSD_VAR_NAME(LC_COLLATE_LocaleName, g)()) |
|
55 #endif //EMULATOR |
|
56 |
|
57 #endif //__SYMBIAN32__ |
|
58 |
|
59 /* |
|
60 * Placeholder wcsxfrm() implementation. See wcscoll.c for a description of |
|
61 * the logic used. |
|
62 */ |
|
63 EXPORT_C size_t |
|
64 wcsxfrm(wchar_t * __restrict dest, const wchar_t * __restrict src, size_t len) |
|
65 { |
|
66 #ifndef __SYMBIAN32__ |
|
67 int prim, sec, l; |
|
68 char *mbsrc, *s, *ss; |
|
69 #endif//__SYMBIAN32__ |
|
70 size_t slen; |
|
71 |
|
72 if (*src == L'\0') { |
|
73 if (len != 0) |
|
74 *dest = L'\0'; |
|
75 return (0); |
|
76 } |
|
77 |
|
78 #ifndef __SYMBIAN32__ |
|
79 if (__collate_load_error || MB_CUR_MAX > 1) |
|
80 #else |
|
81 if(((strcmp("C",(const char*) LC_COLLATE_LocaleName) ==0) ||(strcmp("POSIX", (const char*) LC_COLLATE_LocaleName) ==0)) || |
|
82 MB_CUR_MAX > 1) |
|
83 #endif |
|
84 { |
|
85 slen = wcslen(src); |
|
86 if (len > 0) { |
|
87 if (slen < len) |
|
88 wcscpy(dest, src); |
|
89 else { |
|
90 wcsncpy(dest, src, len - 1); |
|
91 dest[len - 1] = L'\0'; |
|
92 } |
|
93 } |
|
94 return (slen); |
|
95 } |
|
96 #ifdef __SYMBIAN32__ |
|
97 else |
|
98 { |
|
99 return (0); |
|
100 } |
|
101 #endif //__SYMBIAN32__ |
|
102 |
|
103 #ifndef __SYMBIAN32__ |
|
104 mbsrc = __mbsdup(src); |
|
105 slen = 0; |
|
106 prim = sec = 0; |
|
107 ss = s = __collate_substitute(mbsrc); |
|
108 while (*s != '\0') { |
|
109 while (*s != '\0' && prim == 0) { |
|
110 __collate_lookup(s, &l, &prim, &sec); |
|
111 s += l; |
|
112 } |
|
113 if (prim != 0) { |
|
114 if (len > 1) { |
|
115 *dest++ = (wchar_t)prim; |
|
116 len--; |
|
117 } |
|
118 slen++; |
|
119 prim = 0; |
|
120 } |
|
121 } |
|
122 free(ss); |
|
123 free(mbsrc); |
|
124 if (len != 0) |
|
125 *dest = L'\0'; |
|
126 |
|
127 return (slen); |
|
128 #endif //__SYMBIAN32__ |
|
129 } |
|
130 |
|
131 #ifndef __SYMBIAN32__ |
|
132 static char * |
|
133 __mbsdup(const wchar_t *ws) |
|
134 { |
|
135 static const mbstate_t initial = {0,{0}}; |
|
136 mbstate_t st; |
|
137 const wchar_t *wcp; |
|
138 size_t len; |
|
139 char *mbs; |
|
140 |
|
141 wcp = ws; |
|
142 st = initial; |
|
143 if ((len = wcsrtombs(NULL, &wcp, 0, &st)) == (size_t)-1) |
|
144 return (NULL); |
|
145 if ((mbs = malloc(len + 1)) == NULL) |
|
146 return (NULL); |
|
147 st = initial; |
|
148 wcsrtombs(mbs, &ws, len + 1, &st); |
|
149 |
|
150 return (mbs); |
|
151 } |
|
152 #endif //__SYMBIAN32__ |