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>
#define BUFFER_SIZE 1024
template<typename T>
struct buffer
namespace file_reader
{
template<typename T>
struct buffer
{
buffer()
{
data = nullptr;
@ -29,11 +31,11 @@ struct buffer
if (allocated > 0)
free(data);
}
};
};
template<typename T>
void buffer<T>::write(T *data_in, size_t data_size)
{
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;
@ -46,11 +48,11 @@ void buffer<T>::write(T *data_in, size_t data_size)
}
std::memcpy(&data[size], data_in, data_size);
size += data_size;
}
}
template<typename T>
void buffer<T>::remove(int offset, int remove_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));
@ -68,11 +70,11 @@ void buffer<T>::remove(int offset, int remove_size)
data = new_data;
size = new_size;
allocations++;
}
}
template<typename T>
void buffer<T>::allocate(size_t s)
{
template<typename T>
void buffer<T>::allocate(size_t s)
{
data = (T *)realloc(data, allocated + s);
if (!data)
{
@ -80,4 +82,5 @@ void buffer<T>::allocate(size_t s)
}
allocations++;
allocated += s;
}
}

View file

@ -4,7 +4,7 @@
#include <print>
#include <type_traits>
#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__
#include <libkern/OSByteOrder.h>
@ -26,19 +26,22 @@
#define le64toh(x) OSSwapLittleToHostInt64(x)
#endif
struct parsing_buffer
namespace file_reader
{
struct parsing_buffer
{
parsing_buffer(buffer<char> &b)
:buf(b)
{}
buffer<char> &buf;
char *point;
int consumed_size;
};
};
template <typename T>
static void read_type(T& val, char *data)
{
template <typename T>
static void read_type(T& val, char *data)
{
std::memcpy(&val, data, sizeof(T));
switch (sizeof(T))
{
@ -56,39 +59,39 @@ static void read_type(T& val, char *data)
default:
std::println("This integer is not supported!");
}
}
}
template <>
inline void read_type<float>(float &val, char *data)
{
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)
{
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)
{
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
{
template<typename T>
struct read_var
{
static void call(T& val, parsing_buffer* v)
{
int size = 0;
@ -109,16 +112,17 @@ struct read_var
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>{}), ...);
}
}
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)
const_for_each_(std::make_integer_sequence<decltype(N), N>{}, std::forward<F>(func));
}
}

View file

@ -5,15 +5,17 @@
#include <print>
#include <fcntl.h>
enum RESULT
namespace file_reader
{
enum RESULT
{
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:
explicit file_reader(int file_fd)
:fd(file_fd)
@ -192,4 +194,5 @@ class file_reader
int fd;
off_t file_size;
off_t file_size_remaining;
};
};
}