1 /*
2 * Copyright (C) 2017 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 ANDROID_VINTF_PARSE_STRING_H
18 #define ANDROID_VINTF_PARSE_STRING_H
19
20 #include <iostream>
21 #include <sstream>
22 #include <string>
23 #include <string_view>
24
25 #include "CompatibilityMatrix.h"
26 #include "RuntimeInfo.h"
27 #include "HalManifest.h"
28
29 namespace android {
30 namespace vintf {
31
32 std::ostream& operator<<(std::ostream& os, HalFormat hf);
33 std::ostream& operator<<(std::ostream& os, Transport tr);
34 std::ostream& operator<<(std::ostream& os, Arch ar);
35 std::ostream& operator<<(std::ostream& os, KernelConfigType il);
36 std::ostream& operator<<(std::ostream& os, Tristate tr);
37 std::ostream& operator<<(std::ostream& os, SchemaType ksv);
38 std::ostream& operator<<(std::ostream& os, XmlSchemaFormat f);
39 std::ostream& operator<<(std::ostream& os, Level l);
40 std::ostream& operator<<(std::ostream& os, KernelSepolicyVersion v);
41 std::ostream& operator<<(std::ostream& os, ExclusiveTo e);
42 std::ostream& operator<<(std::ostream& os, const Version& ver);
43 std::ostream& operator<<(std::ostream& os, const VersionRange& vr);
44 std::ostream& operator<<(std::ostream& os, const SepolicyVersion& ver);
45 std::ostream& operator<<(std::ostream& os, const SepolicyVersionRange& vr);
46
47 #pragma clang diagnostic push
48 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
49 std::ostream& operator<<(std::ostream& os, const VndkVersionRange& vr);
50 #pragma clang diagnostic pop
51
52 std::ostream& operator<<(std::ostream& os, const KernelVersion& ver);
53 std::ostream& operator<<(std::ostream& os, const TransportArch& ta);
54 std::ostream& operator<<(std::ostream& os, const ManifestHal& hal);
55 std::ostream& operator<<(std::ostream& os, const KernelConfigTypedValue& kcv);
56 std::ostream& operator<<(std::ostream& os, const FqInstance& fqInstance);
57
58 template <typename T>
to_string(const T & obj)59 std::string to_string(const T& obj) {
60 std::ostringstream oss;
61 oss << obj;
62 return oss.str();
63 }
64
65 bool parse(const std::string& s, HalFormat* hf);
66 bool parse(const std::string& s, Transport* tr);
67 bool parse(const std::string& s, Arch* ar);
68 bool parse(const std::string& s, KernelConfigType* il);
69 bool parse(const std::string& s, KernelConfigKey* key);
70 bool parse(const std::string& s, Tristate* tr);
71 bool parse(const std::string& s, SchemaType* ver);
72 bool parse(const std::string& s, XmlSchemaFormat* ver);
73 bool parse(const std::string& s, Level* l);
74 bool parse(const std::string& s, KernelSepolicyVersion* ksv);
75 bool parse(const std::string& s, ExclusiveTo* e);
76 bool parse(const std::string& s, Version* ver);
77 bool parse(const std::string& s, VersionRange* vr);
78 bool parse(const std::string& s, SepolicyVersion* ver);
79 bool parse(const std::string& s, SepolicyVersionRange* ver);
80
81 #pragma clang diagnostic push
82 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
83 bool parse(const std::string& s, VndkVersionRange* vr);
84 #pragma clang diagnostic pop
85
86 bool parse(const std::string& s, KernelVersion* ver);
87 bool parse(const std::string& s, FqInstance* fqInstance);
88
89 bool parseKernelConfigInt(const std::string& s, int64_t* i);
90 bool parseKernelConfigInt(const std::string& s, uint64_t* i);
91 bool parseRange(const std::string& s, KernelConfigRangeValue* range);
92
93 // Parse the KernelConfigValue in s, assuming type kctv->type, and store it in
94 // kctv->value.
95 bool parseKernelConfigValue(const std::string& s, KernelConfigTypedValue* kctv);
96
97 // Parse the KernelConfigTypedValue in s (type is guessed) and store it in kctv.
98 // Do not expect quotes in strings.
99 bool parseKernelConfigTypedValue(const std::string& s, KernelConfigTypedValue* kctv);
100
101 // "100" <=> Version{kFakeAidlMajorVersion, 100}
102 std::string aidlVersionToString(const Version& v);
103 bool parseAidlVersion(const std::string& s, Version* version);
104
105 // "100" <=> VersionRange{kFakeAidlMajorVersion, 100}
106 // "100-105" <=> VersionRange{kFakeAidlMajorVersion, 100, 105}
107 std::string aidlVersionRangeToString(const VersionRange& vr);
108 bool parseAidlVersionRange(const std::string& s, VersionRange* vr);
109
110 // A string that describes the whole object, with versions of all
111 // its components. For debugging and testing purposes only. This is not
112 // the XML string.
113 std::string dump(const HalManifest& vm);
114
115 std::string dump(const RuntimeInfo& ki, bool verbose = true);
116
117 std::vector<std::string> expandInstances(const MatrixHal& req);
118
119 std::string toFQNameString(const std::string& package, const Version& version,
120 const std::string& intf = "", const std::string& instance = "");
121
122 std::string toFQNameString(const Version& version, const std::string& intf,
123 const std::string& instance);
124
125 // [email protected]::IFoo/default.
126 // Note that the format is extended to support a range of versions.
127 std::string toFQNameString(const std::string& package, const VersionRange& range,
128 const std::string& interface, const std::string& instance);
129
130 std::string toFQNameString(const VersionRange& range, const std::string& interface,
131 const std::string& instance);
132
133 std::string toFQNameString(const std::string& interface, const std::string& instance);
134
135 std::string toAidlFqnameString(const std::string& package, const std::string& interface,
136 const std::string& instance = "");
137
138 std::string_view parseApexName(std::string_view path);
139
140 } // namespace vintf
141 } // namespace android
142
143 #endif // ANDROID_VINTF_PARSE_STRING_H
144