blob: 7a8bbee26a01482069296e37d50e2ad1a95c7198 [file] [log] [blame]
Andy Hung1ea842e2020-05-18 10:47:31 -07001/*
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 <string>
20#include <vector>
21
22namespace android::mediametrics::stringutils {
23
24/**
Andy Hunga629bd12020-06-05 16:03:53 -070025 * fieldPrint is a helper method that logs to a stringstream a sequence of
26 * field names (in a fixed size array) together with a variable number of arg parameters.
27 *
28 * stringstream << field[0] << ":" << arg0 << " ";
29 * stringstream << field[1] << ":" << arg1 << " ";
30 * ...
31 * stringstream << field[N-1] << ":" << arg{N-1} << " ";
32 *
33 * The number of fields must exactly match the (variable) arguments.
34 *
35 * Example:
36 *
37 * const char * const fields[] = { "integer" };
38 * std::stringstream ss;
39 * fieldPrint(ss, fields, int(10));
40 */
41template <size_t N, typename... Targs>
42void fieldPrint(std::stringstream& ss, const char * const (& fields)[N], Targs... args) {
43 static_assert(N == sizeof...(args)); // guarantee #fields == #args
44 auto fptr = fields; // get a pointer to the base of fields array
45 ((ss << *fptr++ << ":" << args << " "), ...); // (fold expression), send to stringstream.
46}
47
48/**
Andy Hung1ea842e2020-05-18 10:47:31 -070049 * Return string tokens from iterator, separated by spaces and reserved chars.
50 */
51std::string tokenizer(std::string::const_iterator& it,
52 const std::string::const_iterator& end, const char *reserved);
53
54/**
55 * Splits flags string based on delimeters (or, whitespace which is removed).
56 */
57std::vector<std::string> split(const std::string& flags, const char *delim);
58
59/**
60 * Parse the devices string and return a vector of device address pairs.
61 *
62 * A failure to parse returns early with the contents that were able to be parsed.
63 */
64std::vector<std::pair<std::string, std::string>> getDeviceAddressPairs(const std::string &devices);
65
66/**
67 * Replaces targetChars with replaceChar in string, returns number of chars replaced.
68 */
69size_t replace(std::string &str, const char *targetChars, const char replaceChar);
70
71} // namespace android::mediametrics::stringutils