fxos/include/fxos/util/Buffer.h

58 lines
2.1 KiB
C++

//---------------------------------------------------------------------------//
// 1100101 |_ mov #0, r4 __ //
// 11 |_ <0xb380 %5c4> / _|_ _____ ___ //
// 0110 |_ 3.50 -> 3.60 | _\ \ / _ (_-< //
// |_ base# + offset |_| /_\_\___/__/ //
//---------------------------------------------------------------------------//
// fxos/util/Buffer: Generic RAII buffer to load files and data
//
// The Buffer object can be used to load files in buffers of fixed sizes and
// supports some resizing operations. Its main uses are to load files mapped
// to virtual space regions with a size that fits the declared region, and to
// load data files in an RAII fashion.
//---
#ifndef FXOS_UTIL_BUFFER_H
#define FXOS_UTIL_BUFFER_H
#include <string>
#include <memory>
#include <vector>
/* An RAII contiguous memory buffer */
struct Buffer
{
/* Empty buffer with size 0 and no pointer */
Buffer();
/* Empty buffer initialized with given byte */
Buffer(size_t size, int fill = 0x00);
/* Buffer initialized from file, reading the given size from the
beginning of the file. If the file is smaller than the specified
size, the buffer is padded.
If this constructor is used, the file path is remembered. */
Buffer(std::string const &filepath, ssize_t size = -1, int fill = 0x00);
/* Buffer initialized from file by looking in one of the specified
directories only. */
Buffer(std::string filepath, std::vector<std::string> const &folders,
ssize_t size = -1, int fill = 0x00);
/* Create a buffer by copying and resizing another buffer */
Buffer(Buffer const &other, size_t new_size, int fill = 0x00);
/* Buffer size */
size_t size;
/* Data */
std::unique_ptr<char[]> data;
/* File path, when constructed from file */
std::string path;
private:
void loadFromFile(std::string const &path, ssize_t size, int fill);
};
#endif /* FXOS_UTIL_BUFFER_H */