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