Home | History | Annotate | Download | only in common
      1 /*
      2 *****************************************************************************************
      3 * Copyright (C) 2015, International Business Machines
      4 * Corporation and others. All Rights Reserved.
      5 *****************************************************************************************
      6 */
      7 
      8 #include "unicode/utypes.h"
      9 
     10 #if !UCONFIG_NO_FORMATTING
     11 
     12 #include "unicode/ulistformatter.h"
     13 #include "unicode/listformatter.h"
     14 #include "unicode/localpointer.h"
     15 #include "cmemory.h"
     16 
     17 U_NAMESPACE_USE
     18 
     19 U_CAPI UListFormatter* U_EXPORT2
     20 ulistfmt_open(const char*  locale,
     21               UErrorCode*  status)
     22 {
     23     if (U_FAILURE(*status)) {
     24         return NULL;
     25     }
     26     LocalPointer<ListFormatter> listfmt(ListFormatter::createInstance(Locale(locale), *status));
     27     if (U_FAILURE(*status)) {
     28         return NULL;
     29     }
     30     return (UListFormatter*)listfmt.orphan();
     31 }
     32 
     33 
     34 U_CAPI void U_EXPORT2
     35 ulistfmt_close(UListFormatter *listfmt)
     36 {
     37     delete (ListFormatter*)listfmt;
     38 }
     39 
     40 
     41 U_CAPI int32_t U_EXPORT2
     42 ulistfmt_format(const UListFormatter* listfmt,
     43                 const UChar* const strings[],
     44                 const int32_t *    stringLengths,
     45                 int32_t            stringCount,
     46                 UChar*             result,
     47                 int32_t            resultCapacity,
     48                 UErrorCode*        status)
     49 {
     50     if (U_FAILURE(*status)) {
     51         return -1;
     52     }
     53     if (stringCount < 0 || (strings == NULL && stringCount > 0) || ((result == NULL)? resultCapacity != 0 : resultCapacity < 0)) {
     54         *status = U_ILLEGAL_ARGUMENT_ERROR;
     55         return -1;
     56     }
     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;
     63             return -1;
     64         }
     65     }
     66     if (stringLengths == NULL) {
     67         for (int32_t stringIndex = 0; stringIndex < stringCount; stringIndex++) {
     68             ustrings[stringIndex].setTo(TRUE, strings[stringIndex], -1);
     69         }
     70     } else {
     71         for (int32_t stringIndex = 0; stringIndex < stringCount; stringIndex++) {
     72             ustrings[stringIndex].setTo(stringLengths[stringIndex] < 0, strings[stringIndex], stringLengths[stringIndex]);
     73         }
     74     }
     75     UnicodeString res;
     76     if (result != NULL) {
     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);
     80     }
     81     ((const ListFormatter*)listfmt)->format( ustrings, stringCount, res, *status );
     82     if (ustrings != ustringsStackBuf) {
     83         delete[] ustrings;
     84     }
     85     return res.extract(result, resultCapacity, *status);
     86 }
     87 
     88 
     89 #endif /* #if !UCONFIG_NO_FORMATTING */
     90