blob: 3b2db85aac3648db20899513213d6494e3192859 [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//#define LOG_NDEBUG 0
Brian Lindahl778b15a2023-05-13 07:42:13 -060018#define LOG_TAG "mediametrics::stringutils"
Andy Hung1ea842e2020-05-18 10:47:31 -070019#include <utils/Log.h>
20
21#include "StringUtils.h"
22
Brian Lindahl778b15a2023-05-13 07:42:13 -060023#include <charconv>
24
Andy Hung76adef72022-09-14 17:24:19 -070025#include "AudioTypes.h"
26
Andy Hung1ea842e2020-05-18 10:47:31 -070027namespace android::mediametrics::stringutils {
28
29std::string tokenizer(std::string::const_iterator& it,
30 const std::string::const_iterator& end, const char *reserved)
31{
32 // consume leading white space
33 for (; it != end && std::isspace(*it); ++it);
34 if (it == end) return {};
35
36 auto start = it;
37 // parse until we hit a reserved keyword or space
38 if (strchr(reserved, *it)) return {start, ++it};
39 for (;;) {
40 ++it;
41 if (it == end || std::isspace(*it) || strchr(reserved, *it)) return {start, it};
42 }
43}
44
45std::vector<std::string> split(const std::string& flags, const char *delim)
46{
47 std::vector<std::string> result;
48 for (auto it = flags.begin(); ; ) {
49 auto flag = tokenizer(it, flags.end(), delim);
50 if (flag.empty() || !std::isalnum(flag[0])) return result;
51 result.emplace_back(std::move(flag));
52
53 // look for the delimeter and discard
54 auto token = tokenizer(it, flags.end(), delim);
55 if (token.size() != 1 || strchr(delim, token[0]) == nullptr) return result;
56 }
57}
58
Brian Lindahl778b15a2023-05-13 07:42:13 -060059bool parseVector(const std::string &str, std::vector<int32_t> *vector) {
60 std::vector<int32_t> values;
61 const char *p = str.c_str();
62 const char *last = p + str.size();
63 while (p != last) {
64 if (*p == ',' || *p == '{' || *p == '}') {
65 p++;
66 }
67 int32_t value = -1;
68 auto [ptr, error] = std::from_chars(p, last, value);
69 if (error == std::errc::invalid_argument || error == std::errc::result_out_of_range) {
70 return false;
71 }
72 p = ptr;
73 values.push_back(value);
74 }
75 *vector = std::move(values);
76 return true;
77}
78
Andy Hung1ea842e2020-05-18 10:47:31 -070079std::vector<std::pair<std::string, std::string>> getDeviceAddressPairs(const std::string& devices)
80{
81 std::vector<std::pair<std::string, std::string>> result;
82
Andy Hungc04a8202024-11-01 17:38:06 -070083 // Currently, the device format is
84 //
85 // devices = device_addr OR device_addr|devices
86 // device_addr = device OR (device, addr)
87 //
88 // EXAMPLE:
89 // device1|(device2, addr2)|...
Andy Hung1ea842e2020-05-18 10:47:31 -070090
91 static constexpr char delim[] = "()|,";
92 for (auto it = devices.begin(); ; ) {
Andy Hungc04a8202024-11-01 17:38:06 -070093 std::string address;
94 std::string device = tokenizer(it, devices.end(), delim);
95 if (device.empty()) return result;
96 if (device == "(") { // it is a pair otherwise we consider it a device
97 device = tokenizer(it, devices.end(), delim); // get actual device
98 auto token = tokenizer(it, devices.end(), delim);
99 if (token != ",") return result; // malformed, must have a comma
Andy Hung1ea842e2020-05-18 10:47:31 -0700100
Andy Hungc04a8202024-11-01 17:38:06 -0700101 // special handling here for empty addresses
102 address = tokenizer(it, devices.end(), delim);
103 if (address.empty()) return result;
104 if (address == ")") { // no address, just the ")"
105 address.clear();
106 } else {
107 token = tokenizer(it, devices.end(), delim);
108 if (token != ")") return result;
109 }
Andy Hung1ea842e2020-05-18 10:47:31 -0700110 }
Andy Hungc04a8202024-11-01 17:38:06 -0700111 // misaligned token, device must start alphanumeric.
112 if (!std::isalnum(device[0])) return result;
Andy Hung1ea842e2020-05-18 10:47:31 -0700113
114 result.emplace_back(std::move(device), std::move(address));
115
Andy Hungc04a8202024-11-01 17:38:06 -0700116 auto token = tokenizer(it, devices.end(), delim);
Andy Hung1ea842e2020-05-18 10:47:31 -0700117 if (token != "|") return result; // this includes end of string detection
118 }
119}
120
121size_t replace(std::string &str, const char *targetChars, const char replaceChar)
122{
123 size_t replaced = 0;
124 for (char &c : str) {
125 if (strchr(targetChars, c) != nullptr) {
126 c = replaceChar;
127 ++replaced;
128 }
129 }
130 return replaced;
131}
132
Andy Hung76adef72022-09-14 17:24:19 -0700133template <types::AudioEnumCategory CATEGORY>
134std::pair<std::string /* external statsd */, std::string /* internal */>
135parseDevicePairs(const std::string& devicePairs) {
136 std::pair<std::string, std::string> result{};
137 const auto devaddrvec = stringutils::getDeviceAddressPairs(devicePairs);
138 for (const auto& [device, addr] : devaddrvec) { // addr ignored for now.
139 if (!result.second.empty()) {
140 result.second.append("|"); // delimit devices with '|'.
141 result.first.append("|");
142 }
143 result.second.append(device);
144 result.first.append(types::lookup<CATEGORY, std::string>(device));
145 }
146 return result;
147}
148
149std::pair<std::string /* external statsd */, std::string /* internal */>
150parseOutputDevicePairs(const std::string& devicePairs) {
151 return parseDevicePairs<types::OUTPUT_DEVICE>(devicePairs);
152}
153
154std::pair<std::string /* external statsd */, std::string /* internal */>
155parseInputDevicePairs(const std::string& devicePairs) {
156 return parseDevicePairs<types::INPUT_DEVICE>(devicePairs);
157}
158
Andy Hung1ea842e2020-05-18 10:47:31 -0700159} // namespace android::mediametrics::stringutils