75 lines
1.9 KiB
C++
75 lines
1.9 KiB
C++
//---
|
|
// fxos.util: Utility functions
|
|
//---
|
|
|
|
#ifndef LIBFXOS_UTIL_H
|
|
#define LIBFXOS_UTIL_H
|
|
|
|
#include <memory>
|
|
#include <iostream>
|
|
#include <string>
|
|
#include <memory>
|
|
#include <cstdio>
|
|
|
|
/* Format a string with printf() syntax */
|
|
template<typename ... Args>
|
|
std::string format(std::string const &format, Args ... args)
|
|
{
|
|
/* Reserve space for snprintf() to put its NUL */
|
|
size_t size = snprintf(nullptr, 0, format.c_str(), args ...) + 1;
|
|
|
|
std::unique_ptr<char[]> buf(new char[size]);
|
|
snprintf(buf.get(), size, format.c_str(), args ...);
|
|
|
|
/* Remove the NUL from the string */
|
|
return std::string(buf.get(), buf.get() + size - 1);
|
|
}
|
|
|
|
/* An RAII contiguous memory buffer */
|
|
class Buffer
|
|
{
|
|
public:
|
|
/* Empty buffer initialized with given byte */
|
|
Buffer(int size, int fill=0x00);
|
|
|
|
/* Buffer initialized from file, reading the given size and offset.
|
|
* Default offset is beginning of file.
|
|
* Default size (-1) is file size. If the specified region ends after
|
|
the end of the file, the buffer is padded. */
|
|
Buffer(std::string filepath, int size=-1, int offset=0, int fill=0x00);
|
|
|
|
/* Create a buffer by copying (and possibly resizing) another buffer */
|
|
Buffer(Buffer const &other, int new_size=-1);
|
|
|
|
/* Size */
|
|
int size() const noexcept;
|
|
};
|
|
|
|
/* A file abstraction that supports both direct load and memory mapping */
|
|
class File
|
|
{
|
|
public:
|
|
/* Load a file, either by buffer or by memory mapping */
|
|
File(std::string path, bool mmap=false);
|
|
|
|
/* Get the path, size and loading address of the file */
|
|
std::string path() const noexcept;
|
|
size_t size() const noexcept;
|
|
char *data() const noexcept;
|
|
|
|
/* Free the allocated buffers */
|
|
~File();
|
|
|
|
private:
|
|
/* Path to file */
|
|
std::string m_path;
|
|
/* Size of buffer, or mapping */
|
|
size_t m_size;
|
|
/* Whether mmap() was used on the file */
|
|
bool m_mmap;
|
|
/* Data buffer (m_mmap=false) or mapping address (m_mmap=true) */
|
|
char *m_addr;
|
|
};
|
|
|
|
#endif /* LIBFXOS_UTIL_H */
|