Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef AAPT_LOCALE_VALUE_H |
| 18 | #define AAPT_LOCALE_VALUE_H |
| 19 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 20 | #include <string> |
| 21 | #include <vector> |
| 22 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 23 | #include "androidfw/ResourceTypes.h" |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 24 | #include "androidfw/StringPiece.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 25 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 26 | namespace aapt { |
| 27 | |
| 28 | /** |
| 29 | * A convenience class to build and parse locales. |
| 30 | */ |
| 31 | struct LocaleValue { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 32 | char language[4]; |
| 33 | char region[4]; |
| 34 | char script[4]; |
| 35 | char variant[8]; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 36 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 37 | inline LocaleValue(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 38 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 39 | /** |
| 40 | * Initialize this LocaleValue from a config string. |
| 41 | */ |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 42 | bool InitFromFilterString(const android::StringPiece& config); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 43 | |
Adam Lesinski | b58c3ef | 2017-09-12 17:39:52 -0700 | [diff] [blame^] | 44 | // Initializes this LocaleValue from a BCP-47 locale tag. |
| 45 | bool InitFromBcp47Tag(const android::StringPiece& bcp47tag); |
| 46 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 47 | /** |
| 48 | * Initialize this LocaleValue from parts of a vector. |
| 49 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 50 | ssize_t InitFromParts(std::vector<std::string>::iterator iter, |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 51 | std::vector<std::string>::iterator end); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 52 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 53 | /** |
| 54 | * Initialize this LocaleValue from a ResTable_config. |
| 55 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 56 | void InitFromResTable(const android::ResTable_config& config); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 57 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 58 | /** |
| 59 | * Set the locale in a ResTable_config from this LocaleValue. |
| 60 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 61 | void WriteTo(android::ResTable_config* out) const; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 62 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 63 | inline int compare(const LocaleValue& other) const; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 64 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 65 | inline bool operator<(const LocaleValue& o) const; |
| 66 | inline bool operator<=(const LocaleValue& o) const; |
| 67 | inline bool operator==(const LocaleValue& o) const; |
| 68 | inline bool operator!=(const LocaleValue& o) const; |
| 69 | inline bool operator>=(const LocaleValue& o) const; |
| 70 | inline bool operator>(const LocaleValue& o) const; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 71 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 72 | private: |
Adam Lesinski | b58c3ef | 2017-09-12 17:39:52 -0700 | [diff] [blame^] | 73 | bool InitFromBcp47TagImpl(const android::StringPiece& bcp47tag, const char separator); |
| 74 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 75 | void set_language(const char* language); |
| 76 | void set_region(const char* language); |
| 77 | void set_script(const char* script); |
| 78 | void set_variant(const char* variant); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | // |
| 82 | // Implementation |
| 83 | // |
| 84 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 85 | LocaleValue::LocaleValue() { memset(this, 0, sizeof(LocaleValue)); } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 86 | |
| 87 | int LocaleValue::compare(const LocaleValue& other) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 88 | return memcmp(this, &other, sizeof(LocaleValue)); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | bool LocaleValue::operator<(const LocaleValue& o) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 92 | return compare(o) < 0; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | bool LocaleValue::operator<=(const LocaleValue& o) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 96 | return compare(o) <= 0; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | bool LocaleValue::operator==(const LocaleValue& o) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 100 | return compare(o) == 0; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | bool LocaleValue::operator!=(const LocaleValue& o) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 104 | return compare(o) != 0; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | bool LocaleValue::operator>=(const LocaleValue& o) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 108 | return compare(o) >= 0; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | bool LocaleValue::operator>(const LocaleValue& o) const { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 112 | return compare(o) > 0; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 113 | } |
| 114 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 115 | } // namespace aapt |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 116 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 117 | #endif // AAPT_LOCALE_VALUE_H |