From 31978968ae1d7b5865b54a358eef0a899026f02b Mon Sep 17 00:00:00 2001 From: Hunter Rick Date: Sun, 12 Nov 2023 22:28:13 -0800 Subject: [PATCH] Made changes as recommended in PR review by gavv --- .../roc_address/print_supported.cpp | 26 ++++------ .../roc_address/print_supported.h | 6 +-- .../roc_address/protocol_map.cpp | 49 ++++++++++++------- .../roc_address/protocol_map.h | 6 +-- .../roc_sndio/print_supported.cpp | 2 +- 5 files changed, 47 insertions(+), 42 deletions(-) diff --git a/src/internal_modules/roc_address/print_supported.cpp b/src/internal_modules/roc_address/print_supported.cpp index 93dae6ae65..564acaca73 100644 --- a/src/internal_modules/roc_address/print_supported.cpp +++ b/src/internal_modules/roc_address/print_supported.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019 Roc Streaming authors + * Copyright (c) 2023 Roc Streaming authors * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this @@ -16,13 +16,11 @@ namespace address { namespace { -enum { ArraySize = 100, LineSize = 70 }; +enum { LineSize = 70 }; -void print_string_list(core::Printer& prn, - Interface interface, - const core::StringList& list, - const char* prefix, - const char* suffix) { +void print_interface_protos(core::Printer& prn, + Interface interface, + const core::StringList& list) { const char* str = list.front(); while (str != NULL) { @@ -33,7 +31,7 @@ void print_string_list(core::Printer& prn, prn.writef(" %s:", interface_to_str(interface)); while (size < LineSize) { - size += prn.writef(" %s%s%s", prefix, str, suffix); + size += prn.writef(" %s%s%s", "", str, "://"); str = list.nextof(str); if (!str) { @@ -52,26 +50,22 @@ bool print_supported(ProtocolMap& protocol_map, core::IArena& arena) { core::Array interface_array(arena); core::StringList list(arena); - if (!interface_array.resize(Iface_Max)) { - return false; - } - if (!protocol_map.get_supported_interfaces(interface_array)) { roc_log(LogError, "can't retrieve interface array"); return false; } - for (size_t x = 0; x < interface_array.size(); x++) { - if (!protocol_map.get_supported_protocols(interface_array[x], list)) { + for (size_t n_interface = 0; n_interface < interface_array.size(); n_interface++) { + if (!protocol_map.get_supported_protocols(interface_array[n_interface], list)) { roc_log(LogError, "can't retrieve protocols list"); return false; } - if (x == 0) { + if (n_interface == 0) { prn.writef("\nsupported network protocols:\n"); } - print_string_list(prn, interface_array[x], list, "", "://"); + print_interface_protos(prn, interface_array[n_interface], list); } return true; } diff --git a/src/internal_modules/roc_address/print_supported.h b/src/internal_modules/roc_address/print_supported.h index cd18fce8d8..6503ae56ef 100644 --- a/src/internal_modules/roc_address/print_supported.h +++ b/src/internal_modules/roc_address/print_supported.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019 Roc Streaming authors + * Copyright (c) 2023 Roc Streaming authors * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this @@ -7,7 +7,7 @@ */ //! @file roc_address/print_supported.h -//! @brief Print supported schemes and formats. +//! @brief Print supported interfaces and protocols. #ifndef ROC_ADDRESS_PRINT_SUPPORTED_H_ #define ROC_ADDRESS_PRINT_SUPPORTED_H_ @@ -19,7 +19,7 @@ namespace roc { namespace address { -//! Print supported schemes and formats. +//! Print supported interfaces and protocols. ROC_ATTR_NODISCARD bool print_supported(ProtocolMap&, core::IArena&); } // namespace address diff --git a/src/internal_modules/roc_address/protocol_map.cpp b/src/internal_modules/roc_address/protocol_map.cpp index 93697e8c63..674bc8b170 100644 --- a/src/internal_modules/roc_address/protocol_map.cpp +++ b/src/internal_modules/roc_address/protocol_map.cpp @@ -121,22 +121,22 @@ const ProtocolAttrs* ProtocolMap::find_by_scheme(const char* scheme) const { return NULL; } -void ProtocolMap::add_proto_(const ProtocolAttrs& proto) { - roc_panic_if((int)proto.protocol < 0); - roc_panic_if((int)proto.protocol >= MaxProtos); - roc_panic_if(protos_[proto.protocol].protocol != 0); - - protos_[proto.protocol] = proto; -} - bool ProtocolMap::get_supported_interfaces(core::Array& interface_array) { interface_array.clear(); bool interfaces_exist = false; - for (unsigned x = (unsigned)Iface_Consolidated + 1; x != (unsigned)Iface_Max; x++) { - for (size_t y = 0; y < MaxProtos; y++) { - if (x == (unsigned)protos_[y].iface) { - interface_array.push_back(protos_[y].iface); + for (unsigned n_iface = (unsigned)Iface_Consolidated; n_iface != (unsigned)Iface_Max; + n_iface++) { + for (size_t n_proto = 0; n_proto < MaxProtos; n_proto++) { + if (protos_[n_proto].protocol == Proto_None) { + continue; + } + + if (n_iface == (unsigned)protos_[n_proto].iface) { + if (!interface_array.grow(interface_array.size() + 1)) { + return false; + } + interface_array.push_back(protos_[n_proto].iface); interfaces_exist = true; break; } @@ -149,23 +149,34 @@ bool ProtocolMap::get_supported_interfaces(core::Array& interface_arr bool ProtocolMap::get_supported_protocols(Interface interface, core::StringList& list) { list.clear(); - bool protocolsExist = false; + bool protocols_exist = false; - for (size_t x = 0; x < MaxProtos; x++) { - if (interface == ProtocolMap::instance().protos_[x].iface) { - const char* proto_name = - proto_to_str(ProtocolMap::instance().protos_[x].protocol); + for (size_t n_proto = 0; n_proto < MaxProtos; n_proto++) { + if (protos_[n_proto].protocol == Proto_None) { + continue; + } + + if (interface == ProtocolMap::instance().protos_[n_proto].iface) { + const char* proto_name = protos_[n_proto].scheme_name; if (!list.find(proto_name)) { if (!list.push_back(proto_name)) { return false; } } - protocolsExist = true; + protocols_exist = true; } } - return protocolsExist; + return protocols_exist; +} + +void ProtocolMap::add_proto_(const ProtocolAttrs& proto) { + roc_panic_if((int)proto.protocol < 0); + roc_panic_if((int)proto.protocol >= MaxProtos); + roc_panic_if(protos_[proto.protocol].protocol != 0); + + protos_[proto.protocol] = proto; } } // namespace address diff --git a/src/internal_modules/roc_address/protocol_map.h b/src/internal_modules/roc_address/protocol_map.h index 202e1c3682..c72327c418 100644 --- a/src/internal_modules/roc_address/protocol_map.h +++ b/src/internal_modules/roc_address/protocol_map.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Roc Streaming authors + * Copyright (c) 2023 Roc Streaming authors * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this @@ -68,10 +68,10 @@ class ProtocolMap : public core::NonCopyable<> { //! Get protocol attributes by scheme name. const ProtocolAttrs* find_by_scheme(const char* scheme) const; - //! Get list of interfaces with at least one protocol + //! Get list of interfaces with at least one protocol. ROC_ATTR_NODISCARD bool get_supported_interfaces(core::Array&); - //! Get all supported protocols + //! Get all supported protocols. ROC_ATTR_NODISCARD bool get_supported_protocols(Interface, core::StringList&); private: diff --git a/src/internal_modules/roc_sndio/print_supported.cpp b/src/internal_modules/roc_sndio/print_supported.cpp index d56b9b9c96..f397000902 100644 --- a/src/internal_modules/roc_sndio/print_supported.cpp +++ b/src/internal_modules/roc_sndio/print_supported.cpp @@ -16,7 +16,7 @@ namespace sndio { namespace { -enum { ArraySize = 100, LineSize = 70 }; +enum { LineSize = 70 }; void print_string_list(core::Printer& prn, const core::StringList& list,