xref: /aosp_15_r20/system/libvintf/include-host/vintf/Dirmap.h (revision 70a7ec852fcefd15a4fb57f8f183a8b1c3aacb08)
1*70a7ec85SAndroid Build Coastguard Worker /*
2*70a7ec85SAndroid Build Coastguard Worker  * Copyright (C) 2020 The Android Open Source Project
3*70a7ec85SAndroid Build Coastguard Worker  *
4*70a7ec85SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*70a7ec85SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*70a7ec85SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*70a7ec85SAndroid Build Coastguard Worker  *
8*70a7ec85SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*70a7ec85SAndroid Build Coastguard Worker  *
10*70a7ec85SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*70a7ec85SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*70a7ec85SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*70a7ec85SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*70a7ec85SAndroid Build Coastguard Worker  * limitations under the License.
15*70a7ec85SAndroid Build Coastguard Worker  */
16*70a7ec85SAndroid Build Coastguard Worker 
17*70a7ec85SAndroid Build Coastguard Worker #pragma once
18*70a7ec85SAndroid Build Coastguard Worker 
19*70a7ec85SAndroid Build Coastguard Worker #include <map>
20*70a7ec85SAndroid Build Coastguard Worker #include <string>
21*70a7ec85SAndroid Build Coastguard Worker 
22*70a7ec85SAndroid Build Coastguard Worker namespace android::vintf::details {
23*70a7ec85SAndroid Build Coastguard Worker 
24*70a7ec85SAndroid Build Coastguard Worker using Dirmap = std::map<std::string, std::string>;
25*70a7ec85SAndroid Build Coastguard Worker 
26*70a7ec85SAndroid Build Coastguard Worker // Assuming each arg is in the format <key><split><value>, turn into a map
27*70a7ec85SAndroid Build Coastguard Worker // of key-value pairs. E.g. if split is '=', then
28*70a7ec85SAndroid Build Coastguard Worker // {"foo=bar", "bar=baz"} -> map{foo: bar, bar: baz}
29*70a7ec85SAndroid Build Coastguard Worker template <typename T>
splitArgs(const T & args,char split)30*70a7ec85SAndroid Build Coastguard Worker std::map<std::string, std::string> splitArgs(const T& args, char split) {
31*70a7ec85SAndroid Build Coastguard Worker     std::map<std::string, std::string> ret;
32*70a7ec85SAndroid Build Coastguard Worker     for (const auto& arg : args) {
33*70a7ec85SAndroid Build Coastguard Worker         auto pos = arg.find(split);
34*70a7ec85SAndroid Build Coastguard Worker         auto key = arg.substr(0, pos);
35*70a7ec85SAndroid Build Coastguard Worker         auto value = pos == std::string::npos ? std::string{} : arg.substr(pos + 1);
36*70a7ec85SAndroid Build Coastguard Worker         ret[key] = value;
37*70a7ec85SAndroid Build Coastguard Worker     }
38*70a7ec85SAndroid Build Coastguard Worker     return ret;
39*70a7ec85SAndroid Build Coastguard Worker }
40*70a7ec85SAndroid Build Coastguard Worker 
41*70a7ec85SAndroid Build Coastguard Worker // {"foo:bar", "bar:baz"} -> map{foo: bar, bar: baz}
42*70a7ec85SAndroid Build Coastguard Worker template <typename T>
getDirmap(const T & args)43*70a7ec85SAndroid Build Coastguard Worker Dirmap getDirmap(const T& args) {
44*70a7ec85SAndroid Build Coastguard Worker     return splitArgs(args, ':');
45*70a7ec85SAndroid Build Coastguard Worker }
46*70a7ec85SAndroid Build Coastguard Worker 
47*70a7ec85SAndroid Build Coastguard Worker }  // namespace android::vintf::details
48