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