2 *****************************************************************************************
3 * Copyright (C) 2015, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 *****************************************************************************************
8 #include "unicode/utypes.h"
10 #if !UCONFIG_NO_FORMATTING
12 #include "unicode/ulistformatter.h"
13 #include "unicode/listformatter.h"
14 #include "unicode/localpointer.h"
19 U_CAPI UListFormatter
* U_EXPORT2
20 ulistfmt_open(const char* locale
,
23 if (U_FAILURE(*status
)) {
26 LocalPointer
<ListFormatter
> listfmt(ListFormatter::createInstance(Locale(locale
), *status
));
27 if (U_FAILURE(*status
)) {
30 return (UListFormatter
*)listfmt
.orphan();
35 ulistfmt_close(UListFormatter
*listfmt
)
37 delete (ListFormatter
*)listfmt
;
41 U_CAPI
int32_t U_EXPORT2
42 ulistfmt_format(const UListFormatter
* listfmt
,
43 const UChar
* const strings
[],
44 const int32_t * stringLengths
,
47 int32_t resultCapacity
,
50 if (U_FAILURE(*status
)) {
53 if (stringCount
< 0 || (strings
== NULL
&& stringCount
> 0) || ((result
== NULL
)? resultCapacity
!= 0 : resultCapacity
< 0)) {
54 *status
= U_ILLEGAL_ARGUMENT_ERROR
;
57 UnicodeString ustringsStackBuf
[4];
58 UnicodeString
* ustrings
= ustringsStackBuf
;
59 if (stringCount
> UPRV_LENGTHOF(ustringsStackBuf
)) {
60 ustrings
= new UnicodeString
[stringCount
];
61 if (ustrings
== NULL
) {
62 *status
= U_MEMORY_ALLOCATION_ERROR
;
66 if (stringLengths
== NULL
) {
67 for (int32_t stringIndex
= 0; stringIndex
< stringCount
; stringIndex
++) {
68 ustrings
[stringIndex
].setTo(TRUE
, strings
[stringIndex
], -1);
71 for (int32_t stringIndex
= 0; stringIndex
< stringCount
; stringIndex
++) {
72 ustrings
[stringIndex
].setTo(stringLengths
[stringIndex
] < 0, strings
[stringIndex
], stringLengths
[stringIndex
]);
77 // NULL destination for pure preflighting: empty dummy string
78 // otherwise, alias the destination buffer (copied from udat_format)
79 res
.setTo(result
, 0, resultCapacity
);
81 ((const ListFormatter
*)listfmt
)->format( ustrings
, stringCount
, res
, *status
);
82 if (ustrings
!= ustringsStackBuf
) {
85 return res
.extract(result
, resultCapacity
, *status
);
89 #endif /* #if !UCONFIG_NO_FORMATTING */