intl/icu/source/common/ucat.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/intl/icu/source/common/ucat.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,76 @@
     1.4 +/*
     1.5 +**********************************************************************
     1.6 +* Copyright (c) 2003, International Business Machines
     1.7 +* Corporation and others.  All Rights Reserved.
     1.8 +**********************************************************************
     1.9 +* Author: Alan Liu
    1.10 +* Created: March 19 2003
    1.11 +* Since: ICU 2.6
    1.12 +**********************************************************************
    1.13 +*/
    1.14 +#include "unicode/ucat.h"
    1.15 +#include "unicode/ustring.h"
    1.16 +#include "cstring.h"
    1.17 +#include "uassert.h"
    1.18 +
    1.19 +/* Separator between set_num and msg_num */
    1.20 +static const char SEPARATOR = '%';
    1.21 +
    1.22 +/* Maximum length of a set_num/msg_num key, incl. terminating zero.
    1.23 + * Longest possible key is "-2147483648%-2147483648" */
    1.24 +#define MAX_KEY_LEN (24)
    1.25 +
    1.26 +/**
    1.27 + * Fill in buffer with a set_num/msg_num key string, given the numeric
    1.28 + * values. Numeric values must be >= 0. Buffer must be of length
    1.29 + * MAX_KEY_LEN or more.
    1.30 + */
    1.31 +static char*
    1.32 +_catkey(char* buffer, int32_t set_num, int32_t msg_num) {
    1.33 +    int32_t i = 0;
    1.34 +    i = T_CString_integerToString(buffer, set_num, 10);
    1.35 +    buffer[i++] = SEPARATOR;
    1.36 +    T_CString_integerToString(buffer+i, msg_num, 10);
    1.37 +    return buffer;
    1.38 +}
    1.39 +
    1.40 +U_CAPI u_nl_catd U_EXPORT2
    1.41 +u_catopen(const char* name, const char* locale, UErrorCode* ec) {
    1.42 +    return (u_nl_catd) ures_open(name, locale, ec);
    1.43 +}
    1.44 +
    1.45 +U_CAPI void U_EXPORT2
    1.46 +u_catclose(u_nl_catd catd) {
    1.47 +    ures_close((UResourceBundle*) catd); /* may be NULL */
    1.48 +}
    1.49 +
    1.50 +U_CAPI const UChar* U_EXPORT2
    1.51 +u_catgets(u_nl_catd catd, int32_t set_num, int32_t msg_num,
    1.52 +          const UChar* s,
    1.53 +          int32_t* len, UErrorCode* ec) {
    1.54 +
    1.55 +    char key[MAX_KEY_LEN];
    1.56 +    const UChar* result;
    1.57 +
    1.58 +    if (ec == NULL || U_FAILURE(*ec)) {
    1.59 +        goto ERROR;
    1.60 +    }
    1.61 +
    1.62 +    result = ures_getStringByKey((const UResourceBundle*) catd,
    1.63 +                                 _catkey(key, set_num, msg_num),
    1.64 +                                 len, ec);
    1.65 +    if (U_FAILURE(*ec)) {
    1.66 +        goto ERROR;
    1.67 +    }
    1.68 +
    1.69 +    return result;
    1.70 +
    1.71 + ERROR:
    1.72 +    /* In case of any failure, return s */
    1.73 +    if (len != NULL) {
    1.74 +        *len = u_strlen(s);
    1.75 +    }
    1.76 +    return s;
    1.77 +}
    1.78 +
    1.79 +/*eof*/

mercurial