]>
git.saurik.com Git - apple/icu.git/blob - icuSources/common/ustr_cnv.cpp
2 *******************************************************************************
4 * Copyright (C) 1998-2014, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
8 * file name: ustr_cnv.cpp
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
);
84 u_flushDefaultConverter()
86 UConverter
*converter
= NULL
;
88 if (gDefaultConverter
!= NULL
) {
91 /* need to check to make sure it wasn't taken out from under us */
92 if (gDefaultConverter
!= NULL
) {
93 converter
= gDefaultConverter
;
94 gDefaultConverter
= NULL
;
99 /* if the cache was populated, flush it */
100 if(converter
!= NULL
) {
101 ucnv_close(converter
);
106 /* conversions between char* and UChar* ------------------------------------- */
108 /* maximum string length for u_uastrcpy() and u_austrcpy() implementations */
109 #define MAX_STRLEN 0x0FFFFFFF
112 returns the minimum of (the length of the null-terminated string) and n.
114 static int32_t u_astrnlen(const char *s1
, int32_t n
)
120 while (n
-- && *(s1
++))
128 U_CAPI UChar
* U_EXPORT2
129 u_uastrncpy(UChar
*ucs1
,
133 UChar
*target
= ucs1
;
134 UErrorCode err
= U_ZERO_ERROR
;
135 UConverter
*cnv
= u_getDefaultConverter(&err
);
136 if(U_SUCCESS(err
) && cnv
!= NULL
) {
142 s2
+u_astrnlen(s2
, n
),
146 ucnv_reset(cnv
); /* be good citizens */
147 u_releaseDefaultConverter(cnv
);
148 if(U_FAILURE(err
) && (err
!= U_BUFFER_OVERFLOW_ERROR
) ) {
149 *ucs1
= 0; /* failure */
151 if(target
< (ucs1
+n
)) { /* U_BUFFER_OVERFLOW_ERROR isn't an err, just means no termination will happen. */
152 *target
= 0; /* terminate */
160 U_CAPI UChar
* U_EXPORT2
161 u_uastrcpy(UChar
*ucs1
,
164 UErrorCode err
= U_ZERO_ERROR
;
165 UConverter
*cnv
= u_getDefaultConverter(&err
);
166 if(U_SUCCESS(err
) && cnv
!= NULL
) {
171 (int32_t)uprv_strlen(s2
),
173 u_releaseDefaultConverter(cnv
);
184 returns the minimum of (the length of the null-terminated string) and n.
186 static int32_t u_ustrnlen(const UChar
*ucs1
, int32_t n
)
192 while (n
-- && *(ucs1
++))
200 U_CAPI
char* U_EXPORT2
201 u_austrncpy(char *s1
,
206 UErrorCode err
= U_ZERO_ERROR
;
207 UConverter
*cnv
= u_getDefaultConverter(&err
);
208 if(U_SUCCESS(err
) && cnv
!= NULL
) {
210 ucnv_fromUnicode(cnv
,
214 ucs2
+u_ustrnlen(ucs2
, n
),
218 ucnv_reset(cnv
); /* be good citizens */
219 u_releaseDefaultConverter(cnv
);
220 if(U_FAILURE(err
) && (err
!= U_BUFFER_OVERFLOW_ERROR
) ) {
221 *s1
= 0; /* failure */
223 if(target
< (s1
+n
)) { /* U_BUFFER_OVERFLOW_ERROR isn't an err, just means no termination will happen. */
224 *target
= 0; /* terminate */
232 U_CAPI
char* U_EXPORT2
236 UErrorCode err
= U_ZERO_ERROR
;
237 UConverter
*cnv
= u_getDefaultConverter(&err
);
238 if(U_SUCCESS(err
) && cnv
!= NULL
) {
239 int32_t len
= ucnv_fromUChars(cnv
,
245 u_releaseDefaultConverter(cnv
);