/src/mozilla-central/intl/icu/source/common/ucat.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | // © 2016 and later: Unicode, Inc. and others. |
2 | | // License & terms of use: http://www.unicode.org/copyright.html |
3 | | /* |
4 | | ********************************************************************** |
5 | | * Copyright (c) 2003, International Business Machines |
6 | | * Corporation and others. All Rights Reserved. |
7 | | ********************************************************************** |
8 | | * Author: Alan Liu |
9 | | * Created: March 19 2003 |
10 | | * Since: ICU 2.6 |
11 | | ********************************************************************** |
12 | | */ |
13 | | #include "unicode/ucat.h" |
14 | | #include "unicode/ustring.h" |
15 | | #include "cstring.h" |
16 | | #include "uassert.h" |
17 | | |
18 | | /* Separator between set_num and msg_num */ |
19 | | static const char SEPARATOR = '%'; |
20 | | |
21 | | /* Maximum length of a set_num/msg_num key, incl. terminating zero. |
22 | | * Longest possible key is "-2147483648%-2147483648" */ |
23 | | #define MAX_KEY_LEN (24) |
24 | | |
25 | | /** |
26 | | * Fill in buffer with a set_num/msg_num key string, given the numeric |
27 | | * values. Numeric values must be >= 0. Buffer must be of length |
28 | | * MAX_KEY_LEN or more. |
29 | | */ |
30 | | static char* |
31 | 0 | _catkey(char* buffer, int32_t set_num, int32_t msg_num) { |
32 | 0 | int32_t i = 0; |
33 | 0 | i = T_CString_integerToString(buffer, set_num, 10); |
34 | 0 | buffer[i++] = SEPARATOR; |
35 | 0 | T_CString_integerToString(buffer+i, msg_num, 10); |
36 | 0 | return buffer; |
37 | 0 | } |
38 | | |
39 | | U_CAPI u_nl_catd U_EXPORT2 |
40 | 0 | u_catopen(const char* name, const char* locale, UErrorCode* ec) { |
41 | 0 | return (u_nl_catd) ures_open(name, locale, ec); |
42 | 0 | } |
43 | | |
44 | | U_CAPI void U_EXPORT2 |
45 | 0 | u_catclose(u_nl_catd catd) { |
46 | 0 | ures_close((UResourceBundle*) catd); /* may be NULL */ |
47 | 0 | } |
48 | | |
49 | | U_CAPI const UChar* U_EXPORT2 |
50 | | u_catgets(u_nl_catd catd, int32_t set_num, int32_t msg_num, |
51 | | const UChar* s, |
52 | 0 | int32_t* len, UErrorCode* ec) { |
53 | 0 |
|
54 | 0 | char key[MAX_KEY_LEN]; |
55 | 0 | const UChar* result; |
56 | 0 |
|
57 | 0 | if (ec == NULL || U_FAILURE(*ec)) { |
58 | 0 | goto ERROR; |
59 | 0 | } |
60 | 0 | |
61 | 0 | result = ures_getStringByKey((const UResourceBundle*) catd, |
62 | 0 | _catkey(key, set_num, msg_num), |
63 | 0 | len, ec); |
64 | 0 | if (U_FAILURE(*ec)) { |
65 | 0 | goto ERROR; |
66 | 0 | } |
67 | 0 | |
68 | 0 | return result; |
69 | 0 | |
70 | 0 | ERROR: |
71 | 0 | /* In case of any failure, return s */ |
72 | 0 | if (len != NULL) { |
73 | 0 | *len = u_strlen(s); |
74 | 0 | } |
75 | 0 | return s; |
76 | 0 | } |
77 | | |
78 | | /*eof*/ |