]>
git.saurik.com Git - apple/icu.git/blob - icuSources/common/ustr_cnv.c
2 *******************************************************************************
4 * Copyright (C) 1998-2004, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
8 * file name: ustr_cnv.c
10 * tab size: 8 (not used)
13 * created on: 2004aug24
14 * created by: Markus W. Scherer
16 * Character conversion functions moved here from ustring.c
19 #include "unicode/utypes.h"
21 #if !UCONFIG_NO_CONVERSION
23 #include "unicode/ustring.h"
24 #include "unicode/ucnv.h"
30 /* mutexed access to a shared default converter ----------------------------- */
32 static UConverter
*gDefaultConverter
= NULL
;
34 U_CAPI UConverter
* U_EXPORT2
35 u_getDefaultConverter(UErrorCode
*status
)
37 UConverter
*converter
= NULL
;
39 if (gDefaultConverter
!= NULL
) {
42 /* need to check to make sure it wasn't taken out from under us */
43 if (gDefaultConverter
!= NULL
) {
44 converter
= gDefaultConverter
;
45 gDefaultConverter
= NULL
;
50 /* if the cache was empty, create a converter */
51 if(converter
== NULL
) {
52 converter
= ucnv_open(NULL
, status
);
53 if(U_FAILURE(*status
)) {
54 ucnv_close(converter
);
63 u_releaseDefaultConverter(UConverter
*converter
)
65 if(gDefaultConverter
== NULL
) {
66 if (converter
!= NULL
) {
67 ucnv_reset(converter
);
71 if(gDefaultConverter
== NULL
) {
72 gDefaultConverter
= converter
;
78 if(converter
!= NULL
) {
79 ucnv_close(converter
);
83 /* conversions between char* and UChar* ------------------------------------- */
85 /* maximum string length for u_uastrcpy() and u_austrcpy() implementations */
86 #define MAX_STRLEN 0x0FFFFFFF
89 returns the minimum of (the length of the null-terminated string) and n.
91 static int32_t u_astrnlen(const char *s1
, int32_t n
)
97 while (n
-- && *(s1
++))
105 U_CAPI UChar
* U_EXPORT2
106 u_uastrncpy(UChar
*ucs1
,
110 UChar
*target
= ucs1
;
111 UErrorCode err
= U_ZERO_ERROR
;
112 UConverter
*cnv
= u_getDefaultConverter(&err
);
113 if(U_SUCCESS(err
) && cnv
!= NULL
) {
119 s2
+u_astrnlen(s2
, n
),
123 ucnv_reset(cnv
); /* be good citizens */
124 u_releaseDefaultConverter(cnv
);
125 if(U_FAILURE(err
) && (err
!= U_BUFFER_OVERFLOW_ERROR
) ) {
126 *ucs1
= 0; /* failure */
128 if(target
< (ucs1
+n
)) { /* U_BUFFER_OVERFLOW_ERROR isn't an err, just means no termination will happen. */
129 *target
= 0; /* terminate */
137 U_CAPI UChar
* U_EXPORT2
138 u_uastrcpy(UChar
*ucs1
,
141 UErrorCode err
= U_ZERO_ERROR
;
142 UConverter
*cnv
= u_getDefaultConverter(&err
);
143 if(U_SUCCESS(err
) && cnv
!= NULL
) {
148 (int32_t)uprv_strlen(s2
),
150 u_releaseDefaultConverter(cnv
);
161 returns the minimum of (the length of the null-terminated string) and n.
163 static int32_t u_ustrnlen(const UChar
*ucs1
, int32_t n
)
169 while (n
-- && *(ucs1
++))
177 U_CAPI
char* U_EXPORT2
178 u_austrncpy(char *s1
,
183 UErrorCode err
= U_ZERO_ERROR
;
184 UConverter
*cnv
= u_getDefaultConverter(&err
);
185 if(U_SUCCESS(err
) && cnv
!= NULL
) {
187 ucnv_fromUnicode(cnv
,
191 ucs2
+u_ustrnlen(ucs2
, n
),
195 ucnv_reset(cnv
); /* be good citizens */
196 u_releaseDefaultConverter(cnv
);
197 if(U_FAILURE(err
) && (err
!= U_BUFFER_OVERFLOW_ERROR
) ) {
198 *s1
= 0; /* failure */
200 if(target
< (s1
+n
)) { /* U_BUFFER_OVERFLOW_ERROR isn't an err, just means no termination will happen. */
201 *target
= 0; /* terminate */
209 U_CAPI
char* U_EXPORT2
213 UErrorCode err
= U_ZERO_ERROR
;
214 UConverter
*cnv
= u_getDefaultConverter(&err
);
215 if(U_SUCCESS(err
) && cnv
!= NULL
) {
216 int32_t len
= ucnv_fromUChars(cnv
,
222 u_releaseDefaultConverter(cnv
);