Line data Source code
1 : // © 2016 and later: Unicode, Inc. and others.
2 : // License & terms of use: http://www.unicode.org/copyright.html
3 : /*
4 : *****************************************************************************************
5 : * Copyright (C) 2015, International Business Machines
6 : * Corporation and others. All Rights Reserved.
7 : *****************************************************************************************
8 : */
9 :
10 : #include "unicode/utypes.h"
11 :
12 : #if !UCONFIG_NO_FORMATTING
13 :
14 : #include "unicode/ulistformatter.h"
15 : #include "unicode/listformatter.h"
16 : #include "unicode/localpointer.h"
17 : #include "cmemory.h"
18 :
19 : U_NAMESPACE_USE
20 :
21 : U_CAPI UListFormatter* U_EXPORT2
22 0 : ulistfmt_open(const char* locale,
23 : UErrorCode* status)
24 : {
25 0 : if (U_FAILURE(*status)) {
26 0 : return NULL;
27 : }
28 0 : LocalPointer<ListFormatter> listfmt(ListFormatter::createInstance(Locale(locale), *status));
29 0 : if (U_FAILURE(*status)) {
30 0 : return NULL;
31 : }
32 0 : return (UListFormatter*)listfmt.orphan();
33 : }
34 :
35 :
36 : U_CAPI void U_EXPORT2
37 0 : ulistfmt_close(UListFormatter *listfmt)
38 : {
39 0 : delete (ListFormatter*)listfmt;
40 0 : }
41 :
42 :
43 : U_CAPI int32_t U_EXPORT2
44 0 : ulistfmt_format(const UListFormatter* listfmt,
45 : const UChar* const strings[],
46 : const int32_t * stringLengths,
47 : int32_t stringCount,
48 : UChar* result,
49 : int32_t resultCapacity,
50 : UErrorCode* status)
51 : {
52 0 : if (U_FAILURE(*status)) {
53 0 : return -1;
54 : }
55 0 : if (stringCount < 0 || (strings == NULL && stringCount > 0) || ((result == NULL)? resultCapacity != 0 : resultCapacity < 0)) {
56 0 : *status = U_ILLEGAL_ARGUMENT_ERROR;
57 0 : return -1;
58 : }
59 0 : UnicodeString ustringsStackBuf[4];
60 0 : UnicodeString* ustrings = ustringsStackBuf;
61 0 : if (stringCount > UPRV_LENGTHOF(ustringsStackBuf)) {
62 0 : ustrings = new UnicodeString[stringCount];
63 0 : if (ustrings == NULL) {
64 0 : *status = U_MEMORY_ALLOCATION_ERROR;
65 0 : return -1;
66 : }
67 : }
68 0 : if (stringLengths == NULL) {
69 0 : for (int32_t stringIndex = 0; stringIndex < stringCount; stringIndex++) {
70 0 : ustrings[stringIndex].setTo(TRUE, strings[stringIndex], -1);
71 : }
72 : } else {
73 0 : for (int32_t stringIndex = 0; stringIndex < stringCount; stringIndex++) {
74 0 : ustrings[stringIndex].setTo(stringLengths[stringIndex] < 0, strings[stringIndex], stringLengths[stringIndex]);
75 : }
76 : }
77 0 : UnicodeString res;
78 0 : if (result != NULL) {
79 : // NULL destination for pure preflighting: empty dummy string
80 : // otherwise, alias the destination buffer (copied from udat_format)
81 0 : res.setTo(result, 0, resultCapacity);
82 : }
83 0 : ((const ListFormatter*)listfmt)->format( ustrings, stringCount, res, *status );
84 0 : if (ustrings != ustringsStackBuf) {
85 0 : delete[] ustrings;
86 : }
87 0 : return res.extract(result, resultCapacity, *status);
88 : }
89 :
90 :
91 : #endif /* #if !UCONFIG_NO_FORMATTING */
|