utils.h revision a8bedc6ae3f6d610aa634445809835c26aaf16bc
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    // Return to Lshal::main to print help info.
33    USAGE                                   = 1 << 0,
34    NO_BINDERIZED_MANAGER                   = 1 << 1,
35    NO_PASSTHROUGH_MANAGER                  = 1 << 2,
36    DUMP_BINDERIZED_ERROR                   = 1 << 3,
37    DUMP_PASSTHROUGH_ERROR                  = 1 << 4,
38    DUMP_ALL_LIBS_ERROR                     = 1 << 5,
39    IO_ERROR                                = 1 << 6,
40    NO_INTERFACE                            = 1 << 7,
41    TRANSACTION_ERROR                       = 1 << 8,
42};
43using Status = unsigned int;
44
45struct Arg {
46    int argc;
47    char **argv;
48};
49
50template <typename A>
51std::string join(const A &components, const std::string &separator) {
52    std::stringstream out;
53    bool first = true;
54    for (const auto &component : components) {
55        if (!first) {
56            out << separator;
57        }
58        out << component;
59
60        first = false;
61    }
62    return out.str();
63}
64
65std::string toHexString(uint64_t t);
66
67template<typename String>
68std::pair<String, String> splitFirst(const String &s, char c) {
69    const char *pos = strchr(s.c_str(), c);
70    if (pos == nullptr) {
71        return {s, {}};
72    }
73    return {String(s.c_str(), pos - s.c_str()), String(pos + 1)};
74}
75
76std::vector<std::string> split(const std::string &s, char c);
77
78void replaceAll(std::string *s, char from, char to);
79
80}  // namespace lshal
81}  // namespace android
82
83#endif  // FRAMEWORK_NATIVE_CMDS_LSHAL_UTILS_H_
84