blob: 3d73b2eb17bfcee80c10593b93501984bb1a4470 [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 Lesinskicacb28f2016-10-19 12:18:14 -070044 /**
45 * Initialize this LocaleValue from parts of a vector.
46 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070047 ssize_t InitFromParts(std::vector<std::string>::iterator iter,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070048 std::vector<std::string>::iterator end);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080049
Adam Lesinskicacb28f2016-10-19 12:18:14 -070050 /**
51 * Initialize this LocaleValue from a ResTable_config.
52 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070053 void InitFromResTable(const android::ResTable_config& config);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080054
Adam Lesinskicacb28f2016-10-19 12:18:14 -070055 /**
56 * Set the locale in a ResTable_config from this LocaleValue.
57 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070058 void WriteTo(android::ResTable_config* out) const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080059
Adam Lesinskicacb28f2016-10-19 12:18:14 -070060 inline int compare(const LocaleValue& other) const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080061
Adam Lesinskicacb28f2016-10-19 12:18:14 -070062 inline bool operator<(const LocaleValue& o) const;
63 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;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080068
Adam Lesinskicacb28f2016-10-19 12:18:14 -070069 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070070 void set_language(const char* language);
71 void set_region(const char* language);
72 void set_script(const char* script);
73 void set_variant(const char* variant);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080074};
75
76//
77// Implementation
78//
79
Adam Lesinskicacb28f2016-10-19 12:18:14 -070080LocaleValue::LocaleValue() { memset(this, 0, sizeof(LocaleValue)); }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080081
82int LocaleValue::compare(const LocaleValue& other) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070083 return memcmp(this, &other, sizeof(LocaleValue));
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080084}
85
86bool LocaleValue::operator<(const LocaleValue& o) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070087 return compare(o) < 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080088}
89
90bool LocaleValue::operator<=(const LocaleValue& o) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070091 return compare(o) <= 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080092}
93
94bool LocaleValue::operator==(const LocaleValue& o) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070095 return compare(o) == 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080096}
97
98bool LocaleValue::operator!=(const LocaleValue& o) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070099 return compare(o) != 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800100}
101
102bool LocaleValue::operator>=(const LocaleValue& o) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700103 return compare(o) >= 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800104}
105
106bool LocaleValue::operator>(const LocaleValue& o) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700107 return compare(o) > 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800108}
109
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700110} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800111
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700112#endif // AAPT_LOCALE_VALUE_H