intl/icu/source/common/ustr_cnv.c

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /*
michael@0 2 *******************************************************************************
michael@0 3 *
michael@0 4 * Copyright (C) 1998-2013, International Business Machines
michael@0 5 * Corporation and others. All Rights Reserved.
michael@0 6 *
michael@0 7 *******************************************************************************
michael@0 8 * file name: ustr_cnv.c
michael@0 9 * encoding: US-ASCII
michael@0 10 * tab size: 8 (not used)
michael@0 11 * indentation:4
michael@0 12 *
michael@0 13 * created on: 2004aug24
michael@0 14 * created by: Markus W. Scherer
michael@0 15 *
michael@0 16 * Character conversion functions moved here from ustring.c
michael@0 17 */
michael@0 18
michael@0 19 #include "unicode/utypes.h"
michael@0 20
michael@0 21 #if !UCONFIG_NO_CONVERSION
michael@0 22
michael@0 23 #include "unicode/ustring.h"
michael@0 24 #include "unicode/ucnv.h"
michael@0 25 #include "cstring.h"
michael@0 26 #include "cmemory.h"
michael@0 27 #include "cmutex.h"
michael@0 28 #include "ustr_cnv.h"
michael@0 29
michael@0 30 /* mutexed access to a shared default converter ----------------------------- */
michael@0 31
michael@0 32 static UConverter *gDefaultConverter = NULL;
michael@0 33
michael@0 34 U_CAPI UConverter* U_EXPORT2
michael@0 35 u_getDefaultConverter(UErrorCode *status)
michael@0 36 {
michael@0 37 UConverter *converter = NULL;
michael@0 38
michael@0 39 if (gDefaultConverter != NULL) {
michael@0 40 umtx_lock(NULL);
michael@0 41
michael@0 42 /* need to check to make sure it wasn't taken out from under us */
michael@0 43 if (gDefaultConverter != NULL) {
michael@0 44 converter = gDefaultConverter;
michael@0 45 gDefaultConverter = NULL;
michael@0 46 }
michael@0 47 umtx_unlock(NULL);
michael@0 48 }
michael@0 49
michael@0 50 /* if the cache was empty, create a converter */
michael@0 51 if(converter == NULL) {
michael@0 52 converter = ucnv_open(NULL, status);
michael@0 53 if(U_FAILURE(*status)) {
michael@0 54 ucnv_close(converter);
michael@0 55 converter = NULL;
michael@0 56 }
michael@0 57 }
michael@0 58
michael@0 59 return converter;
michael@0 60 }
michael@0 61
michael@0 62 U_CAPI void U_EXPORT2
michael@0 63 u_releaseDefaultConverter(UConverter *converter)
michael@0 64 {
michael@0 65 if(gDefaultConverter == NULL) {
michael@0 66 if (converter != NULL) {
michael@0 67 ucnv_reset(converter);
michael@0 68 }
michael@0 69 umtx_lock(NULL);
michael@0 70
michael@0 71 if(gDefaultConverter == NULL) {
michael@0 72 gDefaultConverter = converter;
michael@0 73 converter = NULL;
michael@0 74 }
michael@0 75 umtx_unlock(NULL);
michael@0 76 }
michael@0 77
michael@0 78 if(converter != NULL) {
michael@0 79 ucnv_close(converter);
michael@0 80 }
michael@0 81 }
michael@0 82
michael@0 83 U_CAPI void U_EXPORT2
michael@0 84 u_flushDefaultConverter()
michael@0 85 {
michael@0 86 UConverter *converter = NULL;
michael@0 87
michael@0 88 if (gDefaultConverter != NULL) {
michael@0 89 umtx_lock(NULL);
michael@0 90
michael@0 91 /* need to check to make sure it wasn't taken out from under us */
michael@0 92 if (gDefaultConverter != NULL) {
michael@0 93 converter = gDefaultConverter;
michael@0 94 gDefaultConverter = NULL;
michael@0 95 }
michael@0 96 umtx_unlock(NULL);
michael@0 97 }
michael@0 98
michael@0 99 /* if the cache was populated, flush it */
michael@0 100 if(converter != NULL) {
michael@0 101 ucnv_close(converter);
michael@0 102 }
michael@0 103 }
michael@0 104
michael@0 105
michael@0 106 /* conversions between char* and UChar* ------------------------------------- */
michael@0 107
michael@0 108 /* maximum string length for u_uastrcpy() and u_austrcpy() implementations */
michael@0 109 #define MAX_STRLEN 0x0FFFFFFF
michael@0 110
michael@0 111 /*
michael@0 112 returns the minimum of (the length of the null-terminated string) and n.
michael@0 113 */
michael@0 114 static int32_t u_astrnlen(const char *s1, int32_t n)
michael@0 115 {
michael@0 116 int32_t len = 0;
michael@0 117
michael@0 118 if (s1)
michael@0 119 {
michael@0 120 while (n-- && *(s1++))
michael@0 121 {
michael@0 122 len++;
michael@0 123 }
michael@0 124 }
michael@0 125 return len;
michael@0 126 }
michael@0 127
michael@0 128 U_CAPI UChar* U_EXPORT2
michael@0 129 u_uastrncpy(UChar *ucs1,
michael@0 130 const char *s2,
michael@0 131 int32_t n)
michael@0 132 {
michael@0 133 UChar *target = ucs1;
michael@0 134 UErrorCode err = U_ZERO_ERROR;
michael@0 135 UConverter *cnv = u_getDefaultConverter(&err);
michael@0 136 if(U_SUCCESS(err) && cnv != NULL) {
michael@0 137 ucnv_reset(cnv);
michael@0 138 ucnv_toUnicode(cnv,
michael@0 139 &target,
michael@0 140 ucs1+n,
michael@0 141 &s2,
michael@0 142 s2+u_astrnlen(s2, n),
michael@0 143 NULL,
michael@0 144 TRUE,
michael@0 145 &err);
michael@0 146 ucnv_reset(cnv); /* be good citizens */
michael@0 147 u_releaseDefaultConverter(cnv);
michael@0 148 if(U_FAILURE(err) && (err != U_BUFFER_OVERFLOW_ERROR) ) {
michael@0 149 *ucs1 = 0; /* failure */
michael@0 150 }
michael@0 151 if(target < (ucs1+n)) { /* U_BUFFER_OVERFLOW_ERROR isn't an err, just means no termination will happen. */
michael@0 152 *target = 0; /* terminate */
michael@0 153 }
michael@0 154 } else {
michael@0 155 *ucs1 = 0;
michael@0 156 }
michael@0 157 return ucs1;
michael@0 158 }
michael@0 159
michael@0 160 U_CAPI UChar* U_EXPORT2
michael@0 161 u_uastrcpy(UChar *ucs1,
michael@0 162 const char *s2 )
michael@0 163 {
michael@0 164 UErrorCode err = U_ZERO_ERROR;
michael@0 165 UConverter *cnv = u_getDefaultConverter(&err);
michael@0 166 if(U_SUCCESS(err) && cnv != NULL) {
michael@0 167 ucnv_toUChars(cnv,
michael@0 168 ucs1,
michael@0 169 MAX_STRLEN,
michael@0 170 s2,
michael@0 171 (int32_t)uprv_strlen(s2),
michael@0 172 &err);
michael@0 173 u_releaseDefaultConverter(cnv);
michael@0 174 if(U_FAILURE(err)) {
michael@0 175 *ucs1 = 0;
michael@0 176 }
michael@0 177 } else {
michael@0 178 *ucs1 = 0;
michael@0 179 }
michael@0 180 return ucs1;
michael@0 181 }
michael@0 182
michael@0 183 /*
michael@0 184 returns the minimum of (the length of the null-terminated string) and n.
michael@0 185 */
michael@0 186 static int32_t u_ustrnlen(const UChar *ucs1, int32_t n)
michael@0 187 {
michael@0 188 int32_t len = 0;
michael@0 189
michael@0 190 if (ucs1)
michael@0 191 {
michael@0 192 while (n-- && *(ucs1++))
michael@0 193 {
michael@0 194 len++;
michael@0 195 }
michael@0 196 }
michael@0 197 return len;
michael@0 198 }
michael@0 199
michael@0 200 U_CAPI char* U_EXPORT2
michael@0 201 u_austrncpy(char *s1,
michael@0 202 const UChar *ucs2,
michael@0 203 int32_t n)
michael@0 204 {
michael@0 205 char *target = s1;
michael@0 206 UErrorCode err = U_ZERO_ERROR;
michael@0 207 UConverter *cnv = u_getDefaultConverter(&err);
michael@0 208 if(U_SUCCESS(err) && cnv != NULL) {
michael@0 209 ucnv_reset(cnv);
michael@0 210 ucnv_fromUnicode(cnv,
michael@0 211 &target,
michael@0 212 s1+n,
michael@0 213 &ucs2,
michael@0 214 ucs2+u_ustrnlen(ucs2, n),
michael@0 215 NULL,
michael@0 216 TRUE,
michael@0 217 &err);
michael@0 218 ucnv_reset(cnv); /* be good citizens */
michael@0 219 u_releaseDefaultConverter(cnv);
michael@0 220 if(U_FAILURE(err) && (err != U_BUFFER_OVERFLOW_ERROR) ) {
michael@0 221 *s1 = 0; /* failure */
michael@0 222 }
michael@0 223 if(target < (s1+n)) { /* U_BUFFER_OVERFLOW_ERROR isn't an err, just means no termination will happen. */
michael@0 224 *target = 0; /* terminate */
michael@0 225 }
michael@0 226 } else {
michael@0 227 *s1 = 0;
michael@0 228 }
michael@0 229 return s1;
michael@0 230 }
michael@0 231
michael@0 232 U_CAPI char* U_EXPORT2
michael@0 233 u_austrcpy(char *s1,
michael@0 234 const UChar *ucs2 )
michael@0 235 {
michael@0 236 UErrorCode err = U_ZERO_ERROR;
michael@0 237 UConverter *cnv = u_getDefaultConverter(&err);
michael@0 238 if(U_SUCCESS(err) && cnv != NULL) {
michael@0 239 int32_t len = ucnv_fromUChars(cnv,
michael@0 240 s1,
michael@0 241 MAX_STRLEN,
michael@0 242 ucs2,
michael@0 243 -1,
michael@0 244 &err);
michael@0 245 u_releaseDefaultConverter(cnv);
michael@0 246 s1[len] = 0;
michael@0 247 } else {
michael@0 248 *s1 = 0;
michael@0 249 }
michael@0 250 return s1;
michael@0 251 }
michael@0 252
michael@0 253 #endif

mercurial