Barely functional now (i think)
This commit is contained in:
parent
80a525dbdb
commit
92315413b2
7 changed files with 151 additions and 82 deletions
|
@ -3,8 +3,7 @@ project(file_read)
|
||||||
|
|
||||||
set(CMAKE_CXX_STANDARD 23)
|
set(CMAKE_CXX_STANDARD 23)
|
||||||
|
|
||||||
add_library(file_read STATIC file_read.cpp
|
add_library(file_read STATIC
|
||||||
file_deserialising.cpp
|
|
||||||
file_deserialising.h
|
file_deserialising.h
|
||||||
buffer.cpp
|
buffer.cpp
|
||||||
buffer.h)
|
buffer.h)
|
||||||
|
|
|
@ -10,9 +10,9 @@ void buffer::write(char *data_in, int data_size)
|
||||||
{
|
{
|
||||||
throw std::runtime_error("Allocation failed");
|
throw std::runtime_error("Allocation failed");
|
||||||
}
|
}
|
||||||
std::memcpy(&data[size], data_in, data_size);
|
|
||||||
size += data_size;
|
|
||||||
}
|
}
|
||||||
|
std::memcpy(&data[size], data_in, data_size);
|
||||||
|
size += data_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
void buffer::remove(int offset, int remove_size)
|
void buffer::remove(int offset, int remove_size)
|
||||||
|
|
2
buffer.h
2
buffer.h
|
@ -2,7 +2,7 @@
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#define BUFFER_SIZE 1092
|
#define BUFFER_SIZE 1024
|
||||||
|
|
||||||
struct buffer
|
struct buffer
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,66 +0,0 @@
|
||||||
#include "file_deserialising.h"
|
|
||||||
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
T read_type(char *data)
|
|
||||||
{
|
|
||||||
T a;
|
|
||||||
|
|
||||||
std::memcpy(&a, data, sizeof(T));
|
|
||||||
|
|
||||||
switch (sizeof(T))
|
|
||||||
{
|
|
||||||
case 1:
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
a = be16toh(a);
|
|
||||||
break;
|
|
||||||
case 4:
|
|
||||||
a = be32toh(a);
|
|
||||||
break;
|
|
||||||
case 8:
|
|
||||||
a = be64toh(a);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
std::println("This integer is not supported!");
|
|
||||||
}
|
|
||||||
return a;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <>
|
|
||||||
float read_type<float>(char *data)
|
|
||||||
{
|
|
||||||
uint32_t num_as_uint32;
|
|
||||||
float num;
|
|
||||||
|
|
||||||
memcpy(&num_as_uint32, data, sizeof(uint32_t));
|
|
||||||
num_as_uint32 = be32toh(num_as_uint32);
|
|
||||||
memcpy(&num, &num_as_uint32, sizeof(float));
|
|
||||||
|
|
||||||
return num;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <>
|
|
||||||
double read_type<double>(char *data)
|
|
||||||
{
|
|
||||||
uint64_t num_as_uint64;
|
|
||||||
double num;
|
|
||||||
|
|
||||||
memcpy(&num_as_uint64, data, sizeof(uint64_t));
|
|
||||||
num_as_uint64 = be64toh(num_as_uint64);
|
|
||||||
memcpy(&num, &num_as_uint64, sizeof(double));
|
|
||||||
|
|
||||||
return num;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
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)
|
|
||||||
{
|
|
||||||
if constexpr (N > 0)
|
|
||||||
const_for_each_(std::make_integer_sequence<decltype(N), N>{}, std::forward<F>(func));
|
|
||||||
}
|
|
|
@ -2,8 +2,11 @@
|
||||||
#include <tuple>
|
#include <tuple>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <print>
|
#include <print>
|
||||||
|
#include "buffer.h"
|
||||||
#ifdef __APPLE__
|
#ifdef __APPLE__
|
||||||
#include <libkern/OSByteOrder.h>
|
#include <libkern/OSByteOrder.h>
|
||||||
|
#define read_comp(size, ptr, t) const_for_<size>([&](auto i){std::get<i.value>(t) = read_var<std::tuple_element_t<i.value, decltype(t)>>::call(&ptr);});
|
||||||
|
|
||||||
|
|
||||||
#define htobe16(x) OSSwapHostToBigInt16(x)
|
#define htobe16(x) OSSwapHostToBigInt16(x)
|
||||||
#define htole16(x) OSSwapHostToLittleInt16(x)
|
#define htole16(x) OSSwapHostToLittleInt16(x)
|
||||||
|
@ -21,5 +24,91 @@
|
||||||
#define le64toh(x) OSSwapLittleToHostInt64(x)
|
#define le64toh(x) OSSwapLittleToHostInt64(x)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
template <typename Integer, Integer ...I, typename F> constexpr void const_for_each_(std::integer_sequence<Integer, I...>, F&& func);
|
struct parsing_buffer
|
||||||
template <auto N, typename F> constexpr void const_for_(F&& func);
|
{
|
||||||
|
parsing_buffer(buffer &b)
|
||||||
|
:buf(b)
|
||||||
|
{}
|
||||||
|
buffer &buf;
|
||||||
|
char *point;
|
||||||
|
int consumed_size;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
static T read_type(char *data)
|
||||||
|
{
|
||||||
|
T a;
|
||||||
|
|
||||||
|
std::memcpy(&a, data, sizeof(T));
|
||||||
|
|
||||||
|
switch (sizeof(T))
|
||||||
|
{
|
||||||
|
case 1:
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
a = be16toh(a);
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
a = be32toh(a);
|
||||||
|
break;
|
||||||
|
case 8:
|
||||||
|
a = be64toh(a);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
std::println("This integer is not supported!");
|
||||||
|
}
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
float read_type<float>(char *data)
|
||||||
|
{
|
||||||
|
uint32_t num_as_uint32;
|
||||||
|
float num;
|
||||||
|
|
||||||
|
memcpy(&num_as_uint32, data, sizeof(uint32_t));
|
||||||
|
num_as_uint32 = be32toh(num_as_uint32);
|
||||||
|
memcpy(&num, &num_as_uint32, sizeof(float));
|
||||||
|
|
||||||
|
return num;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
double read_type<double>(char *data)
|
||||||
|
{
|
||||||
|
uint64_t num_as_uint64;
|
||||||
|
double num;
|
||||||
|
|
||||||
|
memcpy(&num_as_uint64, data, sizeof(uint64_t));
|
||||||
|
num_as_uint64 = be64toh(num_as_uint64);
|
||||||
|
memcpy(&num, &num_as_uint64, sizeof(double));
|
||||||
|
|
||||||
|
return num;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
struct read_var
|
||||||
|
{
|
||||||
|
static T call(parsing_buffer* v)
|
||||||
|
{
|
||||||
|
if (sizeof(T) + v->consumed_size > v->buf.size)
|
||||||
|
return T{};
|
||||||
|
T ret = read_type<T>(v->point);
|
||||||
|
v->point += sizeof(T);
|
||||||
|
v->consumed_size += sizeof(T);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
if constexpr (N > 0)
|
||||||
|
const_for_each_(std::make_integer_sequence<decltype(N), N>{}, std::forward<F>(func));
|
||||||
|
}
|
||||||
|
|
|
@ -1,8 +0,0 @@
|
||||||
#include "file_read.h"
|
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
void hello()
|
|
||||||
{
|
|
||||||
std::cout << "Hello, World!" << std::endl;
|
|
||||||
}
|
|
57
file_read.h
57
file_read.h
|
@ -1,3 +1,58 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
#include "file_deserialising.h"
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
void hello();
|
class file_reader
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
file_reader(int file_fd)
|
||||||
|
:fd(file_fd)
|
||||||
|
{
|
||||||
|
buf.size = 0;
|
||||||
|
buf.allocated = 0;
|
||||||
|
buf.data = nullptr;
|
||||||
|
file_size = lseek(fd, 0, SEEK_END);
|
||||||
|
lseek(fd, 0, SEEK_SET);
|
||||||
|
file_size_remaining = file_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename ...T>
|
||||||
|
std::tuple<T...> read_from_tuple(std::tuple<T...> in)
|
||||||
|
{
|
||||||
|
constexpr std::size_t size_tuple = std::tuple_size_v<decltype(in)>;
|
||||||
|
int size = 0;
|
||||||
|
const_for_<size_tuple>([&](auto i){size += sizeof(std::get<i.value>(in));});
|
||||||
|
if (size > buf.size)
|
||||||
|
{
|
||||||
|
int extra_size = 0;
|
||||||
|
if (size + BUFFER_SIZE <= file_size_remaining)
|
||||||
|
{
|
||||||
|
extra_size = BUFFER_SIZE;
|
||||||
|
}
|
||||||
|
else if (size > file_size_remaining)
|
||||||
|
{
|
||||||
|
extra_size = file_size_remaining - size;
|
||||||
|
}
|
||||||
|
char *new_read = (char *)calloc(size + extra_size, sizeof(char));
|
||||||
|
size_t res = read(fd, new_read, size + extra_size);
|
||||||
|
if (res != size + extra_size)
|
||||||
|
{
|
||||||
|
free(new_read);
|
||||||
|
throw std::runtime_error("Read failed!");
|
||||||
|
}
|
||||||
|
buf.write(new_read, size + extra_size);
|
||||||
|
file_size_remaining -= size + extra_size;
|
||||||
|
free(new_read);
|
||||||
|
}
|
||||||
|
parsing_buffer par_buf(buf);
|
||||||
|
par_buf.point = buf.data;
|
||||||
|
par_buf.consumed_size = 0;
|
||||||
|
read_comp(size_tuple, par_buf, in);
|
||||||
|
buf.remove(0, size);
|
||||||
|
return in;
|
||||||
|
}
|
||||||
|
buffer buf;
|
||||||
|
int fd;
|
||||||
|
off_t file_size;
|
||||||
|
off_t file_size_remaining;
|
||||||
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue