| 1 | // © 2016 and later: Unicode, Inc. and others. |
| 2 | // License & terms of use: http://www.unicode.org/copyright.html |
| 3 | /* |
| 4 | ********************************************************************** |
| 5 | * Copyright (c) 2004-2014, International Business Machines |
| 6 | * Corporation and others. All Rights Reserved. |
| 7 | ********************************************************************** |
| 8 | * Author: Alan Liu |
| 9 | * Created: January 16 2004 |
| 10 | * Since: ICU 2.8 |
| 11 | ********************************************************************** |
| 12 | */ |
| 13 | #include "locbased.h" |
| 14 | #include "cstring.h" |
| 15 | #include "charstr.h" |
| 16 | |
| 17 | U_NAMESPACE_BEGIN |
| 18 | |
| 19 | Locale LocaleBased::getLocale(const CharString* valid, const CharString* actual, |
| 20 | ULocDataLocaleType type, UErrorCode& status) { |
| 21 | const char* id = getLocaleID(valid, actual, type, status); |
| 22 | return Locale(id != nullptr ? id : ""); |
| 23 | } |
| 24 | |
| 25 | const char* LocaleBased::getLocaleID(const CharString* valid, const CharString* actual, |
| 26 | ULocDataLocaleType type, UErrorCode& status) { |
| 27 | if (U_FAILURE(status)) { |
| 28 | return nullptr; |
| 29 | } |
| 30 | |
| 31 | switch(type) { |
| 32 | case ULOC_VALID_LOCALE: |
| 33 | return valid == nullptr ? "" : valid->data(); |
| 34 | case ULOC_ACTUAL_LOCALE: |
| 35 | return actual == nullptr ? "" : actual->data(); |
| 36 | default: |
| 37 | status = U_ILLEGAL_ARGUMENT_ERROR; |
| 38 | return nullptr; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | void LocaleBased::setLocaleIDs(const CharString* validID, const CharString* actualID, UErrorCode& status) { |
| 43 | setValidLocaleID(validID, status); |
| 44 | setActualLocaleID(actualID,status); |
| 45 | } |
| 46 | void LocaleBased::setLocaleIDs(const char* validID, const char* actualID, UErrorCode& status) { |
| 47 | setValidLocaleID(validID, status); |
| 48 | setActualLocaleID(actualID,status); |
| 49 | } |
| 50 | |
| 51 | void LocaleBased::setLocaleID(const char* id, CharString*& dest, UErrorCode& status) { |
| 52 | if (U_FAILURE(status)) { return; } |
| 53 | if (id == nullptr || *id == 0) { |
| 54 | delete dest; |
| 55 | dest = nullptr; |
| 56 | } else { |
| 57 | if (dest == nullptr) { |
| 58 | dest = new CharString(id, status); |
| 59 | if (dest == nullptr) { |
| 60 | status = U_MEMORY_ALLOCATION_ERROR; |
| 61 | return; |
| 62 | } |
| 63 | } else { |
| 64 | dest->copyFrom(id, status); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | void LocaleBased::setLocaleID(const CharString* id, CharString*& dest, UErrorCode& status) { |
| 70 | if (U_FAILURE(status)) { return; } |
| 71 | if (id == nullptr || id->isEmpty()) { |
| 72 | delete dest; |
| 73 | dest = nullptr; |
| 74 | } else { |
| 75 | if (dest == nullptr) { |
| 76 | dest = new CharString(*id, status); |
| 77 | if (dest == nullptr) { |
| 78 | status = U_MEMORY_ALLOCATION_ERROR; |
| 79 | return; |
| 80 | } |
| 81 | } else { |
| 82 | dest->copyFrom(*id, status); |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | bool LocaleBased::equalIDs(const CharString* left, const CharString* right) { |
| 88 | // true if both are nullptr |
| 89 | if (left == nullptr && right == nullptr) return true; |
| 90 | // false if only one is nullptr |
| 91 | if (left == nullptr || right == nullptr) return false; |
| 92 | return *left == *right; |
| 93 | } |
| 94 | |
| 95 | U_NAMESPACE_END |