Repository: SerenityOS/serenity
File: Userland/Utilities/lspci.cpp (C++)

Code before:
 */

#include <AK/ByteString.h>
#include <AK/Hex.h>
#include <AK/JsonArray.h>
#include <AK/JsonObject.h>
#include <AK/StringUtils.h>
#include <AK/StringView.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/DirIterator.h>
#include <LibCore/File.h>
#include <LibCore/System.h>
#include <LibMain/Main.h>
#include <LibPCIDB/Database.h>

static bool flag_show_numerical = false;
static bool flag_verbose = false;

static constexpr StringView format_numerical = "{:04x}:{:02x}:{:02x}.{} {}: {}:{} (rev {:02x})"sv;
static constexpr StringView format_textual = "{:04x}:{:02x}:{:02x}.{} {}: {} {} (rev {:02x})"sv;
static constexpr StringView format_region = "\tBAR {}: {} region @ {:#x}"sv;

static ErrorOr<u32> read_hex_string_from_bytebuffer(ByteBuffer const& buf)
{
    auto slice_result = buf.slice(2, buf.size() - 2);
    if (slice_result.is_error())
        return slice_result.release_error();

    auto hex_string = ByteString(slice_result.value().bytes());
    auto result = AK::StringUtils::convert_to_uint_from_hex(hex_string);
    if (!result.has_value())
        return Error::from_string_literal("Failed to convert hex string to number");

    return result.release_value();
}

static u32 convert_sysfs_value_to_uint(ByteString const& value)
{
    if (auto result = AK::StringUtils::convert_to_uint_from_hex(value); result.has_value())
        return result.release_value();
    if (auto result = AK::StringUtils::convert_to_uint(value); result.has_value())
        return result.release_value();
    VERIFY_NOT_REACHED();
}

ErrorOr<int> serenity_main(Main::Arguments arguments)
{
    TRY(Core::System::pledge("stdio rpath"));

    Core::ArgsParser args_parser;
    args_parser.set_general_help("List PCI devices.");

Review comment (suggestion):
Please use `TRY()` instead of manually expanding it.

Code after:
 */

#include <AK/ByteString.h>
#include <AK/Hex.h>
#include <AK/JsonArray.h>
#include <AK/JsonObject.h>
#include <AK/StringUtils.h>
#include <AK/StringView.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/DirIterator.h>
#include <LibCore/File.h>
#include <LibCore/System.h>
#include <LibMain/Main.h>
#include <LibPCIDB/Database.h>

static bool flag_show_numerical = false;
static bool flag_verbose = false;

static constexpr StringView format_numerical = "{:04x}:{:02x}:{:02x}.{} {}: {}:{} (rev {:02x})"sv;
static constexpr StringView format_textual = "{:04x}:{:02x}:{:02x}.{} {}: {} {} (rev {:02x})"sv;
static constexpr StringView format_region = "\tBAR {}: {} region @ {:#x}"sv;

static ErrorOr<u32> read_hex_string_from_bytebuffer(ByteBuffer const& buf)
{
    auto slice_result = TRY(buf.slice(2, buf.size() - 2));
    auto hex_string = ByteString(slice_result.bytes());
    auto result = AK::StringUtils::convert_to_uint_from_hex(hex_string);
    if (!result.has_value())
        return Error::from_string_literal("Failed to convert hex string to number");

    return result.release_value();
}

static u32 convert_sysfs_value_to_uint(ByteString const& value)
{
    if (auto result = AK::StringUtils::convert_to_uint_from_hex(value); result.has_value())
        return result.release_value();
    if (auto result = AK::StringUtils::convert_to_uint(value); result.has_value())
        return result.release_value();
    VERIFY_NOT_REACHED();
}

ErrorOr<int> serenity_main(Main::Arguments arguments)
{
    TRY(Core::System::pledge("stdio rpath"));

    Core::ArgsParser args_parser;
    args_parser.set_general_help("List PCI devices.");
    args_parser.add_option(flag_show_numerical, "Show numerical IDs", "numerical", 'n');
    args_parser.add_option(flag_verbose, "Show verbose info on devices", "verbose", 'v');
    args_parser.parse(arguments);