65 fh = fopen(filename,
"wb");
67 OJPH_ERROR(0x00060001,
"failed to open %s for writing", filename);
74 return fwrite(ptr, 1,
size,
fh);
117 std::swap(this->
is_open,other.is_open);
118 std::swap(this->
clear_mem,other.clear_mem);
119 std::swap(this->
buf_size,other.buf_size);
120 std::swap(this->
used_size,other.used_size);
121 std::swap(this->
buf,other.buf);
122 std::swap(this->
cur_ptr,other.cur_ptr);
154 assert(this->
is_open ==
false);
208 size_t needed_size = (size_t)
tell() + new_size;
212 memcpy(this->
cur_ptr, ptr, new_size);
223 FILE *f = fopen(file_name,
"wb");
225 OJPH_ERROR(0x00060003,
"failed to open %s for writing", file_name);
228 OJPH_ERROR(0x00060004,
"failed writing to %s", file_name);
237 needed_size += (needed_size + 1) >> 1;
244 OJPH_ERROR(0x00060005,
"failed to allocate memory (%zu bytes)",
247 if (this->
buf != NULL)
277 fh = fopen(filename,
"rb");
279 OJPH_ERROR(0x00060002,
"failed to open %s for reading", filename);
286 return fread(ptr, 1,
size,
fh);
339 assert(this->data == NULL);
347 std::ptrdiff_t bytes_left = (
data + this->size) -
cur_ptr;
350 size_t bytes_to_read =
ojph_min(
size, (
size_t)bytes_left);
351 memcpy(ptr,
cur_ptr, bytes_to_read);
353 return bytes_to_read;
365 if (offset >= 0 && (
size_t)offset <=
size)
374 if (bytes_off >= 0 && (
size_t)bytes_off <=
size)
382 if (offset <= 0 && (std::ptrdiff_t)
size + offset >= 0)
397 std::swap(this->
data,other.data);
398 std::swap(this->
cur_ptr,other.cur_ptr);
399 std::swap(this->
size,other.size);
void open(const char *filename)
size_t read(void *ptr, size_t size) override
void open(const char *filename)
size_t write(const void *ptr, size_t size) override
size_t read(void *ptr, size_t size) override
mem_infile(mem_infile &&) noexcept
void open(const ui8 *data, size_t size)
void swap(mem_infile &) noexcept
mem_infile & operator=(mem_infile const &)=delete
si64 tell() override
Call this function to know the file size (i.e., number of bytes used to store the file).
static const size_t ALIGNED_ALLOC_MASK
size_t write(const void *ptr, size_t size) override
Call this function to write data to the memory file.
void expand_storage(size_t new_size, bool clear_all)
This function expands storage by x1.5 needed space.
void open(size_t initial_size=65536, bool clear_mem=false)
Call this function to open a memory file.
mem_outfile & operator=(mem_outfile const &)=delete
void swap(mem_outfile &other) noexcept
A utility function to swap the contents of two instances.
void write_to_file(const char *file_name) const
Call this function to write the memory file data to a file.
int ojph_fseek(FILE *stream, si64 offset, int origin)
void * ojph_aligned_malloc(size_t alignment, size_t size)
si64 ojph_ftell(FILE *stream)
void ojph_aligned_free(void *pointer)
#define OJPH_ERROR(t,...)