Adapted for unsigned char buffers
This commit is contained in:
parent
54a063d5e6
commit
27ba379101
1 changed files with 67 additions and 4 deletions
|
@ -29,13 +29,14 @@
|
||||||
|
|
||||||
namespace file_reader
|
namespace file_reader
|
||||||
{
|
{
|
||||||
|
template <typename T>
|
||||||
struct parsing_buffer
|
struct parsing_buffer
|
||||||
{
|
{
|
||||||
parsing_buffer(buffer<char> &b)
|
parsing_buffer(buffer<T> &b)
|
||||||
:buf(b)
|
:buf(b)
|
||||||
{}
|
{}
|
||||||
buffer<char> &buf;
|
buffer<T> &buf;
|
||||||
char *point;
|
T *point;
|
||||||
int consumed_size;
|
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 <>
|
template <>
|
||||||
inline void read_type<float>(float &val, char *data)
|
inline void read_type<float>(float &val, char *data)
|
||||||
{
|
{
|
||||||
|
@ -71,6 +94,16 @@ namespace file_reader
|
||||||
memcpy(&val, &num_as_uint32, sizeof(float));
|
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 <>
|
template <>
|
||||||
inline void read_type<double>(double &val, char *data)
|
inline void read_type<double>(double &val, char *data)
|
||||||
{
|
{
|
||||||
|
@ -81,6 +114,16 @@ namespace file_reader
|
||||||
memcpy(&val, &num_as_uint64, sizeof(double));
|
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<>
|
template<>
|
||||||
inline void read_type<buffer<char>>(buffer<char> &val, char *data)
|
inline void read_type<buffer<char>>(buffer<char> &val, char *data)
|
||||||
{
|
{
|
||||||
|
@ -92,7 +135,7 @@ namespace file_reader
|
||||||
template<typename T>
|
template<typename T>
|
||||||
struct read_var
|
struct read_var
|
||||||
{
|
{
|
||||||
static void call(T& val, parsing_buffer* v)
|
static void call(T& val, parsing_buffer<char> * v)
|
||||||
{
|
{
|
||||||
int size = 0;
|
int size = 0;
|
||||||
if constexpr (typeid(T) == typeid(buffer<char>))
|
if constexpr (typeid(T) == typeid(buffer<char>))
|
||||||
|
@ -112,6 +155,26 @@ namespace file_reader
|
||||||
v->point += size;
|
v->point += size;
|
||||||
v->consumed_size += 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;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue