• 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) 2010-2012, 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/upluralrules.h"
15 #include "unicode/plurrule.h"
16 #include "unicode/locid.h"
17 #include "unicode/unistr.h"
18 #include "unicode/unum.h"
19 #include "unicode/numfmt.h"
20 
21 U_NAMESPACE_USE
22 
23 
24 U_CAPI UPluralRules* U_EXPORT2
uplrules_open(const char * locale,UErrorCode * status)25 uplrules_open(const char *locale, UErrorCode *status)
26 {
27     return uplrules_openForType(locale, UPLURAL_TYPE_CARDINAL, status);
28 }
29 
30 U_CAPI UPluralRules* U_EXPORT2
uplrules_openForType(const char * locale,UPluralType type,UErrorCode * status)31 uplrules_openForType(const char *locale, UPluralType type, UErrorCode *status)
32 {
33     return (UPluralRules*)PluralRules::forLocale(Locale(locale), type, *status);
34 }
35 
36 U_CAPI void U_EXPORT2
uplrules_close(UPluralRules * uplrules)37 uplrules_close(UPluralRules *uplrules)
38 {
39     delete (PluralRules*)uplrules;
40 }
41 
42 U_CAPI int32_t U_EXPORT2
uplrules_select(const UPluralRules * uplrules,double number,UChar * keyword,int32_t capacity,UErrorCode * status)43 uplrules_select(const UPluralRules *uplrules,
44                 double number,
45                 UChar *keyword, int32_t capacity,
46                 UErrorCode *status)
47 {
48     if (U_FAILURE(*status)) {
49         return 0;
50     }
51     if (keyword == NULL ? capacity != 0 : capacity < 0) {
52         *status = U_ILLEGAL_ARGUMENT_ERROR;
53         return 0;
54     }
55     UnicodeString result = ((PluralRules*)uplrules)->select(number);
56     return result.extract(keyword, capacity, *status);
57 }
58 
59 U_CAPI int32_t U_EXPORT2
uplrules_selectWithFormat(const UPluralRules * uplrules,double number,const UNumberFormat * fmt,UChar * keyword,int32_t capacity,UErrorCode * status)60 uplrules_selectWithFormat(const UPluralRules *uplrules,
61                           double number,
62                           const UNumberFormat *fmt,
63                           UChar *keyword, int32_t capacity,
64                           UErrorCode *status)
65 {
66     if (U_FAILURE(*status)) {
67         return 0;
68     }
69     const PluralRules* plrules = reinterpret_cast<const PluralRules*>(uplrules);
70     const NumberFormat* nf = reinterpret_cast<const NumberFormat*>(fmt);
71     if (plrules == NULL || nf == NULL || ((keyword == NULL)? capacity != 0 : capacity < 0)) {
72         *status = U_ILLEGAL_ARGUMENT_ERROR;
73         return 0;
74     }
75     Formattable obj(number);
76     UnicodeString result = plrules->select(obj, *nf, *status);
77     return result.extract(keyword, capacity, *status);
78 }
79 
80 U_CAPI UEnumeration* U_EXPORT2
uplrules_getKeywords(const UPluralRules * uplrules,UErrorCode * status)81 uplrules_getKeywords(const UPluralRules *uplrules,
82                      UErrorCode *status)
83 {
84     if (U_FAILURE(*status)) {
85         return NULL;
86     }
87     const PluralRules* plrules = reinterpret_cast<const PluralRules*>(uplrules);
88     if (plrules == NULL) {
89         *status = U_ILLEGAL_ARGUMENT_ERROR;
90         return NULL;
91     }
92     StringEnumeration *senum = plrules->getKeywords(*status);
93     if (U_FAILURE(*status)) {
94         return NULL;
95     }
96     if (senum == NULL) {
97         *status = U_MEMORY_ALLOCATION_ERROR;
98         return NULL;
99     }
100     return uenum_openFromStringEnumeration(senum, status);
101 }
102 
103 #endif /* #if !UCONFIG_NO_FORMATTING */
104