Skip to content

Commit 137add2

Browse files
Lukas HutakLukas955
authored andcommitted
fdsdump: fieldView: define component
1 parent 400d24e commit 137add2

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
2+
3+
#include <stdexcept>
4+
5+
#include "fieldView.hpp"
6+
7+
uint64_t
8+
FieldView::as_uint() const
9+
{
10+
uint64_t result;
11+
int ret;
12+
13+
ret = fds_get_uint_be(m_field.data, m_field.size, &result);
14+
if (ret != FDS_OK) {
15+
throw std::invalid_argument("Field value conversion error (unsinged)");
16+
}
17+
18+
return result;
19+
}
20+
21+
int64_t
22+
FieldView::as_int() const
23+
{
24+
int64_t result;
25+
int ret;
26+
27+
ret = fds_get_int_be(m_field.data, m_field.size, &result);
28+
if (ret != FDS_OK) {
29+
throw std::invalid_argument("Conversion error (signed)");
30+
}
31+
32+
return result;
33+
}
34+
35+
double
36+
FieldView::as_float() const
37+
{
38+
double result;
39+
int ret;
40+
41+
ret = fds_get_float_be(m_field.data, m_field.size, &result);
42+
if (ret != FDS_OK) {
43+
throw std::invalid_argument("Conversion error (float)");
44+
}
45+
46+
return result;
47+
}
48+
49+
bool
50+
FieldView::as_bool() const
51+
{
52+
bool result;
53+
int ret;
54+
55+
ret = fds_get_bool(m_field.data, m_field.size, &result);
56+
if (ret != FDS_OK) {
57+
throw std::invalid_argument("Conversion error (boolean)");
58+
}
59+
60+
return result;
61+
}
62+
63+
struct timespec
64+
FieldView::as_datetime() const
65+
{
66+
const struct fds_iemgr_elem *elem = m_field.info->def;
67+
struct timespec result;
68+
int ret;
69+
70+
if (!elem) {
71+
throw std::invalid_argument("Conversion error (undefined datetime type)");
72+
}
73+
74+
ret = fds_get_datetime_hp_be(m_field.data, m_field.size, elem->data_type, &result);
75+
if (ret != FDS_OK) {
76+
throw std::invalid_argument("Conversion error (datetime)");
77+
}
78+
79+
return result;
80+
}
81+
82+
IPAddr
83+
FieldView::as_ipaddr() const
84+
{
85+
if (m_field.size == 4U) {
86+
const uint32_t *value = reinterpret_cast<uint32_t *>(m_field.data);
87+
return IPAddr(*value);
88+
}
89+
90+
if (m_field.size == 16U) {
91+
return IPAddr(m_field.data);
92+
}
93+
94+
throw std::invalid_argument("Conversion error (ipaddr)");
95+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
#pragma once
3+
4+
#include <ctime>
5+
6+
#include <libfds.h>
7+
8+
#include "ipaddr.hpp"
9+
10+
class FieldView {
11+
public:
12+
FieldView(const struct fds_drec_field &field)
13+
: m_field(field) {};
14+
15+
uint64_t as_uint() const;
16+
int64_t as_int() const;
17+
double as_float() const;
18+
bool as_bool() const;
19+
struct timespec as_datetime() const;
20+
IPAddr as_ipaddr() const;
21+
22+
private:
23+
const struct fds_drec_field &m_field;
24+
};

0 commit comments

Comments
 (0)