diff --git a/src/audio/codec.cpp b/src/audio/codec.cpp index 813693e66..d14df2d82 100644 --- a/src/audio/codec.cpp +++ b/src/audio/codec.cpp @@ -42,7 +42,7 @@ #include #include // for printf #include // for NULL, calloc, free, realloc -#include // for strchr, memset, strtok_r, strdupa +#include // for strchr, memset, strtok_r #include #include @@ -53,6 +53,7 @@ #include "utils/color_out.h" // for col, SBOLD, SRED, TRED #include "utils/macros.h" #include "utils/misc.h" +#include "utils/string_view_utils.hpp" #define MOD_NAME "[acodec] " @@ -410,27 +411,24 @@ get_audio_codec(const char *codec) struct audio_codec_params parse_audio_codec_params(const char *ccfg) { - char *cfg = strdupa(ccfg); - char *save_ptr = nullptr; - char *tmp = cfg; - char *item = nullptr; - struct audio_codec_params params { }; - while ((item = strtok_r(tmp, ":", &save_ptr)) != nullptr) { - tmp = nullptr; + std::string_view sv = ccfg; + while (!sv.empty()) { + std::string item = std::string(tokenize(sv, ':')); if (params.codec == AC_NONE) { - params.codec = get_audio_codec(item); + params.codec = get_audio_codec(item.c_str()); if (params.codec == AC_NONE) { MSG(ERROR, "Unknown audio codec \"%s\" given!\n", - item); + item.c_str()); return {}; } continue; } - if (IS_KEY_PREFIX(item, "sample_rate")) { - params.sample_rate = stoi(strchr(item, '=') + 1); + if (IS_KEY_PREFIX(item.c_str(), "sample_rate")) { + params.sample_rate = + stoi(item.substr(item.find('=') + 1)); if (params.sample_rate < 0) { MSG(ERROR, "Sample rate cannot be negative! given " @@ -438,10 +436,10 @@ parse_audio_codec_params(const char *ccfg) params.sample_rate); return {}; } - } else if (IS_KEY_PREFIX(item, "bitrate")) { - const char *val = strchr(item, '=') + 1; + } else if (IS_KEY_PREFIX(item.c_str(), "bitrate")) { + std::string val = item.substr(item.find('=') + 1); const char *endptr = nullptr; - long long rate = unit_evaluate(val, &endptr); + long long rate = unit_evaluate(val.c_str(), &endptr); if (rate <= 0 || rate > INT_MAX || endptr[0] != '\0') { LOG(LOG_LEVEL_ERROR) << "Wrong bitrate: " << val << "\n"; @@ -449,7 +447,7 @@ parse_audio_codec_params(const char *ccfg) } params.bitrate = (int) rate; } else { - MSG(ERROR, "Unknown audio option: %s\n", item); + MSG(ERROR, "Unknown audio option: %s\n", item.c_str()); return {}; } } diff --git a/src/host.cpp b/src/host.cpp index 09d4de031..3d4a08077 100644 --- a/src/host.cpp +++ b/src/host.cpp @@ -6,7 +6,7 @@ * This file contains common external definitions. */ /* - * Copyright (c) 2013-2024 CESNET + * Copyright (c) 2013-2025 CESNET * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -78,7 +78,6 @@ #include // for ssize_t #include // for tuple, get, make_tuple #include // for STDERR_FILENO -#include // for vector #include "audio/audio_capture.h" #include "audio/audio_filter.h" @@ -88,7 +87,6 @@ #include "audio/utils.h" #include "capture_filter.h" #include "compat/platform_pipe.h" -#include "compat/strings.h" // strdupa #include "compat/net.h" // for fd_t #include "cuda_wrapper.h" // for cudaDeviceReset #include "debug.h" @@ -980,29 +978,27 @@ bool parse_params(const char *optarg, bool preinit) print_param_doc(); return false; } - char *tmp = strdupa(optarg); - char *item = nullptr; - char *save_ptr = nullptr; - while ((item = strtok_r(tmp, ",", &save_ptr)) != nullptr) { - tmp = nullptr; - char *key_cstr = item; - const char *val_cstr = ""; - if (char *delim = strchr(item, '=')) { - val_cstr = delim + 1; - *delim = '\0'; + std::string_view sv = optarg; + while (!sv.empty()) { + std::string key = std::string(tokenize(sv, ',')); + std::string val; + std::string::size_type delim_pos = key.find('='); + if (delim_pos != std::string::npos) { + val = key.substr(delim_pos + 1); + key.resize(delim_pos); } - if (!validate_param(key_cstr)) { + if (!validate_param(key.c_str())) { if (preinit) { continue; } - LOG(LOG_LEVEL_ERROR) << "Unknown parameter: " << key_cstr << "\n"; + LOG(LOG_LEVEL_ERROR) << "Unknown parameter: " << key << "\n"; LOG(LOG_LEVEL_INFO) << "Type '" << uv_argv[0] << " --param help' for list.\n"; if (get_commandline_param("allow-unknown-params") == nullptr) { return false; } } - commandline_params[key_cstr] = val_cstr; + commandline_params[key] = std::move(val); } return true; }