1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *****************************************************************************************
5 * Copyright (C) 2015, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 *****************************************************************************************
10 #include "unicode/utypes.h"
12 #if !UCONFIG_NO_FORMATTING
14 #include "unicode/ulistformatter.h"
15 #include "unicode/listformatter.h"
16 #include "unicode/localpointer.h"
21 U_CAPI UListFormatter* U_EXPORT2
22 ulistfmt_open(const char* locale,
25 if (U_FAILURE(*status)) {
28 LocalPointer<ListFormatter> listfmt(ListFormatter::createInstance(Locale(locale), *status));
29 if (U_FAILURE(*status)) {
32 return (UListFormatter*)listfmt.orphan();
37 ulistfmt_close(UListFormatter *listfmt)
39 delete (ListFormatter*)listfmt;
43 U_CAPI int32_t U_EXPORT2
44 ulistfmt_format(const UListFormatter* listfmt,
45 const UChar* const strings[],
46 const int32_t * stringLengths,
49 int32_t resultCapacity,
52 if (U_FAILURE(*status)) {
55 if (stringCount < 0 || (strings == NULL && stringCount > 0) || ((result == NULL)? resultCapacity != 0 : resultCapacity < 0)) {
56 *status = U_ILLEGAL_ARGUMENT_ERROR;
59 UnicodeString ustringsStackBuf[4];
60 UnicodeString* ustrings = ustringsStackBuf;
61 if (stringCount > UPRV_LENGTHOF(ustringsStackBuf)) {
62 ustrings = new UnicodeString[stringCount];
63 if (ustrings == NULL) {
64 *status = U_MEMORY_ALLOCATION_ERROR;
68 if (stringLengths == NULL) {
69 for (int32_t stringIndex = 0; stringIndex < stringCount; stringIndex++) {
70 ustrings[stringIndex].setTo(TRUE, strings[stringIndex], -1);
73 for (int32_t stringIndex = 0; stringIndex < stringCount; stringIndex++) {
74 ustrings[stringIndex].setTo(stringLengths[stringIndex] < 0, strings[stringIndex], stringLengths[stringIndex]);
79 // NULL destination for pure preflighting: empty dummy string
80 // otherwise, alias the destination buffer (copied from udat_format)
81 res.setTo(result, 0, resultCapacity);
83 ((const ListFormatter*)listfmt)->format( ustrings, stringCount, res, *status );
84 if (ustrings != ustringsStackBuf) {
87 return res.extract(result, resultCapacity, *status);
91 #endif /* #if !UCONFIG_NO_FORMATTING */