blob: fc6c448ac1dcd56696ec7aa5fa4f6dc3b220b54f [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"
24
25#include "util/StringPiece.h"
26
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080027namespace aapt {
28
29/**
30 * A convenience class to build and parse locales.
31 */
32struct LocaleValue {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070033 char language[4];
34 char region[4];
35 char script[4];
36 char variant[8];
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080037
Adam Lesinskicacb28f2016-10-19 12:18:14 -070038 inline LocaleValue();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080039
Adam Lesinskicacb28f2016-10-19 12:18:14 -070040 /**
41 * Initialize this LocaleValue from a config string.
42 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070043 bool InitFromFilterString(const StringPiece& config);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080044
Adam Lesinskicacb28f2016-10-19 12:18:14 -070045 /**
46 * Initialize this LocaleValue from parts of a vector.
47 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070048 ssize_t InitFromParts(std::vector<std::string>::iterator iter,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070049 std::vector<std::string>::iterator end);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080050
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051 /**
52 * Initialize this LocaleValue from a ResTable_config.
53 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070054 void InitFromResTable(const android::ResTable_config& config);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080055
Adam Lesinskicacb28f2016-10-19 12:18:14 -070056 /**
57 * Set the locale in a ResTable_config from this LocaleValue.
58 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070059 void WriteTo(android::ResTable_config* out) const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080060
Adam Lesinskicacb28f2016-10-19 12:18:14 -070061 inline int compare(const LocaleValue& other) const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080062
Adam Lesinskicacb28f2016-10-19 12:18:14 -070063 inline bool operator<(const LocaleValue& o) const;
64 inline bool operator<=(const LocaleValue& o) const;
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;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080069
Adam Lesinskicacb28f2016-10-19 12:18:14 -070070 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070071 void set_language(const char* language);
72 void set_region(const char* language);
73 void set_script(const char* script);
74 void set_variant(const char* variant);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080075};
76
77//
78// Implementation
79//
80
Adam Lesinskicacb28f2016-10-19 12:18:14 -070081LocaleValue::LocaleValue() { memset(this, 0, sizeof(LocaleValue)); }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080082
83int LocaleValue::compare(const LocaleValue& other) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070084 return memcmp(this, &other, sizeof(LocaleValue));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080085}
86
87bool LocaleValue::operator<(const LocaleValue& o) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070088 return compare(o) < 0;
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
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700111} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800112
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700113#endif // AAPT_LOCALE_VALUE_H