]>
Commit | Line | Data |
---|---|---|
f3c0d7a5 A |
1 | // © 2016 and later: Unicode, Inc. and others. |
2 | // License & terms of use: http://www.unicode.org/copyright.html | |
4388f060 A |
3 | /* |
4 | ***************************************************************************************** | |
51004dcb | 5 | * Copyright (C) 2010-2012, International Business Machines |
4388f060 A |
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" | |
f3c0d7a5 A |
18 | #include "unicode/unum.h" |
19 | #include "unicode/numfmt.h" | |
3d1f044b A |
20 | #include "unicode/unumberformatter.h" |
21 | #include "number_decimalquantity.h" | |
22 | #include "number_utypes.h" | |
4388f060 A |
23 | |
24 | U_NAMESPACE_USE | |
25 | ||
3d1f044b A |
26 | namespace { |
27 | ||
28 | /** | |
29 | * Given a number and a format, returns the keyword of the first applicable | |
30 | * rule for the PluralRules object. | |
31 | * @param rules The plural rules. | |
32 | * @param obj The numeric object for which the rule should be determined. | |
33 | * @param fmt The NumberFormat specifying how the number will be formatted | |
34 | * (this can affect the plural form, e.g. "1 dollar" vs "1.0 dollars"). | |
35 | * @param status Input/output parameter. If at entry this indicates a | |
36 | * failure status, the method returns immediately; otherwise | |
37 | * this is set to indicate the outcome of the call. | |
38 | * @return The keyword of the selected rule. Undefined in the case of an error. | |
39 | */ | |
40 | UnicodeString select(const PluralRules &rules, const Formattable& obj, const NumberFormat& fmt, UErrorCode& status) { | |
41 | if (U_SUCCESS(status)) { | |
42 | const DecimalFormat *decFmt = dynamic_cast<const DecimalFormat *>(&fmt); | |
43 | if (decFmt != NULL) { | |
44 | number::impl::DecimalQuantity dq; | |
45 | decFmt->formatToDecimalQuantity(obj, dq, status); | |
46 | if (U_SUCCESS(status)) { | |
47 | return rules.select(dq); | |
48 | } | |
49 | } else { | |
50 | double number = obj.getDouble(status); | |
51 | if (U_SUCCESS(status)) { | |
52 | return rules.select(number); | |
53 | } | |
54 | } | |
55 | } | |
56 | return UnicodeString(); | |
57 | } | |
58 | ||
59 | } // namespace | |
4388f060 A |
60 | |
61 | U_CAPI UPluralRules* U_EXPORT2 | |
51004dcb | 62 | uplrules_open(const char *locale, UErrorCode *status) |
4388f060 | 63 | { |
51004dcb A |
64 | return uplrules_openForType(locale, UPLURAL_TYPE_CARDINAL, status); |
65 | } | |
66 | ||
67 | U_CAPI UPluralRules* U_EXPORT2 | |
68 | uplrules_openForType(const char *locale, UPluralType type, UErrorCode *status) | |
69 | { | |
70 | return (UPluralRules*)PluralRules::forLocale(Locale(locale), type, *status); | |
4388f060 A |
71 | } |
72 | ||
73 | U_CAPI void U_EXPORT2 | |
74 | uplrules_close(UPluralRules *uplrules) | |
75 | { | |
76 | delete (PluralRules*)uplrules; | |
77 | } | |
78 | ||
79 | U_CAPI int32_t U_EXPORT2 | |
80 | uplrules_select(const UPluralRules *uplrules, | |
81 | double number, | |
82 | UChar *keyword, int32_t capacity, | |
83 | UErrorCode *status) | |
84 | { | |
85 | if (U_FAILURE(*status)) { | |
86 | return 0; | |
87 | } | |
88 | if (keyword == NULL ? capacity != 0 : capacity < 0) { | |
89 | *status = U_ILLEGAL_ARGUMENT_ERROR; | |
90 | return 0; | |
91 | } | |
92 | UnicodeString result = ((PluralRules*)uplrules)->select(number); | |
93 | return result.extract(keyword, capacity, *status); | |
94 | } | |
95 | ||
3d1f044b A |
96 | U_CAPI int32_t U_EXPORT2 |
97 | uplrules_selectFormatted(const UPluralRules *uplrules, | |
98 | const UFormattedNumber* number, | |
99 | UChar *keyword, int32_t capacity, | |
100 | UErrorCode *status) | |
101 | { | |
102 | if (U_FAILURE(*status)) { | |
103 | return 0; | |
104 | } | |
105 | if (keyword == NULL ? capacity != 0 : capacity < 0) { | |
106 | *status = U_ILLEGAL_ARGUMENT_ERROR; | |
107 | return 0; | |
108 | } | |
109 | const number::impl::DecimalQuantity* dq = | |
110 | number::impl::validateUFormattedNumberToDecimalQuantity(number, *status); | |
111 | if (U_FAILURE(*status)) { | |
112 | return 0; | |
113 | } | |
114 | UnicodeString result = ((PluralRules*)uplrules)->select(*dq); | |
115 | return result.extract(keyword, capacity, *status); | |
116 | } | |
117 | ||
f3c0d7a5 A |
118 | U_CAPI int32_t U_EXPORT2 |
119 | uplrules_selectWithFormat(const UPluralRules *uplrules, | |
120 | double number, | |
121 | const UNumberFormat *fmt, | |
122 | UChar *keyword, int32_t capacity, | |
123 | UErrorCode *status) | |
124 | { | |
125 | if (U_FAILURE(*status)) { | |
126 | return 0; | |
127 | } | |
128 | const PluralRules* plrules = reinterpret_cast<const PluralRules*>(uplrules); | |
129 | const NumberFormat* nf = reinterpret_cast<const NumberFormat*>(fmt); | |
130 | if (plrules == NULL || nf == NULL || ((keyword == NULL)? capacity != 0 : capacity < 0)) { | |
131 | *status = U_ILLEGAL_ARGUMENT_ERROR; | |
132 | return 0; | |
133 | } | |
134 | Formattable obj(number); | |
3d1f044b | 135 | UnicodeString result = select(*plrules, obj, *nf, *status); |
f3c0d7a5 A |
136 | return result.extract(keyword, capacity, *status); |
137 | } | |
138 | ||
139 | U_CAPI UEnumeration* U_EXPORT2 | |
140 | uplrules_getKeywords(const UPluralRules *uplrules, | |
141 | UErrorCode *status) | |
142 | { | |
143 | if (U_FAILURE(*status)) { | |
144 | return NULL; | |
145 | } | |
146 | const PluralRules* plrules = reinterpret_cast<const PluralRules*>(uplrules); | |
147 | if (plrules == NULL) { | |
148 | *status = U_ILLEGAL_ARGUMENT_ERROR; | |
149 | return NULL; | |
150 | } | |
151 | StringEnumeration *senum = plrules->getKeywords(*status); | |
152 | if (U_FAILURE(*status)) { | |
153 | return NULL; | |
154 | } | |
155 | if (senum == NULL) { | |
156 | *status = U_MEMORY_ALLOCATION_ERROR; | |
157 | return NULL; | |
158 | } | |
159 | return uenum_openFromStringEnumeration(senum, status); | |
160 | } | |
4388f060 A |
161 | |
162 | #endif /* #if !UCONFIG_NO_FORMATTING */ |