diff --git a/CMakeLists.txt b/CMakeLists.txt index 4631ae1..f63be46 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,8 +3,9 @@ project(file_read) set(CMAKE_CXX_STANDARD 23) -add_library(file_read STATIC +add_library(file_read INTERFACE src/file_deserialising.h - src/buffer.cpp src/buffer.h src/file_read.h) + +set_target_properties(file_read PROPERTIES LINKER_LANGUAGE CXX) diff --git a/src/buffer.cpp b/src/buffer.cpp deleted file mode 100644 index 0f14559..0000000 --- a/src/buffer.cpp +++ /dev/null @@ -1,96 +0,0 @@ -#include "buffer.h" - -void buffer::write(char *data_in, size_t data_size) -{ - if (data_size > allocated - size) - { - allocated += (size + data_size) + BUFFER_SIZE; - data = (char *)realloc(data, allocated); - allocations++; - if (!data) - { - throw std::runtime_error("Allocation failed"); - } - } - std::memcpy(&data[size], data_in, data_size); - size += data_size; -} - -void buffer::remove(int offset, int remove_size) -{ - if (offset + remove_size > size) - return ; - char *new_data = (char *)calloc(allocated, sizeof(char)); - 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++; -} - -void buffer::allocate(size_t s) -{ - data = (char *)realloc(data, allocated + s); - if (!data) - { - throw std::runtime_error("Allocation failed"); - } - allocated += s; -} - -void buffer_unsigned::write(unsigned char *data_in, size_t data_size) -{ - if (data_size > allocated - size) - { - allocated += (size + data_size) + BUFFER_SIZE; - data = (unsigned char *)realloc(data, allocated); - allocations++; - if (!data) - { - throw std::runtime_error("Allocation failed"); - } - } - std::memcpy(&data[size], data_in, data_size); - size += data_size; -} - -void buffer_unsigned::remove(int offset, int remove_size) -{ - if (offset + remove_size > size) - return ; - unsigned char *new_data = (unsigned char *)calloc(allocated, sizeof(char)); - 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++; -} - -void buffer_unsigned::allocate(size_t s) -{ - data = (unsigned char *)realloc(data, allocated + s); - if (!data) - { - throw std::runtime_error("Allocation failed"); - } - allocations++; - allocated += s; -} diff --git a/src/buffer.h b/src/buffer.h index fe966d5..8f3b21c 100644 --- a/src/buffer.h +++ b/src/buffer.h @@ -4,6 +4,7 @@ #include #define BUFFER_SIZE 1024 +template struct buffer { buffer() @@ -13,17 +14,16 @@ struct buffer allocated = 0; allocations = 0; } - char *data; + T *data; size_t size; size_t allocated; int allocations; - void write(char *data_in, size_t data_size); + 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 & operator=(const buffer &) = delete; ~buffer() { if (allocated > 0) @@ -31,28 +31,53 @@ struct buffer } }; -struct buffer_unsigned +template +void buffer::write(T *data_in, size_t data_size) { - buffer_unsigned() + if (data_size > allocated - size) { - data = nullptr; - size = 0; - allocated = 0; - allocations = 0; + allocated += (size + data_size) + BUFFER_SIZE; + data = (T *)realloc(data, allocated); + allocations++; + if (!data) + { + throw std::runtime_error("Allocation failed"); + } } - unsigned char *data; - size_t size; - size_t allocated; - int allocations; + std::memcpy(&data[size], data_in, data_size); + size += data_size; +} - void write(unsigned char *data_in, size_t data_size); - void remove(int offset, int remove_size); - void allocate(size_t s); - - buffer_unsigned(const buffer_unsigned&) = delete; - ~buffer_unsigned() +template +void buffer::remove(int offset, int remove_size) +{ + if (offset + remove_size > size) + return ; + T *new_data = (T *)calloc(allocated, sizeof(T)); + if (!new_data) { - if (allocated > 0) - free(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 +void buffer::allocate(size_t s) +{ + data = (T *)realloc(data, allocated + s); + if (!data) + { + throw std::runtime_error("Allocation failed"); + } + allocations++; + allocated += s; +} diff --git a/src/file_deserialising.h b/src/file_deserialising.h index 1bf2d95..fefb141 100644 --- a/src/file_deserialising.h +++ b/src/file_deserialising.h @@ -28,10 +28,10 @@ struct parsing_buffer { - parsing_buffer(buffer &b) + parsing_buffer(buffer &b) :buf(b) {} - buffer &buf; + buffer &buf; char *point; int consumed_size; }; @@ -79,7 +79,7 @@ inline void read_type(double &val, char *data) } template<> -inline void read_type(buffer &val, char *data) +inline void read_type>(buffer &val, char *data) { val.allocated = val.size + 1; val.data = (char *)calloc(val.size + 1,sizeof(char)); @@ -92,13 +92,13 @@ struct read_var static void call(T& val, parsing_buffer* v) { int size = 0; - if constexpr (typeid(T) == typeid(buffer)) + if constexpr (typeid(T) == typeid(buffer)) size = val.size; else size = sizeof(T); if (size + v->consumed_size > v->buf.size) { - if constexpr (typeid(T) == typeid(buffer)) + if constexpr (typeid(T) == typeid(buffer)) { val.size = v->buf.size; } diff --git a/src/file_read.h b/src/file_read.h index 99a50b6..004cef1 100644 --- a/src/file_read.h +++ b/src/file_read.h @@ -79,7 +79,7 @@ class file_reader size_t total_read_size = 0; const_for_([&](auto i) { - if constexpr (typeid(std::get(in)) == typeid(buffer)) + if constexpr (typeid(std::get(in)) == typeid(buffer)) size += std::get(in).size; else size += sizeof(std::get(in)); @@ -188,7 +188,7 @@ class file_reader return std::make_pair(size, ret); } private: - buffer buf; + buffer buf; int fd; off_t file_size; off_t file_size_remaining;