1 // Copyright (C) 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
19 U_NAMESPACE_USE
20
21
22 U_CAPI UPluralRules* U_EXPORT2
uplrules_open(const char * locale,UErrorCode * status)23 uplrules_open(const char *locale, UErrorCode *status)
24 {
25 return uplrules_openForType(locale, UPLURAL_TYPE_CARDINAL, status);
26 }
27
28 U_CAPI UPluralRules* U_EXPORT2
uplrules_openForType(const char * locale,UPluralType type,UErrorCode * status)29 uplrules_openForType(const char *locale, UPluralType type, UErrorCode *status)
30 {
31 return (UPluralRules*)PluralRules::forLocale(Locale(locale), type, *status);
32 }
33
34 U_CAPI void U_EXPORT2
uplrules_close(UPluralRules * uplrules)35 uplrules_close(UPluralRules *uplrules)
36 {
37 delete (PluralRules*)uplrules;
38 }
39
40 U_CAPI int32_t U_EXPORT2
uplrules_select(const UPluralRules * uplrules,double number,UChar * keyword,int32_t capacity,UErrorCode * status)41 uplrules_select(const UPluralRules *uplrules,
42 double number,
43 UChar *keyword, int32_t capacity,
44 UErrorCode *status)
45 {
46 if (U_FAILURE(*status)) {
47 return 0;
48 }
49 if (keyword == NULL ? capacity != 0 : capacity < 0) {
50 *status = U_ILLEGAL_ARGUMENT_ERROR;
51 return 0;
52 }
53 UnicodeString result = ((PluralRules*)uplrules)->select(number);
54 return result.extract(keyword, capacity, *status);
55 }
56
57
58 #endif /* #if !UCONFIG_NO_FORMATTING */
59