Locale.h revision 6f6ceb7e1456698b1f33e04536bfb3227f9fcfcb
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 <androidfw/ResourceTypes.h>
216f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski#include <string>
226f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski#include <vector>
236f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
246f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinskinamespace aapt {
256f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
266f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski/**
276f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski * A convenience class to build and parse locales.
286f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski */
296f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinskistruct LocaleValue {
306f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski    char language[4];
316f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski    char region[4];
326f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski    char script[4];
336f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski    char variant[8];
346f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
356f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski    inline LocaleValue();
366f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
376f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski    /**
386f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski     * Initialize this LocaleValue from a config string.
396f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski     */
406f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski    bool initFromFilterString(const std::string& config);
416f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
426f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski    /**
436f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski     * Initialize this LocaleValue from parts of a vector.
446f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski     */
456f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski    ssize_t initFromParts(std::vector<std::string>::iterator iter,
466f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski            std::vector<std::string>::iterator end);
476f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
486f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski    /**
496f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski     * Initialize this LocaleValue from a ResTable_config.
506f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski     */
516f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski    void initFromResTable(const android::ResTable_config& config);
526f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
536f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski    /**
546f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski     * Set the locale in a ResTable_config from this LocaleValue.
556f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski     */
566f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski    void writeTo(android::ResTable_config* out) const;
576f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
586f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski    std::string toDirName() const;
596f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
606f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski    inline int compare(const LocaleValue& other) const;
616f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
626f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski    inline bool operator<(const LocaleValue& o) const;
636f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski    inline bool operator<=(const LocaleValue& o) const;
646f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski    inline bool operator==(const LocaleValue& o) const;
656f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski    inline bool operator!=(const LocaleValue& o) const;
666f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski    inline bool operator>=(const LocaleValue& o) const;
676f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski    inline bool operator>(const LocaleValue& o) const;
686f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
696f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinskiprivate:
706f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski     void setLanguage(const char* language);
716f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski     void setRegion(const char* language);
726f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski     void setScript(const char* script);
736f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski     void setVariant(const char* variant);
746f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski};
756f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
766f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski//
776f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski// Implementation
786f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski//
796f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
806f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam LesinskiLocaleValue::LocaleValue() {
816f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski    memset(this, 0, sizeof(LocaleValue));
826f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski}
836f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
846f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinskiint LocaleValue::compare(const LocaleValue& other) const {
856f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski    return memcmp(this, &other, sizeof(LocaleValue));
866f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski}
876f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
886f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinskibool LocaleValue::operator<(const LocaleValue& o) const {
896f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski    return compare(o) < 0;
906f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski}
916f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
926f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinskibool LocaleValue::operator<=(const LocaleValue& o) const {
936f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski    return compare(o) <= 0;
946f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski}
956f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
966f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinskibool LocaleValue::operator==(const LocaleValue& o) const {
976f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski    return compare(o) == 0;
986f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski}
996f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
1006f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinskibool LocaleValue::operator!=(const LocaleValue& o) const {
1016f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski    return compare(o) != 0;
1026f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski}
1036f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
1046f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinskibool LocaleValue::operator>=(const LocaleValue& o) const {
1056f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski    return compare(o) >= 0;
1066f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski}
1076f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
1086f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinskibool LocaleValue::operator>(const LocaleValue& o) const {
1096f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski    return compare(o) > 0;
1106f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski}
1116f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
1126f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski} // namespace aapt
1136f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski
1146f6ceb7e1456698b1f33e04536bfb3227f9fcfcbAdam Lesinski#endif // AAPT_LOCALE_VALUE_H
115