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