First commit
This commit is contained in:
commit
80a525dbdb
8 changed files with 172 additions and 0 deletions
8
.idea/.gitignore
generated
vendored
Normal file
8
.idea/.gitignore
generated
vendored
Normal file
|
@ -0,0 +1,8 @@
|
|||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
10
CMakeLists.txt
Normal file
10
CMakeLists.txt
Normal file
|
@ -0,0 +1,10 @@
|
|||
cmake_minimum_required(VERSION 3.31)
|
||||
project(file_read)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 23)
|
||||
|
||||
add_library(file_read STATIC file_read.cpp
|
||||
file_deserialising.cpp
|
||||
file_deserialising.h
|
||||
buffer.cpp
|
||||
buffer.h)
|
32
buffer.cpp
Normal file
32
buffer.cpp
Normal file
|
@ -0,0 +1,32 @@
|
|||
#include "buffer.h"
|
||||
|
||||
void buffer::write(char *data_in, int data_size)
|
||||
{
|
||||
if (data_size > allocated - size)
|
||||
{
|
||||
allocated = allocated + (allocated - (size + data_size)) + BUFFER_SIZE;
|
||||
data = (char *)realloc(data, allocated);
|
||||
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 (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;
|
||||
}
|
20
buffer.h
Normal file
20
buffer.h
Normal file
|
@ -0,0 +1,20 @@
|
|||
#pragma once
|
||||
#include <cstdlib>
|
||||
#include <stdexcept>
|
||||
#include <cstring>
|
||||
#define BUFFER_SIZE 1092
|
||||
|
||||
struct buffer
|
||||
{
|
||||
char *data;
|
||||
int size;
|
||||
int allocated;
|
||||
|
||||
void write(char *data_in, int data_size);
|
||||
void remove(int offset, int remove_size);
|
||||
|
||||
~buffer()
|
||||
{
|
||||
free(data);
|
||||
}
|
||||
};
|
66
file_deserialising.cpp
Normal file
66
file_deserialising.cpp
Normal file
|
@ -0,0 +1,66 @@
|
|||
#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));
|
||||
}
|
25
file_deserialising.h
Normal file
25
file_deserialising.h
Normal file
|
@ -0,0 +1,25 @@
|
|||
#pragma once
|
||||
#include <tuple>
|
||||
#include <cstring>
|
||||
#include <print>
|
||||
#ifdef __APPLE__
|
||||
#include <libkern/OSByteOrder.h>
|
||||
|
||||
#define htobe16(x) OSSwapHostToBigInt16(x)
|
||||
#define htole16(x) OSSwapHostToLittleInt16(x)
|
||||
#define be16toh(x) OSSwapBigToHostInt16(x)
|
||||
#define le16toh(x) OSSwapLittleToHostInt16(x)
|
||||
|
||||
#define htobe32(x) OSSwapHostToBigInt32(x)
|
||||
#define htole32(x) OSSwapHostToLittleInt32(x)
|
||||
#define be32toh(x) OSSwapBigToHostInt32(x)
|
||||
#define le32toh(x) OSSwapLittleToHostInt32(x)
|
||||
|
||||
#define htobe64(x) OSSwapHostToBigInt64(x)
|
||||
#define htole64(x) OSSwapHostToLittleInt64(x)
|
||||
#define be64toh(x) OSSwapBigToHostInt64(x)
|
||||
#define le64toh(x) OSSwapLittleToHostInt64(x)
|
||||
#endif
|
||||
|
||||
template <typename Integer, Integer ...I, typename F> constexpr void const_for_each_(std::integer_sequence<Integer, I...>, F&& func);
|
||||
template <auto N, typename F> constexpr void const_for_(F&& func);
|
8
file_read.cpp
Normal file
8
file_read.cpp
Normal file
|
@ -0,0 +1,8 @@
|
|||
#include "file_read.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
void hello()
|
||||
{
|
||||
std::cout << "Hello, World!" << std::endl;
|
||||
}
|
3
file_read.h
Normal file
3
file_read.h
Normal file
|
@ -0,0 +1,3 @@
|
|||
#pragma once
|
||||
|
||||
void hello();
|
Loading…
Add table
Add a link
Reference in a new issue