34#include <NDEVR/File.h>
35#include <NDEVR/String.h>
36#include <NDEVR/Dictionary.h>
37#include <NDEVR/Bounds.h>
38#include <NDEVR/Compressor.h>
46 template <
class t_type>
49 char*
const p =
reinterpret_cast<char*
>(&in);
50 for (
size_t i = 0; i <
sizeof(t_type) / 2; ++i)
51 std::swap(p[i], p[
sizeof(t_type) - i - 1]);
69 void open(
bool read,
bool safe)
73 m_file.open(safe ? File::e_binary_read_safe : File::e_binary_read);
75 m_file.open(safe ? File::e_binary_write_safe : File::e_binary_write);
84 m_file.open(File::e_binary_read);
85 m_file.invalidateCache();
86 uint08 size = m_file.fileSize();
87 m_cached_data.setSize(size);
88 fread(m_cached_data.begin(), 1, m_cached_data.size(), m_file.filePtr());
93 m_cached_data.clear();
103 else if (m_multitreaded_compression)
110 m_file.open(File::e_binary_write);
111 fwrite(m_cached_data.begin(), 1, m_cached_data.size(), m_file.filePtr());
119 return m_cached_data;
122 template<
class t_type>
128 if (m_cache_location == m_cached_data.size())
129 m_cached_data.addAll((
uint01*)&data,
sizeof(data));
131 m_cached_data.setAll((
uint01*)&data, m_cache_location,
sizeof(data));
132 m_cache_location +=
sizeof(data);
136 fwrite(&data,
sizeof(t_type), 1, m_file.filePtr());
141 template<
class t_type,
class t_index_type,
class t_memory_allocator,
class t_memory_manager>
144 if (data.
size() != 0)
148 binary.
compression_mode = Compressor::PickCompressionMode<t_type>(compression_mode);
158 m_compressed_data.clear();
163 m_compressed_data.setSize(data.
memSize());
167 Compressor::CompressData(binary);
168 writeCompression(binary);
173 if (m_large_compression)
174 write(cast<uint08>(data.
size()));
176 write(cast<uint04>(data.
size()));
179 template<
class t_type,
class t_index_type,
class t_memory_allocator,
class t_memory_manager>
191 Buffer<
decltype(t_type::Type())> combined_data(total_size);
194 combined_data.addAll(data[i]);
196 write(sizes, compression_mode);
197 write(combined_data, compression_mode);
204 if (m_cache_location == m_cached_data.size())
208 m_cache_location +=
getMin(data.
size(), size);
209 if (size > data.
size())
212 if (m_cache_location == m_cached_data.size())
213 m_cached_data.setAllToValue(fill_space, m_cache_location, add_size);
215 m_cached_data.addAndFillSpace(add_size, fill_space);
216 m_cache_location += add_size;
222 fwrite(data.
begin(), data.
size(), 1, m_file.filePtr());
223 if (size > data.
size())
226 for(
uint04 i = 0; i < add_size; i++)
227 fwrite(&fill_space, add_size, 1, m_file.filePtr());
232 template<
class t_type>
238 data = *((t_type*)(&m_cached_data[m_cache_location]));
239 m_cache_location += cast<uint08>(
sizeof(t_type));
243 fread(&data,
sizeof(t_type), 1, m_file.filePtr());
253 char c = *((
char*)(&m_cached_data[m_cache_location++]));
254 if (c == terminator || m_cache_location >= m_cached_data.size())
263 char c = cast<char>(fgetc(m_file.filePtr()));
264 if (c == terminator || feof(m_file.filePtr()))
277 char c = *((
char*)(&m_cached_data[m_cache_location++]));
278 if (c == terminator || m_cache_location >= m_cached_data.size())
287 char c = cast<char>(fgetc(m_file.filePtr()));
288 if (c == terminator || feof(m_file.filePtr()))
300 uint04 actual_size = readData(
string.begin(), max_size);
301 string.setSize(actual_size);
308 for (
uint04 i = 0; i < max_size; i++)
310 char c = *((
char*)(&m_cached_data[m_cache_location++]));
311 if (m_cache_location >= m_cached_data.size())
318 for (
uint04 i = 0; i < max_size; i++)
320 char c = cast<char>(fgetc(m_file.filePtr()));
321 if (feof(m_file.filePtr()))
328 template<
class t_type,
class t_index_type,
class t_memory_allocator,
class t_memory_manager>
331 bool allow_multithread = m_multitreaded_compression;
332 read(data, allow_multithread);
334 template<
class t_type,
class t_index_type,
class t_memory_allocator,
class t_memory_manager>
337 bool allow_multithread =
false;
338 read(data, allow_multithread);
341 template<
class t_type,
class t_index_type,
class t_memory_allocator,
class t_memory_manager>
345 Buffer<
decltype(t_type::Type())> combined_data;
347 allow_multithread =
false;
348 read(sizes, allow_multithread);
349 read(combined_data, allow_multithread);
351 uint04 current_offset = 0;
356 data[i].
addAll(&combined_data[0] + current_offset, sizes[i]);
357 current_offset += sizes[i];
361 template<
class t_index_type,
class t_memory_allocator,
class t_memory_manager>
366 allow_multithread =
false;
367 read(indices, allow_multithread);
372 if (bounds.
span() > 0U)
374 data[i] = m_strings.substr(bounds[
MIN], bounds[
MAX]);
383 template<
class t_type,
class t_index_type,
class t_memory_allocator,
class t_memory_manager>
387 if (m_large_compression)
394 if (data.
size() != 0)
397 if (m_large_compression)
403 binary.
compression_mode = cast<CompressionMode>(m_cached_data[m_cache_location]);
409 allow_multithread &= binary.
compression_mode != CompressionMode::e_string_compression;
410 allow_multithread &= binary.
compression_mode != CompressionMode::e_no_compression;
411 if (m_multitreaded_compression && allow_multithread)
412 m_compressions.add(binary);
414 Compressor::DecompressData(binary);
419 Compressor::DecompressData(binary);
429 size_t fsize = fread(&m_compressed_data[0], 1, m_compressed_data.size(), m_file.filePtr());
430 lib_assert(m_compressed_data.size() == cast<uint08>(fsize),
"Bad read size");
432 fread(&m_compressed_data[0], 1, m_compressed_data.size(), m_file.filePtr());
437 Compressor::DecompressData(binary);
442 Compressor::DecompressData(binary);
449#define ndevr_ftell _ftelli64
450#define ndevr_fseek _fseeki64
452#define ndevr_ftell ftell
453#define ndevr_fseek fseek
458 return m_cache_location;
460 return cast<uint08>(
ndevr_ftell(m_file.filePtr()));
466 return cast<fltp04>(m_cache_location) / cast<fltp04>(m_cached_data.size());
471 m_file_size = m_file.fileSize();
472 return cast<fltp04>(cast<uint08>(
ndevr_ftell(m_file.filePtr()))) / cast<fltp04>(m_file_size);
478 m_cache_location = location;
480 ndevr_fseek(m_file.filePtr(), cast<long long>(location), SEEK_SET);
493 return m_cached_data.size();
495 m_file_size = m_file.fileSize();
509 bool m_using_cache =
false;
510 bool m_is_read =
false;
511 bool m_large_compression =
false;
512 bool m_multitreaded_compression =
true;
#define ndevr_fseek
Definition BinaryFile.h:450
#define ndevr_ftell
Definition BinaryFile.h:449
#define lib_assert(expression, message)
Definition LibAssert.h:61
Logic for reading or writing to a binary file including logic for.
Definition BinaryFile.h:59
uint04 readString(char *string, char terminator='\0')
Definition BinaryFile.h:270
void read(Buffer< t_type, t_index_type, t_memory_allocator, t_memory_manager > &data)
Definition BinaryFile.h:329
void setUseLargeCompression(bool use_large_compression)
Definition BinaryFile.h:483
std::enable_if<!ObjectInfo< t_type >::Buffer >::type read(Buffer< t_type, t_index_type, t_memory_allocator, t_memory_manager > &data, bool &allow_multithread)
Definition BinaryFile.h:384
Buffer< Bounds< 1, uint04 > > m_string_size_info
Definition BinaryFile.h:502
void write(const t_type &data)
Definition BinaryFile.h:123
fltp04 percent()
Definition BinaryFile.h:462
std::enable_if< ObjectInfo< t_type >::Buffer >::type write(const Buffer< t_type, t_index_type, t_memory_allocator, t_memory_manager > &data, CompressionMode compression_mode)
Definition BinaryFile.h:180
void writeCompression(BinaryCompressionObject &compression_object)
void seek(uint08 location)
Definition BinaryFile.h:475
std::enable_if<!ObjectInfo< t_type >::Buffer >::type write(const Buffer< t_type, t_index_type, t_memory_allocator, t_memory_manager > &data, CompressionMode compression_mode)
Definition BinaryFile.h:142
void close()
Definition BinaryFile.h:97
void writeStringData(CompressionMode compression_mode)
void open(bool read, bool safe)
Definition BinaryFile.h:69
Dictionary< String, uint04 > m_string_reference
Definition BinaryFile.h:501
String m_strings
Definition BinaryFile.h:500
void cachedOpen(bool read)
Definition BinaryFile.h:77
std::enable_if< ObjectInfo< t_type >::Buffer >::type read(Buffer< t_type, t_index_type, t_memory_allocator, t_memory_manager > &data, bool &allow_multithread)
Definition BinaryFile.h:342
void readNow(Buffer< t_type, t_index_type, t_memory_allocator, t_memory_manager > &data)
Definition BinaryFile.h:335
uint08 fileSize() const
Definition BinaryFile.h:490
void writeRawData(const String &data, uint04 size, char fill_space='\0')
Definition BinaryFile.h:199
void readStringData(bool has_reference_table)
uint08 position()
Definition BinaryFile.h:455
void readStringBuffer(Buffer< String, t_index_type, t_memory_allocator, t_memory_manager > &data, bool &allow_multithread)
Definition BinaryFile.h:362
File m_file
Definition BinaryFile.h:499
Buffer< BinaryCompressionObject > m_compressions
Definition BinaryFile.h:506
String readData(uint04 max_size)
Definition BinaryFile.h:296
BinaryCompressionObject createCompressionObject()
void uncompressSections(uint04 start, uint04 count)
~BinaryFile()
Definition BinaryFile.h:65
Buffer< uint01, uint08 > m_compressed_data
Definition BinaryFile.h:504
uint04 readData(char *string, uint04 max_size)
Definition BinaryFile.h:304
Buffer< uint01, uint08 > m_cached_data
Definition BinaryFile.h:505
BinaryFile(const File &file)
Definition BinaryFile.h:62
void readString(String &string, char terminator='\0')
Definition BinaryFile.h:247
t_type read()
Definition BinaryFile.h:233
const Buffer< uint01, uint08 > & cachedData() const
Definition BinaryFile.h:117
A specification of upper and lower bounds in N-dimensions.
Definition Bounds.hpp:52
constexpr Ray< t_dims, t_type > span() const
The side lengths of these bounds. For each dimension, the span is max - min.
Definition Bounds.hpp:111
The equivelent of std::vector but with a bit more control. The basic array unit of the library.
Definition Buffer.hpp:56
constexpr t_index_type size() const
Definition Buffer.hpp:823
decltype(auto) ptr()
Definition Buffer.hpp:387
void addAll(const Buffer< t_type, t_other_index_type, t_other_memory_allocator, t_other_memory_manager > &buffer)
Definition Buffer.hpp:243
void setSize(t_index_type new_size)
Definition Buffer.hpp:803
decltype(auto) begin()
Definition Buffer.hpp:402
t_index_type memSize() const
Definition Buffer.hpp:397
void clear()
Definition Buffer.hpp:422
A hash-based key-value store, useful for quick associative lookups. Key features include:
Definition Dictionary.h:61
Logic for reading or writing to a file as well as navigating filesystems.
Definition File.h:48
The core String class for the NDEVR API.
Definition String.h:69
Logic for reading or writing to a string or a user friendly, TranslatedString.
Definition StringStream.h:230
constexpr bool IsInvalid(const t_type &value)
Query if 'value' is valid or invalid. Invalid values should return invalid if used for calculations o...
Definition BaseFunctions.hpp:170
@ MIN
Definition BaseValues.hpp:196
@ MAX
Definition BaseValues.hpp:197
float fltp04
Defines an alias representing a 4 byte floating-point number Bit layout is as follows: -Sign: 1 bit a...
Definition BaseValues.hpp:127
uint8_t uint01
-Defines an alias representing a 1 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:80
t_type ChangeEndian(t_type in)
Definition BinaryFile.h:47
CompressionMode
Logical information about the type of compression implemented or requested.
Definition Compressor.h:16
@ e_string_compression
Definition Compressor.h:20
@ e_string_reference
Definition Compressor.h:31
uint64_t uint08
-Defines an alias representing an 8 byte, unsigned integer
Definition BaseValues.hpp:106
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:96
double fltp08
Defines an alias representing an 8 byte floating-point number.
Definition BaseValues.hpp:149
constexpr t_type getMin(const t_type &left, const t_type &right)
Finds the minimum of the given arguments based on the < operator Author: Tyler Parke Date: 2017-11-05...
Definition BaseFunctions.hpp:56
A container for storing compressed data, typically used for File IO operations. Responsible for stori...
Definition Compressor.h:52
uint01 * compressed_data
Definition Compressor.h:56
uint08 compressed_size
Definition Compressor.h:57
uint01 * uncompressed_data
Definition Compressor.h:54
uint08 uncompressed_size
Definition Compressor.h:55
CompressionMode compression_mode
Definition Compressor.h:53
uint08 buffer_size
Definition Compressor.h:58
Defines for a given type (such as sint04, fltp08, UUID, etc) a maximum, minimum, and reserved 'invali...
Definition BaseValues.hpp:233