Adapted for unsigned char buffers

This commit is contained in:
luna 2025-07-25 05:40:26 +02:00
parent 54a063d5e6
commit 27ba379101

View file

@ -29,13 +29,14 @@
namespace file_reader
{
template <typename T>
struct parsing_buffer
{
parsing_buffer(buffer<char> &b)
parsing_buffer(buffer<T> &b)
:buf(b)
{}
buffer<char> &buf;
char *point;
buffer<T> &buf;
T *point;
int consumed_size;
};
@ -61,6 +62,28 @@ namespace file_reader
}
}
template <typename T>
static void read_type(T& val, unsigned char *data)
{
std::memcpy(&val, data, sizeof(T));
switch (sizeof(T))
{
case 1:
break;
case 2:
val = be16toh(val);
break;
case 4:
val = be32toh(val);
break;
case 8:
val = be64toh(val);
break;
default:
std::println("This integer is not supported!");
}
}
template <>
inline void read_type<float>(float &val, char *data)
{
@ -71,6 +94,16 @@ namespace file_reader
memcpy(&val, &num_as_uint32, sizeof(float));
}
template <>
inline void read_type<float>(float &val, unsigned char *data)
{
uint32_t num_as_uint32;
memcpy(&num_as_uint32, data, sizeof(float));
num_as_uint32 = be32toh(num_as_uint32);
memcpy(&val, &num_as_uint32, sizeof(float));
}
template <>
inline void read_type<double>(double &val, char *data)
{
@ -81,6 +114,16 @@ namespace file_reader
memcpy(&val, &num_as_uint64, sizeof(double));
}
template <>
inline void read_type<double>(double &val, unsigned char *data)
{
uint64_t num_as_uint64;
memcpy(&num_as_uint64, data, sizeof(double));
num_as_uint64 = be64toh(num_as_uint64);
memcpy(&val, &num_as_uint64, sizeof(double));
}
template<>
inline void read_type<buffer<char>>(buffer<char> &val, char *data)
{
@ -92,7 +135,7 @@ namespace file_reader
template<typename T>
struct read_var
{
static void call(T& val, parsing_buffer* v)
static void call(T& val, parsing_buffer<char> * v)
{
int size = 0;
if constexpr (typeid(T) == typeid(buffer<char>))
@ -112,6 +155,26 @@ namespace file_reader
v->point += size;
v->consumed_size += size;
}
static void call(T& val, parsing_buffer<unsigned char> * v)
{
int size = 0;
if constexpr (typeid(T) == typeid(buffer<unsigned char>))
size = val.size;
else
size = sizeof(T);
if (size + v->consumed_size > v->buf.size)
{
if constexpr (typeid(T) == typeid(buffer<unsigned char>))
{
val.size = v->buf.size;
}
else
return ;
}
read_type<T>(val, v->point);
v->point += size;
v->consumed_size += size;
}
};