1 /*
2 * Copyright (C) 2020 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 #pragma once
18
19 #include <map>
20 #include <string>
21
22 namespace android::vintf::details {
23
24 using Dirmap = std::map<std::string, std::string>;
25
26 // Assuming each arg is in the format <key><split><value>, turn into a map
27 // of key-value pairs. E.g. if split is '=', then
28 // {"foo=bar", "bar=baz"} -> map{foo: bar, bar: baz}
29 template <typename T>
splitArgs(const T & args,char split)30 std::map<std::string, std::string> splitArgs(const T& args, char split) {
31 std::map<std::string, std::string> ret;
32 for (const auto& arg : args) {
33 auto pos = arg.find(split);
34 auto key = arg.substr(0, pos);
35 auto value = pos == std::string::npos ? std::string{} : arg.substr(pos + 1);
36 ret[key] = value;
37 }
38 return ret;
39 }
40
41 // {"foo:bar", "bar:baz"} -> map{foo: bar, bar: baz}
42 template <typename T>
getDirmap(const T & args)43 Dirmap getDirmap(const T& args) {
44 return splitArgs(args, ':');
45 }
46
47 } // namespace android::vintf::details
48