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 #include "Resource.h"
18 #include "util/StringPiece.h"
19 
20 #include <map>
21 #include <string>
22 
23 namespace aapt {
24 
toString(ResourceType type)25 StringPiece16 toString(ResourceType type) {
26     switch (type) {
27         case ResourceType::kAnim:          return u"anim";
28         case ResourceType::kAnimator:      return u"animator";
29         case ResourceType::kArray:         return u"array";
30         case ResourceType::kAttr:          return u"attr";
31         case ResourceType::kAttrPrivate:   return u"^attr-private";
32         case ResourceType::kBool:          return u"bool";
33         case ResourceType::kColor:         return u"color";
34         case ResourceType::kDimen:         return u"dimen";
35         case ResourceType::kDrawable:      return u"drawable";
36         case ResourceType::kFraction:      return u"fraction";
37         case ResourceType::kId:            return u"id";
38         case ResourceType::kInteger:       return u"integer";
39         case ResourceType::kInterpolator:  return u"interpolator";
40         case ResourceType::kLayout:        return u"layout";
41         case ResourceType::kMenu:          return u"menu";
42         case ResourceType::kMipmap:        return u"mipmap";
43         case ResourceType::kPlurals:       return u"plurals";
44         case ResourceType::kRaw:           return u"raw";
45         case ResourceType::kString:        return u"string";
46         case ResourceType::kStyle:         return u"style";
47         case ResourceType::kStyleable:     return u"styleable";
48         case ResourceType::kTransition:    return u"transition";
49         case ResourceType::kXml:           return u"xml";
50     }
51     return {};
52 }
53 
54 static const std::map<StringPiece16, ResourceType> sResourceTypeMap {
55         { u"anim", ResourceType::kAnim },
56         { u"animator", ResourceType::kAnimator },
57         { u"array", ResourceType::kArray },
58         { u"attr", ResourceType::kAttr },
59         { u"^attr-private", ResourceType::kAttrPrivate },
60         { u"bool", ResourceType::kBool },
61         { u"color", ResourceType::kColor },
62         { u"dimen", ResourceType::kDimen },
63         { u"drawable", ResourceType::kDrawable },
64         { u"fraction", ResourceType::kFraction },
65         { u"id", ResourceType::kId },
66         { u"integer", ResourceType::kInteger },
67         { u"interpolator", ResourceType::kInterpolator },
68         { u"layout", ResourceType::kLayout },
69         { u"menu", ResourceType::kMenu },
70         { u"mipmap", ResourceType::kMipmap },
71         { u"plurals", ResourceType::kPlurals },
72         { u"raw", ResourceType::kRaw },
73         { u"string", ResourceType::kString },
74         { u"style", ResourceType::kStyle },
75         { u"styleable", ResourceType::kStyleable },
76         { u"transition", ResourceType::kTransition },
77         { u"xml", ResourceType::kXml },
78 };
79 
parseResourceType(const StringPiece16 & str)80 const ResourceType* parseResourceType(const StringPiece16& str) {
81     auto iter = sResourceTypeMap.find(str);
82     if (iter == std::end(sResourceTypeMap)) {
83         return nullptr;
84     }
85     return &iter->second;
86 }
87 
operator <(const ResourceKey & a,const ResourceKey & b)88 bool operator<(const ResourceKey& a, const ResourceKey& b) {
89     return std::tie(a.name, a.config) < std::tie(b.name, b.config);
90 }
91 
operator <(const ResourceKeyRef & a,const ResourceKeyRef & b)92 bool operator<(const ResourceKeyRef& a, const ResourceKeyRef& b) {
93     return std::tie(a.name, a.config) < std::tie(b.name, b.config);
94 }
95 
96 } // namespace aapt
97