Home | History | Annotate | Download | only in aapt2
      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 "util/StringPiece.h"
     21 
     22 #include <androidfw/ResourceTypes.h>
     23 #include <string>
     24 #include <vector>
     25 
     26 namespace aapt {
     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 StringPiece& config);
     43 
     44     /**
     45      * Initialize this LocaleValue from parts of a vector.
     46      */
     47     ssize_t initFromParts(std::vector<std::string>::iterator iter,
     48             std::vector<std::string>::iterator end);
     49 
     50     /**
     51      * Initialize this LocaleValue from a ResTable_config.
     52      */
     53     void initFromResTable(const android::ResTable_config& config);
     54 
     55     /**
     56      * Set the locale in a ResTable_config from this LocaleValue.
     57      */
     58     void writeTo(android::ResTable_config* out) const;
     59 
     60     std::string toDirName() const;
     61 
     62     inline int compare(const LocaleValue& other) const;
     63 
     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     inline bool operator>(const LocaleValue& o) const;
     70 
     71 private:
     72      void setLanguage(const char* language);
     73      void setRegion(const char* language);
     74      void setScript(const char* script);
     75      void setVariant(const char* variant);
     76 };
     77 
     78 //
     79 // Implementation
     80 //
     81 
     82 LocaleValue::LocaleValue() {
     83     memset(this, 0, sizeof(LocaleValue));
     84 }
     85 
     86 int LocaleValue::compare(const LocaleValue& other) const {
     87     return memcmp(this, &other, sizeof(LocaleValue));
     88 }
     89 
     90 bool LocaleValue::operator<(const LocaleValue& o) const {
     91     return compare(o) < 0;
     92 }
     93 
     94 bool LocaleValue::operator<=(const LocaleValue& o) const {
     95     return compare(o) <= 0;
     96 }
     97 
     98 bool LocaleValue::operator==(const LocaleValue& o) const {
     99     return compare(o) == 0;
    100 }
    101 
    102 bool LocaleValue::operator!=(const LocaleValue& o) const {
    103     return compare(o) != 0;
    104 }
    105 
    106 bool LocaleValue::operator>=(const LocaleValue& o) const {
    107     return compare(o) >= 0;
    108 }
    109 
    110 bool LocaleValue::operator>(const LocaleValue& o) const {
    111     return compare(o) > 0;
    112 }
    113 
    114 } // namespace aapt
    115 
    116 #endif // AAPT_LOCALE_VALUE_H
    117