Template
Initial commit
This commit is contained in:
commit
d1890b6e18
16 files changed
+702
No files matched your search
@@ -0,0 +1,72 @@
|
||||
#include "io-helper.h"
|
||||
|
||||
#include <system_error>
|
||||
|
||||
std::FILE* core::open_file(const char* file_path, const char* mode)
|
||||
{
|
||||
std::FILE* file = std::fopen(file_path, mode);
|
||||
if (!file)
|
||||
throw std::system_error(errno, std::system_category());
|
||||
|
||||
return file;
|
||||
}
|
||||
|
||||
void core::seek_file_to_beginning(std::FILE* file, long offset)
|
||||
{
|
||||
const int result = std::fseek(file, offset, SEEK_SET);
|
||||
if (result != 0)
|
||||
throw std::system_error(errno, std::system_category());
|
||||
}
|
||||
|
||||
void core::seek_file_relatively(std::FILE* file, long offset)
|
||||
{
|
||||
const int result = std::fseek(file, offset, SEEK_CUR);
|
||||
if (result != 0)
|
||||
throw std::system_error(errno, std::system_category());
|
||||
}
|
||||
|
||||
void core::seek_file_to_end(std::FILE* file, long offset)
|
||||
{
|
||||
const int result = std::fseek(file, offset, SEEK_END);
|
||||
if (result != 0)
|
||||
throw std::system_error(errno, std::system_category());
|
||||
}
|
||||
|
||||
long core::get_file_offset(std::FILE* file)
|
||||
{
|
||||
const long offset = std::ftell(file);
|
||||
if (offset < 0)
|
||||
throw std::system_error(errno, std::system_category());
|
||||
|
||||
return offset;
|
||||
}
|
||||
|
||||
std::size_t core::read_from_file(void* buffer, std::size_t size, std::size_t count, std::FILE* file)
|
||||
{
|
||||
const std::size_t objects_read = std::fread(buffer, size, count, file);
|
||||
if (objects_read < count)
|
||||
{
|
||||
if (std::feof(file))
|
||||
throw std::runtime_error("Failed to read from file: End-of-file reached.");
|
||||
else
|
||||
throw std::runtime_error("Failed to read from file: Unknown error.");
|
||||
}
|
||||
|
||||
return objects_read;
|
||||
}
|
||||
|
||||
std::size_t core::write_to_file(const void* buffer, std::size_t size, std::size_t count, std::FILE* file)
|
||||
{
|
||||
const std::size_t objects_written = std::fwrite(buffer, size, count, file);
|
||||
if (objects_written < count)
|
||||
throw std::runtime_error("Failed to write to file.");
|
||||
|
||||
return objects_written;
|
||||
}
|
||||
|
||||
void core::close_file(std::FILE* file)
|
||||
{
|
||||
int result = std::fclose(file);
|
||||
if (result != 0)
|
||||
throw std::runtime_error("Failed to close file.");
|
||||
}
|
||||
Reference in new issue
Block a user