1 /*
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
18 #define LOG_TAG "mediametrics::stringutils"
19 #include <utils/Log.h>
20
21 #include "StringUtils.h"
22 #include "AudioTypes.h"
23 #include <audio_utils/StringUtils.h>
24 #include <charconv>
25
26 namespace android::mediametrics::stringutils {
27
replace(std::string & str,const char * targetChars,const char replaceChar)28 size_t replace(std::string &str, const char *targetChars, const char replaceChar)
29 {
30 size_t replaced = 0;
31 for (char &c : str) {
32 if (strchr(targetChars, c) != nullptr) {
33 c = replaceChar;
34 ++replaced;
35 }
36 }
37 return replaced;
38 }
39
40 template <types::AudioEnumCategory CATEGORY>
41 std::pair<std::string /* external statsd */, std::string /* internal */>
parseDevicePairs(const std::string & devicePairs)42 parseDevicePairs(const std::string& devicePairs) {
43 std::pair<std::string, std::string> result{};
44 const auto devaddrvec = audio_utils::stringutils::getDeviceAddressPairs(devicePairs);
45 for (const auto& [device, addr] : devaddrvec) { // addr ignored for now.
46 if (!result.second.empty()) {
47 result.second.append("|"); // delimit devices with '|'.
48 result.first.append("|");
49 }
50 result.second.append(device);
51 result.first.append(types::lookup<CATEGORY, std::string>(device));
52 }
53 return result;
54 }
55
56 std::pair<std::string /* external statsd */, std::string /* internal */>
parseOutputDevicePairs(const std::string & devicePairs)57 parseOutputDevicePairs(const std::string& devicePairs) {
58 return parseDevicePairs<types::OUTPUT_DEVICE>(devicePairs);
59 }
60
61 std::pair<std::string /* external statsd */, std::string /* internal */>
parseInputDevicePairs(const std::string & devicePairs)62 parseInputDevicePairs(const std::string& devicePairs) {
63 return parseDevicePairs<types::INPUT_DEVICE>(devicePairs);
64 }
65
66 } // namespace android::mediametrics::stringutils
67