Skip to content

Commit 2109511

Browse files
committed
fdsdump: add extra methods to FieldView
1 parent 23a0051 commit 2109511

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/tools/fdsdump/src/common/fieldView.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#include <common/fieldView.hpp>
66

7+
#include <libfds/converters.h>
8+
79
namespace fdsdump {
810

911
uint64_t
@@ -115,4 +117,35 @@ FieldView::as_ipaddr() const
115117
throw std::invalid_argument("Conversion error (ipaddr)");
116118
}
117119

120+
std::string
121+
FieldView::as_string() const
122+
{
123+
std::string value;
124+
125+
if (m_field.size > 0) {
126+
// &value[0] doesn't work for zero-sized strings, so we have to handle this specially
127+
value.resize(m_field.size);
128+
int ret = fds_get_string(m_field.data, m_field.size, &value[0]);
129+
if (ret != FDS_OK) {
130+
throw std::invalid_argument("Conversion error (string)");
131+
}
132+
}
133+
134+
return value;
135+
}
136+
137+
std::vector<uint8_t>
138+
FieldView::as_bytes() const
139+
{
140+
std::vector<uint8_t> value;
141+
142+
value.resize(m_field.size);
143+
int ret = fds_get_octet_array(m_field.data, m_field.size, value.data());
144+
if (ret != FDS_OK) {
145+
throw std::invalid_argument("Conversion error (bytes)");
146+
}
147+
148+
return value;
149+
}
150+
118151
} // fdsdump

src/tools/fdsdump/src/common/fieldView.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11

22
#pragma once
33

4+
#include <cstdint>
45
#include <ctime>
6+
#include <string>
7+
#include <vector>
58

69
#include <libfds.h>
710

@@ -21,6 +24,8 @@ class FieldView {
2124
struct timespec as_datetime() const;
2225
uint64_t as_datetime_ms() const;
2326
IPAddr as_ipaddr() const;
27+
std::string as_string() const;
28+
std::vector<uint8_t> as_bytes() const;
2429

2530
private:
2631
const struct fds_drec_field &m_field;

0 commit comments

Comments
 (0)