blob: 6d8b598415ccbc76b8b9acd689c26990735b15db [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
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 Lesinski6f6ceb72014-11-14 14:48:12 -080020#include <string>
21#include <vector>
22
Adam Lesinskice5e56e2016-10-21 17:56:45 -070023#include "androidfw/ResourceTypes.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080024#include "androidfw/StringPiece.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070025
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080026namespace aapt {
27
28/**
29 * A convenience class to build and parse locales.
30 */
31struct LocaleValue {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070032 char language[4];
33 char region[4];
34 char script[4];
35 char variant[8];
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080036
Adam Lesinskicacb28f2016-10-19 12:18:14 -070037 inline LocaleValue();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080038
Adam Lesinskicacb28f2016-10-19 12:18:14 -070039 /**
40 * Initialize this LocaleValue from a config string.
41 */
Adam Lesinskid5083f62017-01-16 15:07:21 -080042 bool InitFromFilterString(const android::StringPiece& config);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080043
Adam Lesinskib58c3ef2017-09-12 17:39:52 -070044 // Initializes this LocaleValue from a BCP-47 locale tag.
45 bool InitFromBcp47Tag(const android::StringPiece& bcp47tag);
46
Adam Lesinskicacb28f2016-10-19 12:18:14 -070047 /**
48 * Initialize this LocaleValue from parts of a vector.
49 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070050 ssize_t InitFromParts(std::vector<std::string>::iterator iter,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051 std::vector<std::string>::iterator end);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080052
Adam Lesinskicacb28f2016-10-19 12:18:14 -070053 /**
54 * Initialize this LocaleValue from a ResTable_config.
55 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070056 void InitFromResTable(const android::ResTable_config& config);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080057
Adam Lesinskicacb28f2016-10-19 12:18:14 -070058 /**
59 * Set the locale in a ResTable_config from this LocaleValue.
60 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070061 void WriteTo(android::ResTable_config* out) const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080062
Adam Lesinskicacb28f2016-10-19 12:18:14 -070063 inline int compare(const LocaleValue& other) const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080064
Adam Lesinskicacb28f2016-10-19 12:18:14 -070065 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 Lesinski6f6ceb72014-11-14 14:48:12 -080071
Adam Lesinskicacb28f2016-10-19 12:18:14 -070072 private:
Adam Lesinskib58c3ef2017-09-12 17:39:52 -070073 bool InitFromBcp47TagImpl(const android::StringPiece& bcp47tag, const char separator);
74
Adam Lesinskice5e56e2016-10-21 17:56:45 -070075 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 Lesinski6f6ceb72014-11-14 14:48:12 -080079};
80
81//
82// Implementation
83//
84
Adam Lesinskicacb28f2016-10-19 12:18:14 -070085LocaleValue::LocaleValue() { memset(this, 0, sizeof(LocaleValue)); }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080086
87int LocaleValue::compare(const LocaleValue& other) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070088 return memcmp(this, &other, sizeof(LocaleValue));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080089}
90
91bool LocaleValue::operator<(const LocaleValue& o) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070092 return compare(o) < 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080093}
94
95bool LocaleValue::operator<=(const LocaleValue& o) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070096 return compare(o) <= 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080097}
98
99bool LocaleValue::operator==(const LocaleValue& o) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700100 return compare(o) == 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800101}
102
103bool LocaleValue::operator!=(const LocaleValue& o) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700104 return compare(o) != 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800105}
106
107bool LocaleValue::operator>=(const LocaleValue& o) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700108 return compare(o) >= 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800109}
110
111bool LocaleValue::operator>(const LocaleValue& o) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700112 return compare(o) > 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800113}
114
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700115} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800116
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700117#endif // AAPT_LOCALE_VALUE_H