1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
#ifndef BYTEME_RAW_FILE_WRITER_HPP
#define BYTEME_RAW_FILE_WRITER_HPP
#include <vector>
#include <stdexcept>
#include <string>
#include <cstdio>
#include "Writer.hpp"
#include "SelfClosingFILE.hpp"
/**
* @file RawFileWriter.hpp
*
* @brief Write bytes to a file without any extra transformations.
*/
namespace byteme {
/**
* @brief Write bytes to a file.
*
* This class will write bytes to a file without any further transformations.
* It is basically a simple wrapper around `FILE` structures, with correct closing and error checking.
*/
class RawFileWriter : public Writer {
public:
/**
* @param path Path to the file.
* @param buffer_size Size of the buffer to use for writing.
*/
RawFileWriter(const char* path, size_t buffer_size = 65536) : file(path, "wb") {
if (std::setvbuf(file.handle, nullptr, _IOFBF, buffer_size)) {
throw std::runtime_error("failed to set a buffer size for file writing");
}
}
/**
* @param path Path to the file.
* @param buffer_size Size of the buffer to use for writing.
*/
RawFileWriter(const std::string& path, size_t buffer_size = 65536) : RawFileWriter(path.c_str(), buffer_size) {}
public:
using Writer::write;
void write(const unsigned char* buffer, size_t n) {
size_t ok = std::fwrite(buffer, sizeof(unsigned char), n, file.handle);
if (ok < n) {
throw std::runtime_error("failed to write raw binary file (fwrite error " + std::to_string(std::ferror(file.handle)) + ")");
}
}
void finish() {
if (std::fclose(file.handle)) {
throw std::runtime_error("failed to close raw binary file");
}
file.handle = nullptr;
}
private:
SelfClosingFILE file;
};
}
#endif
|