ucat.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. /* Separator between set_num and msg_num */
  18. static const char SEPARATOR = '%';
  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. * Fill in buffer with a set_num/msg_num key string, given the numeric
  24. * values. Numeric values must be >= 0. Buffer must be of length
  25. * MAX_KEY_LEN or more.
  26. */
  27. static char*
  28. _catkey(char* buffer, int32_t set_num, int32_t msg_num) {
  29. int32_t i = 0;
  30. i = T_CString_integerToString(buffer, set_num, 10);
  31. buffer[i++] = SEPARATOR;
  32. T_CString_integerToString(buffer+i, msg_num, 10);
  33. return buffer;
  34. }
  35. U_CAPI u_nl_catd U_EXPORT2
  36. u_catopen(const char* name, const char* locale, UErrorCode* ec) {
  37. return (u_nl_catd) ures_open(name, locale, ec);
  38. }
  39. U_CAPI void U_EXPORT2
  40. u_catclose(u_nl_catd catd) {
  41. ures_close((UResourceBundle*) catd); /* may be nullptr */
  42. }
  43. U_CAPI const char16_t* U_EXPORT2
  44. u_catgets(u_nl_catd catd, int32_t set_num, int32_t msg_num,
  45. const char16_t* s,
  46. int32_t* len, UErrorCode* ec) {
  47. char key[MAX_KEY_LEN];
  48. const char16_t* result;
  49. if (ec == nullptr || U_FAILURE(*ec)) {
  50. goto ERROR;
  51. }
  52. result = ures_getStringByKey((const UResourceBundle*) catd,
  53. _catkey(key, set_num, msg_num),
  54. len, ec);
  55. if (U_FAILURE(*ec)) {
  56. goto ERROR;
  57. }
  58. return result;
  59. ERROR:
  60. /* In case of any failure, return s */
  61. if (len != nullptr) {
  62. *len = u_strlen(s);
  63. }
  64. return s;
  65. }
  66. /*eof*/