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,9 +4,11 @@
#include <cstring> #include <cstring>
#define BUFFER_SIZE 1024 #define BUFFER_SIZE 1024
template<typename T> namespace file_reader
struct buffer
{ {
template<typename T>
struct buffer
{
buffer() buffer()
{ {
data = nullptr; data = nullptr;
@ -29,11 +31,11 @@ struct buffer
if (allocated > 0) if (allocated > 0)
free(data); free(data);
} }
}; };
template<typename T> template<typename T>
void buffer<T>::write(T *data_in, size_t data_size) void buffer<T>::write(T *data_in, size_t data_size)
{ {
if (data_size > allocated - size) if (data_size > allocated - size)
{ {
allocated += (size + data_size) + BUFFER_SIZE; allocated += (size + data_size) + BUFFER_SIZE;
@ -46,11 +48,11 @@ void buffer<T>::write(T *data_in, size_t data_size)
} }
std::memcpy(&data[size], data_in, data_size); std::memcpy(&data[size], data_in, data_size);
size += data_size; size += data_size;
} }
template<typename T> template<typename T>
void buffer<T>::remove(int offset, int remove_size) void buffer<T>::remove(int offset, int remove_size)
{ {
if (offset + remove_size > size) if (offset + remove_size > size)
return ; return ;
T *new_data = (T *)calloc(allocated, sizeof(T)); T *new_data = (T *)calloc(allocated, sizeof(T));
@ -68,11 +70,11 @@ void buffer<T>::remove(int offset, int remove_size)
data = new_data; data = new_data;
size = new_size; size = new_size;
allocations++; allocations++;
} }
template<typename T> template<typename T>
void buffer<T>::allocate(size_t s) void buffer<T>::allocate(size_t s)
{ {
data = (T *)realloc(data, allocated + s); data = (T *)realloc(data, allocated + s);
if (!data) if (!data)
{ {
@ -80,4 +82,5 @@ void buffer<T>::allocate(size_t s)
} }
allocations++; allocations++;
allocated += s; 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,19 +26,22 @@
#define le64toh(x) OSSwapLittleToHostInt64(x) #define le64toh(x) OSSwapLittleToHostInt64(x)
#endif #endif
struct parsing_buffer
namespace file_reader
{ {
struct parsing_buffer
{
parsing_buffer(buffer<char> &b) parsing_buffer(buffer<char> &b)
:buf(b) :buf(b)
{} {}
buffer<char> &buf; buffer<char> &buf;
char *point; char *point;
int consumed_size; int consumed_size;
}; };
template <typename T> template <typename T>
static void read_type(T& val, char *data) static void read_type(T& val, char *data)
{ {
std::memcpy(&val, data, sizeof(T)); std::memcpy(&val, data, sizeof(T));
switch (sizeof(T)) switch (sizeof(T))
{ {
@ -56,39 +59,39 @@ static void read_type(T& val, char *data)
default: default:
std::println("This integer is not supported!"); 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)
{ {
uint32_t num_as_uint32; uint32_t num_as_uint32;
memcpy(&num_as_uint32, data, sizeof(float)); memcpy(&num_as_uint32, data, sizeof(float));
num_as_uint32 = be32toh(num_as_uint32); num_as_uint32 = be32toh(num_as_uint32);
memcpy(&val, &num_as_uint32, sizeof(float)); 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)
{ {
uint64_t num_as_uint64; uint64_t num_as_uint64;
memcpy(&num_as_uint64, data, sizeof(double)); memcpy(&num_as_uint64, data, sizeof(double));
num_as_uint64 = be64toh(num_as_uint64); num_as_uint64 = be64toh(num_as_uint64);
memcpy(&val, &num_as_uint64, sizeof(double)); 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)
{ {
val.allocated = val.size + 1; val.allocated = val.size + 1;
val.data = (char *)calloc(val.size + 1,sizeof(char)); val.data = (char *)calloc(val.size + 1,sizeof(char));
std::memcpy(val.data, data, val.size); std::memcpy(val.data, data, val.size);
} }
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* v)
{ {
int size = 0; int size = 0;
@ -109,16 +112,17 @@ struct read_var
v->point += size; v->point += size;
v->consumed_size += 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
{ {
enum RESULT
{
READ_CORRECT, //read was as expected READ_CORRECT, //read was as expected
READ_INCOMPLETE, //read is incomplete (probably because of the file ending) 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_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)
@ -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;
}; };
}