utils.h revision 443df7932cedd88d523164ac7175fa19ec553c7b
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 FRAMEWORK_NATIVE_CMDS_LSHAL_UTILS_H_
18#define FRAMEWORK_NATIVE_CMDS_LSHAL_UTILS_H_
19
20#include <iomanip>
21#include <iostream>
22#include <string>
23#include <sstream>
24#include <utility>
25#include <vector>
26
27namespace android {
28namespace lshal {
29
30enum : unsigned int {
31    OK                                      = 0,
32    USAGE                                   = 1 << 0,
33    NO_BINDERIZED_MANAGER                   = 1 << 1,
34    NO_PASSTHROUGH_MANAGER                  = 1 << 2,
35    DUMP_BINDERIZED_ERROR                   = 1 << 3,
36    DUMP_PASSTHROUGH_ERROR                  = 1 << 4,
37    DUMP_ALL_LIBS_ERROR                     = 1 << 5,
38    IO_ERROR                                = 1 << 6,
39};
40using Status = unsigned int;
41
42struct Arg {
43    int argc;
44    char **argv;
45};
46
47template <typename A>
48std::string join(const A &components, const std::string &separator) {
49    std::stringstream out;
50    bool first = true;
51    for (const auto &component : components) {
52        if (!first) {
53            out << separator;
54        }
55        out << component;
56
57        first = false;
58    }
59    return out.str();
60}
61
62std::string toHexString(uint64_t t);
63
64template<typename String>
65std::pair<String, String> splitFirst(const String &s, char c) {
66    const char *pos = strchr(s.c_str(), c);
67    if (pos == nullptr) {
68        return {s, {}};
69    }
70    return {String(s.c_str(), pos - s.c_str()), String(pos + 1)};
71}
72
73std::vector<std::string> split(const std::string &s, char c);
74
75void replaceAll(std::string *s, char from, char to);
76
77}  // namespace lshal
78}  // namespace android
79
80#endif  // FRAMEWORK_NATIVE_CMDS_LSHAL_UTILS_H_
81