• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
ulistfmt_open(const char * locale,UErrorCode * status)22 ulistfmt_open(const char*  locale,
23               UErrorCode*  status)
24 {
25     if (U_FAILURE(*status)) {
26         return NULL;
27     }
28     LocalPointer<ListFormatter> listfmt(ListFormatter::createInstance(Locale(locale), *status));
29     if (U_FAILURE(*status)) {
30         return NULL;
31     }
32     return (UListFormatter*)listfmt.orphan();
33 }
34 
35 
36 U_CAPI void U_EXPORT2
ulistfmt_close(UListFormatter * listfmt)37 ulistfmt_close(UListFormatter *listfmt)
38 {
39     delete (ListFormatter*)listfmt;
40 }
41 
42 
43 U_CAPI int32_t U_EXPORT2
ulistfmt_format(const UListFormatter * listfmt,const UChar * const strings[],const int32_t * stringLengths,int32_t stringCount,UChar * result,int32_t resultCapacity,UErrorCode * status)44 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     if (U_FAILURE(*status)) {
53         return -1;
54     }
55     if (stringCount < 0 || (strings == NULL && stringCount > 0) || ((result == NULL)? resultCapacity != 0 : resultCapacity < 0)) {
56         *status = U_ILLEGAL_ARGUMENT_ERROR;
57         return -1;
58     }
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;
65             return -1;
66         }
67     }
68     if (stringLengths == NULL) {
69         for (int32_t stringIndex = 0; stringIndex < stringCount; stringIndex++) {
70             ustrings[stringIndex].setTo(TRUE, strings[stringIndex], -1);
71         }
72     } else {
73         for (int32_t stringIndex = 0; stringIndex < stringCount; stringIndex++) {
74             ustrings[stringIndex].setTo(stringLengths[stringIndex] < 0, strings[stringIndex], stringLengths[stringIndex]);
75         }
76     }
77     UnicodeString res;
78     if (result != NULL) {
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);
82     }
83     ((const ListFormatter*)listfmt)->format( ustrings, stringCount, res, *status );
84     if (ustrings != ustringsStackBuf) {
85         delete[] ustrings;
86     }
87     return res.extract(result, resultCapacity, *status);
88 }
89 
90 
91 #endif /* #if !UCONFIG_NO_FORMATTING */
92