75 file_contents(nullptr),
90 size_t open(
const std::string &filename);
147 buffer_size(10 * 1024 * 1024),
149 buffer(
std::make_unique<uint8_t []>(buffer_size)),
165 file(
const char *filename,
const char *mode) :
171 #if defined(__STDC_LIB_EXT1__) 172 fopen_s(&fp, filename, mode);
174 fp = fopen(filename, mode);
187 file(
const std::string &filename,
const std::string &mode) :
188 file(filename.c_str(), mode.c_str())
203 if (fp !=
nullptr && fp != stdin && fp != stdout && fp != stderr)
223 buffer = std::make_unique<uint8_t []>(
buffer_size);
224 return buffer == NULL ? 0 : 1;
273 file_position +=
size;
278 return ::fread(data, 1, size, fp);
289 void read(std::vector<uint8_t> &buffer)
294 size_t bytes_read =
read(&buffer[0], buffer.size());
301 else if (bytes_read != buffer.size())
302 buffer.resize(bytes_read);
323 bytes_written +=
size;
328 file_position +=
size;
330 if (buffer_used + size < buffer_size)
335 memcpy(buffer.get() +
buffer_used, data, (size_t)size);
344 from = (uint8_t *)data;
349 memcpy(buffer.get(), from, (size_t)block_size);
350 buffer_used += block_size;
368 size_t write(
const std::string &buffer)
370 return write(buffer.c_str(), buffer.size());
381 size_t size(
void)
const;
416 auto error = _fseeki64(fp, offset, SEEK_SET);
418 auto error = fseeko(fp, offset, SEEK_SET);
424 file_position = offset;
430 throw std::out_of_range(
"file::seek() failure");
444 static size_t read_entire_file(
const std::string &filename, std::string &into);
459 return into.
open(filename);
473 static bool write_entire_file(
const std::string &filename,
const std::string &buffer);
487 static void buffer_to_list(std::vector<uint8_t *> &line_list, std::string &buffer);
512 static std::string
mkstemp(std::string prefix);
static bool is_directory(const std::string &filename)
Determines whether the given file system object is a directoy or not.
Definition: file.cpp:259
static void buffer_to_list(std::vector< uint8_t *> &line_list, std::string &buffer)
Turn a single std::string into a vector of uint8_t * (i.e. "C" strings).
Definition: file.cpp:198
file(const std::string &filename, const std::string &mode)
Constructor used for opening files.
Definition: file.h:187
size_t write(const std::string &buffer)
Write bytes number of bytes to the give file at the current cursor position.
Definition: file.h:368
size_t tell(void)
Return the byte offset of the file pointer in the current file.
Definition: file.h:391
size_t buffer_used
How much of the internal file buffer is being used.
Definition: file.h:121
size_t open(const std::string &filename)
Open and read the file into memory.
Definition: file.cpp:36
const void * file_contents
The contents of the file.
Definition: file.h:63
size_t bytes_read
Number of bytes read from this file.
Definition: file.h:124
~file()
Destructor.
Definition: file.h:200
size_t read_entire_file(const uint8_t *&into) const
Return the contents and length of the file.
Definition: file.h:110
size_t size
The size of the file.
Definition: file.h:64
size_t write(const void *data, size_t size)
Write bytes number of bytes to the give file at the current cursor position.
Definition: file.h:315
~file_read_only()
Destrucgtor.
Definition: file.cpp:115
std::unique_ptr< uint8_t []> buffer
Internal file buffer.
Definition: file.h:122
file(FILE *fp)
Constructor with a C FILE * object.
Definition: file.h:144
void read(std::vector< uint8_t > &buffer)
Read buffer.size() bytes from the give file into the buffer. If at end of file then this method will ...
Definition: file.h:289
size_t setvbuf(size_t size)
change the size of the internal buffer (does not flush() first)
Definition: file.h:220
size_t read(void *data, size_t size)
Read bytes number of bytes from the give file into the buffer.
Definition: file.h:258
static void unittest(void)
Unit test this class.
Definition: file.cpp:350
size_t bytes_written
Number of bytes written to this file.
Definition: file.h:123
void flush(void)
Flush the internal buffers to disk (called automatically on close).
Definition: file.h:234
static size_t read_entire_file(const std::string &filename, file_read_only &into)
Read the contents of file filename into the std::string into.
Definition: file.h:457
static std::string mkstemp(std::string prefix)
Generate a temporary filename containing the given prefix.
Definition: file.cpp:329
file_read_only()
Constructor.
Definition: file.h:74
FILE * fp
The underlying representation is a FILE * from C (as they appear to be fast).
Definition: file.h:118
static bool write_entire_file(const std::string &filename, const std::string &buffer)
Write the contents of buffer to the file specified in filenane.
Definition: file.cpp:176
void seek(size_t offset)
Seek to the given offset in the file.
Definition: file.h:405
File based I/O methods including whole file and partial files.
Definition: file.h:45
size_t file_position
The ftell() position in the file.
Definition: file.h:119
size_t buffer_size
Size of the internal file buffering.
Definition: file.h:120
Definition: compress_integer_elias_delta_simd.c:23
A read_only file object, the memory was probably allocated with mmap() and needs deallocating accordi...
Definition: file.h:55
file(const char *filename, const char *mode)
Constructor used for opening files.
Definition: file.h:165