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