11cb0ef41Sopenharmony_ci// © 2016 and later: Unicode, Inc. and others.
21cb0ef41Sopenharmony_ci// License & terms of use: http://www.unicode.org/copyright.html
31cb0ef41Sopenharmony_ci/*
41cb0ef41Sopenharmony_ci**********************************************************************
51cb0ef41Sopenharmony_ci* Copyright (c) 2003, International Business Machines
61cb0ef41Sopenharmony_ci* Corporation and others.  All Rights Reserved.
71cb0ef41Sopenharmony_ci**********************************************************************
81cb0ef41Sopenharmony_ci* Author: Alan Liu
91cb0ef41Sopenharmony_ci* Created: March 19 2003
101cb0ef41Sopenharmony_ci* Since: ICU 2.6
111cb0ef41Sopenharmony_ci**********************************************************************
121cb0ef41Sopenharmony_ci*/
131cb0ef41Sopenharmony_ci#include "unicode/ucat.h"
141cb0ef41Sopenharmony_ci#include "unicode/ustring.h"
151cb0ef41Sopenharmony_ci#include "cstring.h"
161cb0ef41Sopenharmony_ci#include "uassert.h"
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci/* Separator between set_num and msg_num */
191cb0ef41Sopenharmony_cistatic const char SEPARATOR = '%';
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ci/* Maximum length of a set_num/msg_num key, incl. terminating zero.
221cb0ef41Sopenharmony_ci * Longest possible key is "-2147483648%-2147483648" */
231cb0ef41Sopenharmony_ci#define MAX_KEY_LEN (24)
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci/**
261cb0ef41Sopenharmony_ci * Fill in buffer with a set_num/msg_num key string, given the numeric
271cb0ef41Sopenharmony_ci * values. Numeric values must be >= 0. Buffer must be of length
281cb0ef41Sopenharmony_ci * MAX_KEY_LEN or more.
291cb0ef41Sopenharmony_ci */
301cb0ef41Sopenharmony_cistatic char*
311cb0ef41Sopenharmony_ci_catkey(char* buffer, int32_t set_num, int32_t msg_num) {
321cb0ef41Sopenharmony_ci    int32_t i = 0;
331cb0ef41Sopenharmony_ci    i = T_CString_integerToString(buffer, set_num, 10);
341cb0ef41Sopenharmony_ci    buffer[i++] = SEPARATOR;
351cb0ef41Sopenharmony_ci    T_CString_integerToString(buffer+i, msg_num, 10);
361cb0ef41Sopenharmony_ci    return buffer;
371cb0ef41Sopenharmony_ci}
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ciU_CAPI u_nl_catd U_EXPORT2
401cb0ef41Sopenharmony_ciu_catopen(const char* name, const char* locale, UErrorCode* ec) {
411cb0ef41Sopenharmony_ci    return (u_nl_catd) ures_open(name, locale, ec);
421cb0ef41Sopenharmony_ci}
431cb0ef41Sopenharmony_ci
441cb0ef41Sopenharmony_ciU_CAPI void U_EXPORT2
451cb0ef41Sopenharmony_ciu_catclose(u_nl_catd catd) {
461cb0ef41Sopenharmony_ci    ures_close((UResourceBundle*) catd); /* may be nullptr */
471cb0ef41Sopenharmony_ci}
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ciU_CAPI const char16_t* U_EXPORT2
501cb0ef41Sopenharmony_ciu_catgets(u_nl_catd catd, int32_t set_num, int32_t msg_num,
511cb0ef41Sopenharmony_ci          const char16_t* s,
521cb0ef41Sopenharmony_ci          int32_t* len, UErrorCode* ec) {
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci    char key[MAX_KEY_LEN];
551cb0ef41Sopenharmony_ci    const char16_t* result;
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci    if (ec == nullptr || U_FAILURE(*ec)) {
581cb0ef41Sopenharmony_ci        goto ERROR;
591cb0ef41Sopenharmony_ci    }
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ci    result = ures_getStringByKey((const UResourceBundle*) catd,
621cb0ef41Sopenharmony_ci                                 _catkey(key, set_num, msg_num),
631cb0ef41Sopenharmony_ci                                 len, ec);
641cb0ef41Sopenharmony_ci    if (U_FAILURE(*ec)) {
651cb0ef41Sopenharmony_ci        goto ERROR;
661cb0ef41Sopenharmony_ci    }
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci    return result;
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ci ERROR:
711cb0ef41Sopenharmony_ci    /* In case of any failure, return s */
721cb0ef41Sopenharmony_ci    if (len != nullptr) {
731cb0ef41Sopenharmony_ci        *len = u_strlen(s);
741cb0ef41Sopenharmony_ci    }
751cb0ef41Sopenharmony_ci    return s;
761cb0ef41Sopenharmony_ci}
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ci/*eof*/
79