16f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski/*
26f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski * Copyright (C) 2015 The Android Open Source Project
36f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski *
46f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski * Licensed under the Apache License, Version 2.0 (the "License");
56f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski * you may not use this file except in compliance with the License.
66f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski * You may obtain a copy of the License at
76f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski *
86f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski *      http://www.apache.org/licenses/LICENSE-2.0
96f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski *
106f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski * Unless required by applicable law or agreed to in writing, software
116f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski * distributed under the License is distributed on an "AS IS" BASIS,
126f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
136f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski * See the License for the specific language governing permissions and
146f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski * limitations under the License.
156f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski */
166f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
176f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski#ifndef AAPT_LOCALE_VALUE_H
186f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski#define AAPT_LOCALE_VALUE_H
196f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
206f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski#include <string>
216f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski#include <vector>
226f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
23ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski#include "androidfw/ResourceTypes.h"
24d5083f6f6b9bc76bbe64052bcec639eee752a321Adam Lesinski#include "androidfw/StringPiece.h"
25ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski
266f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinskinamespace aapt {
276f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
286f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski/**
296f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski * A convenience class to build and parse locales.
306f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski */
316f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinskistruct LocaleValue {
32cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  char language[4];
33cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  char region[4];
34cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  char script[4];
35cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  char variant[8];
36cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski
37cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  inline LocaleValue();
38cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski
39cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  /**
40cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski   * Initialize this LocaleValue from a config string.
41cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski   */
42d5083f6f6b9bc76bbe64052bcec639eee752a321Adam Lesinski  bool InitFromFilterString(const android::StringPiece& config);
43cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski
44cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  /**
45cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski   * Initialize this LocaleValue from parts of a vector.
46cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski   */
47ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  ssize_t InitFromParts(std::vector<std::string>::iterator iter,
48cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski                        std::vector<std::string>::iterator end);
49cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski
50cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  /**
51cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski   * Initialize this LocaleValue from a ResTable_config.
52cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski   */
53ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  void InitFromResTable(const android::ResTable_config& config);
54cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski
55cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  /**
56cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski   * Set the locale in a ResTable_config from this LocaleValue.
57cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski   */
58ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  void WriteTo(android::ResTable_config* out) const;
59cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski
60cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  inline int compare(const LocaleValue& other) const;
61cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski
62cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  inline bool operator<(const LocaleValue& o) const;
63cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  inline bool operator<=(const LocaleValue& o) const;
64cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  inline bool operator==(const LocaleValue& o) const;
65cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  inline bool operator!=(const LocaleValue& o) const;
66cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  inline bool operator>=(const LocaleValue& o) const;
67cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  inline bool operator>(const LocaleValue& o) const;
68cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski
69cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski private:
70ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  void set_language(const char* language);
71ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  void set_region(const char* language);
72ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  void set_script(const char* script);
73ce5e56e243d262a9b65459c3bd0bb9eaadd40628Adam Lesinski  void set_variant(const char* variant);
746f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski};
756f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
766f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski//
776f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski// Implementation
786f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski//
796f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
80cacb28f2d60858106e2819cc7d95a65e8bda890bAdam LesinskiLocaleValue::LocaleValue() { memset(this, 0, sizeof(LocaleValue)); }
816f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
826f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinskiint LocaleValue::compare(const LocaleValue& other) const {
83cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  return memcmp(this, &other, sizeof(LocaleValue));
846f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski}
856f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
866f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinskibool LocaleValue::operator<(const LocaleValue& o) const {
87cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  return compare(o) < 0;
886f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski}
896f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
906f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinskibool LocaleValue::operator<=(const LocaleValue& o) const {
91cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  return compare(o) <= 0;
926f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski}
936f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
946f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinskibool LocaleValue::operator==(const LocaleValue& o) const {
95cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  return compare(o) == 0;
966f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski}
976f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
986f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinskibool LocaleValue::operator!=(const LocaleValue& o) const {
99cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  return compare(o) != 0;
1006f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski}
1016f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
1026f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinskibool LocaleValue::operator>=(const LocaleValue& o) const {
103cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  return compare(o) >= 0;
1046f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski}
1056f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
1066f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinskibool LocaleValue::operator>(const LocaleValue& o) const {
107cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski  return compare(o) > 0;
1086f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski}
1096f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
110cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski}  // namespace aapt
1116f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
112cacb28f2d60858106e2819cc7d95a65e8bda890bAdam Lesinski#endif  // AAPT_LOCALE_VALUE_H
113