mirror of
https://github.com/RGBCube/serenity
synced 2025-07-23 04:47:36 +00:00
AK+Kernel: Return KString from MACAddress::to_string() in the Kernel
This lets us safely handle allocation failure.
This commit is contained in:
parent
6098ffa120
commit
9277d2dce2
2 changed files with 17 additions and 3 deletions
|
@ -9,10 +9,15 @@
|
||||||
#include <AK/AllOf.h>
|
#include <AK/AllOf.h>
|
||||||
#include <AK/Array.h>
|
#include <AK/Array.h>
|
||||||
#include <AK/Assertions.h>
|
#include <AK/Assertions.h>
|
||||||
#include <AK/String.h>
|
|
||||||
#include <AK/Types.h>
|
#include <AK/Types.h>
|
||||||
#include <AK/Vector.h>
|
#include <AK/Vector.h>
|
||||||
|
|
||||||
|
#ifdef KERNEL
|
||||||
|
# include <Kernel/KString.h>
|
||||||
|
#else
|
||||||
|
# include <AK/String.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
class [[gnu::packed]] MACAddress {
|
class [[gnu::packed]] MACAddress {
|
||||||
static constexpr size_t s_mac_address_length = 6u;
|
static constexpr size_t s_mac_address_length = 6u;
|
||||||
|
|
||||||
|
@ -53,10 +58,17 @@ public:
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef KERNEL
|
||||||
|
ErrorOr<NonnullOwnPtr<Kernel::KString>> to_string() const
|
||||||
|
{
|
||||||
|
return Kernel::KString::formatted("{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}", m_data[0], m_data[1], m_data[2], m_data[3], m_data[4], m_data[5]);
|
||||||
|
}
|
||||||
|
#else
|
||||||
String to_string() const
|
String to_string() const
|
||||||
{
|
{
|
||||||
return String::formatted("{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}", m_data[0], m_data[1], m_data[2], m_data[3], m_data[4], m_data[5]);
|
return String::formatted("{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}", m_data[0], m_data[1], m_data[2], m_data[3], m_data[4], m_data[5]);
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
static Optional<MACAddress> from_string(StringView string)
|
static Optional<MACAddress> from_string(StringView string)
|
||||||
{
|
{
|
||||||
|
|
|
@ -45,7 +45,8 @@ private:
|
||||||
auto obj = array.add_object();
|
auto obj = array.add_object();
|
||||||
obj.add("name", adapter.name());
|
obj.add("name", adapter.name());
|
||||||
obj.add("class_name", adapter.class_name());
|
obj.add("class_name", adapter.class_name());
|
||||||
obj.add("mac_address", adapter.mac_address().to_string());
|
auto mac_address = adapter.mac_address().to_string().release_value_but_fixme_should_propagate_errors();
|
||||||
|
obj.add("mac_address", mac_address->view());
|
||||||
if (!adapter.ipv4_address().is_zero()) {
|
if (!adapter.ipv4_address().is_zero()) {
|
||||||
auto ipv4_address = adapter.ipv4_address().to_string().release_value_but_fixme_should_propagate_errors();
|
auto ipv4_address = adapter.ipv4_address().to_string().release_value_but_fixme_should_propagate_errors();
|
||||||
obj.add("ipv4_address", ipv4_address->view());
|
obj.add("ipv4_address", ipv4_address->view());
|
||||||
|
@ -81,7 +82,8 @@ private:
|
||||||
JsonArraySerializer array { builder };
|
JsonArraySerializer array { builder };
|
||||||
arp_table().for_each([&](const auto& it) {
|
arp_table().for_each([&](const auto& it) {
|
||||||
auto obj = array.add_object();
|
auto obj = array.add_object();
|
||||||
obj.add("mac_address", it.value.to_string());
|
auto mac_address = it.value.to_string().release_value_but_fixme_should_propagate_errors();
|
||||||
|
obj.add("mac_address", mac_address->view());
|
||||||
auto ip_address = it.key.to_string().release_value_but_fixme_should_propagate_errors();
|
auto ip_address = it.key.to_string().release_value_but_fixme_should_propagate_errors();
|
||||||
obj.add("ip_address", ip_address->view());
|
obj.add("ip_address", ip_address->view());
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue