ures_cnv.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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) 1997-2006, International Business Machines
  7. * Corporation and others. All Rights Reserved.
  8. *
  9. *******************************************************************************
  10. * file name: ures_cnv.c
  11. * encoding: UTF-8
  12. * tab size: 8 (not used)
  13. * indentation:4
  14. *
  15. * created on: 2004aug25
  16. * created by: Markus W. Scherer
  17. *
  18. * Character conversion functions moved here from uresbund.c
  19. */
  20. #include "unicode/utypes.h"
  21. #include "unicode/putil.h"
  22. #include "unicode/ustring.h"
  23. #include "unicode/ucnv.h"
  24. #include "unicode/ures.h"
  25. #include "uinvchar.h"
  26. #include "ustr_cnv.h"
  27. U_CAPI UResourceBundle * U_EXPORT2
  28. ures_openU(const char16_t *myPath,
  29. const char *localeID,
  30. UErrorCode *status)
  31. {
  32. char pathBuffer[1024];
  33. int32_t length;
  34. char *path = pathBuffer;
  35. if(status==nullptr || U_FAILURE(*status)) {
  36. return nullptr;
  37. }
  38. if(myPath==nullptr) {
  39. path = nullptr;
  40. }
  41. else {
  42. length=u_strlen(myPath);
  43. if(length>=(int32_t)sizeof(pathBuffer)) {
  44. *status=U_ILLEGAL_ARGUMENT_ERROR;
  45. return nullptr;
  46. } else if(uprv_isInvariantUString(myPath, length)) {
  47. /*
  48. * the invariant converter is sufficient for package and tree names
  49. * and is more efficient
  50. */
  51. u_UCharsToChars(myPath, path, length+1); /* length+1 to include the NUL */
  52. } else {
  53. #if !UCONFIG_NO_CONVERSION
  54. /* use the default converter to support variant-character paths */
  55. UConverter *cnv=u_getDefaultConverter(status);
  56. length=ucnv_fromUChars(cnv, path, (int32_t)sizeof(pathBuffer), myPath, length, status);
  57. u_releaseDefaultConverter(cnv);
  58. if(U_FAILURE(*status)) {
  59. return nullptr;
  60. }
  61. if(length>=(int32_t)sizeof(pathBuffer)) {
  62. /* not NUL-terminated - path too long */
  63. *status=U_ILLEGAL_ARGUMENT_ERROR;
  64. return nullptr;
  65. }
  66. #else
  67. /* the default converter is not available */
  68. *status=U_UNSUPPORTED_ERROR;
  69. return nullptr;
  70. #endif
  71. }
  72. }
  73. return ures_open(path, localeID, status);
  74. }