Namespaced everything

This commit is contained in:
Luna 2025-07-18 07:16:38 +02:00
parent ec3d796774
commit 5cdd4f8835
3 changed files with 170 additions and 160 deletions

View file

@ -4,80 +4,83 @@
#include <cstring> #include <cstring>
#define BUFFER_SIZE 1024 #define BUFFER_SIZE 1024
template<typename T> namespace file_reader
struct buffer
{ {
buffer() template<typename T>
struct buffer
{ {
data = nullptr; buffer()
size = 0; {
allocated = 0; data = nullptr;
allocations = 0; size = 0;
allocated = 0;
allocations = 0;
}
T *data;
size_t size;
size_t allocated;
int allocations;
void write(T *data_in, size_t data_size);
void remove(int offset, int remove_size);
void allocate(size_t s);
buffer(const buffer&) = delete;
~buffer()
{
if (allocated > 0)
free(data);
}
};
template<typename T>
void buffer<T>::write(T *data_in, size_t data_size)
{
if (data_size > allocated - size)
{
allocated += (size + data_size) + BUFFER_SIZE;
data = (T *)realloc(data, allocated);
allocations++;
if (!data)
{
throw std::runtime_error("Allocation failed");
}
}
std::memcpy(&data[size], data_in, data_size);
size += data_size;
} }
T *data;
size_t size;
size_t allocated;
int allocations;
void write(T *data_in, size_t data_size); template<typename T>
void remove(int offset, int remove_size); void buffer<T>::remove(int offset, int remove_size)
void allocate(size_t s);
buffer(const buffer&) = delete;
~buffer()
{ {
if (allocated > 0) if (offset + remove_size > size)
free(data); return ;
} T *new_data = (T *)calloc(allocated, sizeof(T));
}; if (!new_data)
{
template<typename T> throw std::runtime_error("Allocation failed");
void buffer<T>::write(T *data_in, size_t data_size) }
{ if (offset > 0)
if (data_size > allocated - size) {
{ std::memcpy(new_data, data, offset);
allocated += (size + data_size) + BUFFER_SIZE; }
data = (T *)realloc(data, allocated); int new_size = size - remove_size;
std::memcpy(&new_data[offset], &data[offset + remove_size], new_size - offset);
free(data);
data = new_data;
size = new_size;
allocations++; allocations++;
}
template<typename T>
void buffer<T>::allocate(size_t s)
{
data = (T *)realloc(data, allocated + s);
if (!data) if (!data)
{ {
throw std::runtime_error("Allocation failed"); throw std::runtime_error("Allocation failed");
} }
allocations++;
allocated += s;
} }
std::memcpy(&data[size], data_in, data_size);
size += data_size;
}
template<typename T>
void buffer<T>::remove(int offset, int remove_size)
{
if (offset + remove_size > size)
return ;
T *new_data = (T *)calloc(allocated, sizeof(T));
if (!new_data)
{
throw std::runtime_error("Allocation failed");
}
if (offset > 0)
{
std::memcpy(new_data, data, offset);
}
int new_size = size - remove_size;
std::memcpy(&new_data[offset], &data[offset + remove_size], new_size - offset);
free(data);
data = new_data;
size = new_size;
allocations++;
}
template<typename T>
void buffer<T>::allocate(size_t s)
{
data = (T *)realloc(data, allocated + s);
if (!data)
{
throw std::runtime_error("Allocation failed");
}
allocations++;
allocated += s;
} }

View file

