ucnvdisp.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. ******************************************************************************
  5. *
  6. * Copyright (C) 1998-2004, International Business Machines
  7. * Corporation and others. All Rights Reserved.
  8. *
  9. ******************************************************************************
  10. *
  11. * ucnvdisp.c:
  12. * Implements APIs for the ICU's codeset conversion library display names.
  13. *
  14. * Modification History:
  15. *
  16. * Date Name Description
  17. * 04/04/99 helena Fixed internal header inclusion.
  18. * 05/09/00 helena Added implementation to handle fallback mappings.
  19. * 06/20/2000 helena OS/400 port changes; mostly typecast.
  20. * 09/08/2004 grhoten split from ucnv.c
  21. */
  22. #include "unicode/utypes.h"
  23. #if !UCONFIG_NO_CONVERSION
  24. #include "unicode/ustring.h"
  25. #include "unicode/ures.h"
  26. #include "unicode/ucnv.h"
  27. #include "cstring.h"
  28. #include "ustr_imp.h"
  29. #include "ucnv_imp.h"
  30. #include "putilimp.h"
  31. U_CAPI int32_t U_EXPORT2
  32. ucnv_getDisplayName(const UConverter *cnv,
  33. const char *displayLocale,
  34. char16_t *displayName, int32_t displayNameCapacity,
  35. UErrorCode *pErrorCode) {
  36. UResourceBundle *rb;
  37. const char16_t *name;
  38. int32_t length;
  39. UErrorCode localStatus = U_ZERO_ERROR;
  40. /* check arguments */
  41. if(pErrorCode==nullptr || U_FAILURE(*pErrorCode)) {
  42. return 0;
  43. }
  44. if(cnv==nullptr || displayNameCapacity<0 || (displayNameCapacity>0 && displayName==nullptr)) {
  45. *pErrorCode=U_ILLEGAL_ARGUMENT_ERROR;
  46. return 0;
  47. }
  48. /* open the resource bundle and get the display name string */
  49. rb=ures_open(nullptr, displayLocale, pErrorCode);
  50. if(U_FAILURE(*pErrorCode)) {
  51. return 0;
  52. }
  53. /* use the internal name as the key */
  54. name=ures_getStringByKey(rb, cnv->sharedData->staticData->name, &length, &localStatus);
  55. ures_close(rb);
  56. if(U_SUCCESS(localStatus)) {
  57. /* copy the string */
  58. if (*pErrorCode == U_ZERO_ERROR) {
  59. *pErrorCode = localStatus;
  60. }
  61. u_memcpy(displayName, name, uprv_min(length, displayNameCapacity)*U_SIZEOF_UCHAR);
  62. } else {
  63. /* convert the internal name into a Unicode string */
  64. length=(int32_t)uprv_strlen(cnv->sharedData->staticData->name);
  65. u_charsToUChars(cnv->sharedData->staticData->name, displayName, uprv_min(length, displayNameCapacity));
  66. }
  67. return u_terminateUChars(displayName, displayNameCapacity, length, pErrorCode);
  68. }
  69. #endif
  70. /*
  71. * Hey, Emacs, please set the following:
  72. *
  73. * Local Variables:
  74. * indent-tabs-mode: nil
  75. * End:
  76. *
  77. */