ulocale.cpp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // © 2023 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. //
  4. #include "unicode/bytestream.h"
  5. #include "unicode/errorcode.h"
  6. #include "unicode/stringpiece.h"
  7. #include "unicode/utypes.h"
  8. #include "unicode/ustring.h"
  9. #include "unicode/ulocale.h"
  10. #include "unicode/locid.h"
  11. #include "bytesinkutil.h"
  12. #include "charstr.h"
  13. #include "cmemory.h"
  14. U_NAMESPACE_USE
  15. #define EXTERNAL(i) (reinterpret_cast<ULocale*>(i))
  16. #define CONST_INTERNAL(e) (reinterpret_cast<const icu::Locale*>(e))
  17. #define INTERNAL(e) (reinterpret_cast<icu::Locale*>(e))
  18. ULocale*
  19. ulocale_openForLocaleID(const char* localeID, int32_t length, UErrorCode* err) {
  20. if (U_FAILURE(*err)) { return nullptr; }
  21. CharString str(length < 0 ? StringPiece(localeID) : StringPiece(localeID, length), *err);
  22. if (U_FAILURE(*err)) { return nullptr; }
  23. return EXTERNAL(icu::Locale::createFromName(str.data()).clone());
  24. }
  25. ULocale*
  26. ulocale_openForLanguageTag(const char* tag, int32_t length, UErrorCode* err) {
  27. if (U_FAILURE(*err)) { return nullptr; }
  28. Locale l = icu::Locale::forLanguageTag(length < 0 ? StringPiece(tag) : StringPiece(tag, length), *err);
  29. if (U_FAILURE(*err)) { return nullptr; }
  30. return EXTERNAL(l.clone());
  31. }
  32. void
  33. ulocale_close(ULocale* locale) {
  34. delete INTERNAL(locale);
  35. }
  36. #define IMPL_ULOCALE_STRING_GETTER(N1, N2) \
  37. const char* ulocale_get ## N1(const ULocale* locale) { \
  38. if (locale == nullptr) return nullptr; \
  39. return CONST_INTERNAL(locale)->get ## N2(); \
  40. }
  41. #define IMPL_ULOCALE_STRING_IDENTICAL_GETTER(N) IMPL_ULOCALE_STRING_GETTER(N, N)
  42. #define IMPL_ULOCALE_GET_KEYWORD_VALUE(N) \
  43. int32_t ulocale_get ##N ( \
  44. const ULocale* locale, const char* keyword, int32_t keywordLength, \
  45. char* valueBuffer, int32_t bufferCapacity, UErrorCode *err) { \
  46. if (U_FAILURE(*err)) return 0; \
  47. if (locale == nullptr) { \
  48. *err = U_ILLEGAL_ARGUMENT_ERROR; \
  49. return 0; \
  50. } \
  51. return ByteSinkUtil::viaByteSinkToTerminatedChars( \
  52. valueBuffer, bufferCapacity, \
  53. [&](ByteSink& sink, UErrorCode& status) { \
  54. CONST_INTERNAL(locale)->get ## N( \
  55. keywordLength < 0 ? StringPiece(keyword) : StringPiece(keyword, keywordLength), \
  56. sink, status); \
  57. }, \
  58. *err); \
  59. }
  60. #define IMPL_ULOCALE_GET_KEYWORDS(N) \
  61. UEnumeration* ulocale_get ## N(const ULocale* locale, UErrorCode *err) { \
  62. if (U_FAILURE(*err)) return nullptr; \
  63. if (locale == nullptr) { \
  64. *err = U_ILLEGAL_ARGUMENT_ERROR; \
  65. return nullptr; \
  66. } \
  67. return uenum_openFromStringEnumeration( \
  68. CONST_INTERNAL(locale)->create ## N(*err), err); \
  69. }
  70. IMPL_ULOCALE_STRING_IDENTICAL_GETTER(Language)
  71. IMPL_ULOCALE_STRING_IDENTICAL_GETTER(Script)
  72. IMPL_ULOCALE_STRING_GETTER(Region, Country)
  73. IMPL_ULOCALE_STRING_IDENTICAL_GETTER(Variant)
  74. IMPL_ULOCALE_STRING_GETTER(LocaleID, Name)
  75. IMPL_ULOCALE_STRING_IDENTICAL_GETTER(BaseName)
  76. IMPL_ULOCALE_GET_KEYWORD_VALUE(KeywordValue)
  77. IMPL_ULOCALE_GET_KEYWORD_VALUE(UnicodeKeywordValue)
  78. IMPL_ULOCALE_GET_KEYWORDS(Keywords)
  79. IMPL_ULOCALE_GET_KEYWORDS(UnicodeKeywords)
  80. bool ulocale_isBogus(const ULocale* locale) {
  81. if (locale == nullptr) return false;
  82. return CONST_INTERNAL(locale)->isBogus();
  83. }
  84. /*eof*/