@ -4,7 +4,7 @@
#include <print> #include <print>
#include <type_traits> #include <type_traits>
#include "buffer.h" #include "buffer.h"
#define read_comp(size, ptr, t) const_for_<size>([&](auto i){read_var<std::tuple_element_t<i.value, std::remove_cvref_t<decltype(t)>>>::call(std::get<i.value>(t), &ptr);}); #define read_comp(size, ptr, t) ::file_reader::const_for_<size>([&](auto i){::file_reader::read_var<std::tuple_element_t<i.value, std::remove_cvref_t<decltype(t)>>>::call(std::get<i.value>(t), &ptr);});
#ifdef __APPLE__ #ifdef __APPLE__
#include <libkern/OSByteOrder.h> #include <libkern/OSByteOrder.h>
@ -26,99 +26,103 @@
#define le64toh(x) OSSwapLittleToHostInt64(x) #define le64toh(x) OSSwapLittleToHostInt64(x)
#endif #endif
struct parsing_buffer
{
parsing_buffer(buffer<char> &b)
:buf(b)
{}
buffer<char> &buf;
char *point;
int consumed_size;
};
template <typename T> namespace file_reader
static void read_type(T& val, char *data)
{ {
std::memcpy(&val, data, sizeof(T)); struct parsing_buffer
switch (sizeof(T))
{ {
case 1: parsing_buffer(buffer<char> &b)
break; :buf(b)
case 2: {}
val = be16toh(val); buffer<char> &buf;
break; char *point;
case 4: int consumed_size;
val = be32toh(val); };
break;
case 8:
val = be64toh(val);
break;
default:
std::println("This integer is not supported!");
}
}
template <> template <typename T>
inline void read_type<float>(float &val, char *data) static void read_type(T& val, 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)
{
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)
{
val.allocated = val.size + 1;
val.data = (char *)calloc(val.size + 1,sizeof(char));
std::memcpy(val.data, data, val.size);
}
template<typename T>
struct read_var
{
static void call(T& val, parsing_buffer* v)
{ {
int size = 0; std::memcpy(&val, data, sizeof(T));
if constexpr (typeid(T) == typeid(buffer<char>)) switch (sizeof(T))
size = val.size;
else
size = sizeof(T);
if (size + v->consumed_size > v->buf.size)
{ {
if constexpr (typeid(T) == typeid(buffer<char>)) case 1:
{ break;
val.size = v->buf.size; case 2:
} val = be16toh(val);
else break;
return ; case 4:
val = be32toh(val);
break;
case 8:
val = be64toh(val);
break;
default:
std::println("This integer is not supported!");
} }
read_type<T>(val, v->point);
v->point += size;
v->consumed_size += size;
} }
};
template <>
inline void read_type<float>(float &val, 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)
{
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)
{
val.allocated = val.size + 1;
val.data = (char *)calloc(val.size + 1,sizeof(char));
std::memcpy(val.data, data, val.size);
}
template<typename T>
struct read_var
{
static void call(T& val, parsing_buffer* v)
{
int size = 0;
if constexpr (typeid(T) == typeid(buffer<char>))
size = val.size;
else
size = sizeof(T);
if (size + v->consumed_size > v->buf.size)
{
if constexpr (typeid(T) == typeid(buffer<char>))
{
val.size = v->buf.size;
}
else
return ;
}
read_type<T>(val, v->point);
v->point += size;
v->consumed_size += size;
}
};
template <typename Integer, Integer ...I, typename F> constexpr void const_for_each_(std::integer_sequence<Integer, I...>, F&& func) template <typename Integer, Integer ...I, typename F> constexpr void const_for_each_(std::integer_sequence<Integer, I...>, F&& func)
{ {
(func(std::integral_constant<Integer, I>{}), ...); (func(std::integral_constant<Integer, I>{}), ...);
} }
template <auto N, typename F> constexpr void const_for_(F&& func) template <auto N, typename F> constexpr void const_for_(F&& func)
{ {
if constexpr (N > 0) if constexpr (N > 0)
const_for_each_(std::make_integer_sequence<decltype(N), N>{}, std::forward<F>(func)); const_for_each_(std::make_integer_sequence<decltype(N), N>{}, std::forward<F>(func));
}
} }

View file

@ -5,15 +5,17 @@
#include <print> #include <print>
#include <fcntl.h> #include <fcntl.h>
enum RESULT namespace file_reader
{ {
READ_CORRECT, //read was as expected enum RESULT
READ_INCOMPLETE, //read is incomplete (probably because of the file ending) {
READ_FILE_ENDED //cannot read because there's not more file to read READ_CORRECT, //read was as expected
}; READ_INCOMPLETE, //read is incomplete (probably because of the file ending)
READ_FILE_ENDED //cannot read because there's not more file to read
};
class file_reader class file_reader
{ {
public: public:
explicit file_reader(int file_fd) explicit file_reader(int file_fd)
:fd(file_fd) :fd(file_fd)
@ -75,7 +77,7 @@ class file_reader
return std::make_pair(0, READ_FILE_ENDED); return std::make_pair(0, READ_FILE_ENDED);
constexpr std::size_t size_tuple = sizeof...(T); constexpr std::size_t size_tuple = sizeof...(T);
int size = 0; int size = 0;
size_t total_read_size = 0; size_t total_read_size = 0;
const_for_<size_tuple>([&](auto i) const_for_<size_tuple>([&](auto i)
{ {
@ -192,4 +194,5 @@ class file_reader
int fd; int fd;
off_t file_size; off_t file_size;
off_t file_size_remaining; off_t file_size_remaining;
}; };
}