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
20#include <androidfw/ResourceTypes.h>
21#include <string>
22#include <vector>
23
24namespace aapt {
25
26/**
27 * A convenience class to build and parse locales.
28 */
29struct LocaleValue {
30    char language[4];
31    char region[4];
32    char script[4];
33    char variant[8];
34
35    inline LocaleValue();
36
37    /**
38     * Initialize this LocaleValue from a config string.
39     */
40    bool initFromFilterString(const std::string& config);
41
42    /**
43     * Initialize this LocaleValue from parts of a vector.
44     */
45    ssize_t initFromParts(std::vector<std::string>::iterator iter,
46            std::vector<std::string>::iterator end);
47
48    /**
49     * Initialize this LocaleValue from a ResTable_config.
50     */
51    void initFromResTable(const android::ResTable_config& config);
52
53    /**
54     * Set the locale in a ResTable_config from this LocaleValue.
55     */
56    void writeTo(android::ResTable_config* out) const;
57
58    std::string toDirName() const;
59
60    inline int compare(const LocaleValue& other) const;
61
62    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;
68
69private:
70     void setLanguage(const char* language);
71     void setRegion(const char* language);
72     void setScript(const char* script);
73     void setVariant(const char* variant);
74};
75
76//
77// Implementation
78//
79
80LocaleValue::LocaleValue() {
81    memset(this, 0, sizeof(LocaleValue));
82}
83
84int LocaleValue::compare(const LocaleValue& other) const {
85    return memcmp(this, &other, sizeof(LocaleValue));
86}
87
88bool LocaleValue::operator<(const LocaleValue& o) const {
89    return compare(o) < 0;
90}
91
92bool LocaleValue::operator<=(const LocaleValue& o) const {
93    return compare(o) <= 0;
94}
95
96bool LocaleValue::operator==(const LocaleValue& o) const {
97    return compare(o) == 0;
98}
99
100bool LocaleValue::operator!=(const LocaleValue& o) const {
101    return compare(o) != 0;
102}
103
104bool LocaleValue::operator>=(const LocaleValue& o) const {
105    return compare(o) >= 0;
106}
107
108bool LocaleValue::operator>(const LocaleValue& o) const {
109    return compare(o) > 0;
110}
111
112} // namespace aapt
113
114#endif // AAPT_LOCALE_VALUE_H
115