Browse Source

std_utils.hh - add file_read_bin() / file_write_bin()

master
heck 2 weeks ago
parent
commit
7c1c8016f9
3 changed files with 30 additions and 0 deletions
  1. +2
    -0
      .gitignore
  2. +26
    -0
      src/std_utils.cc
  3. +2
    -0
      src/std_utils.hh

+ 2
- 0
.gitignore View File

@ -57,3 +57,5 @@ test_pEpLog_basic
/test/pitytest11/test/pitytest_data/
/test/test_pEpSQLite
/test/test_swarm_tofu
/test/test_file_rw_bin
/test/test_rw.bin

+ 26
- 0
src/std_utils.cc View File

@ -118,6 +118,32 @@ namespace pEp {
return ss.str();
}
std::vector<char> file_read_bin(const std::string &filename)
{
std::vector<char> ret{};
if (pEp::Utils::path_exists(filename)) {
std::ifstream ifs(filename, std::ios_base::binary);
ifs.unsetf(std::ios_base::skipws);
if (ifs.bad()) {
throw std::runtime_error("failed to read file: '" + filename + "'");
}
ret = { std::istream_iterator<char>(ifs), std::istream_iterator<char>() };
} else {
throw std::runtime_error("File does not exist: '" + filename + "'");
}
return ret;
}
void file_write_bin(const std::string &filename, std::vector<char> &data)
{
std::fstream f(filename, std::ios_base::out | std::ios_base::binary | std::ios_base::trunc);
f.write(data.data(), static_cast<std::streamsize>(data.size()));
if (f.bad()) {
throw std::runtime_error("failed to write file: '" + filename + "'");
}
}
#ifndef WIN32
void path_ensure_not_existing(const string &path)
{


+ 2
- 0
src/std_utils.hh View File

@ -40,6 +40,8 @@ namespace pEp {
// file
std::ofstream file_create(const std::string &filename);
std::string file_read(const std::string &filename);
std::vector<char> file_read_bin(const std::string& filename);
void file_write_bin(const std::string& filename, std::vector<char>& data);
// dir
#ifndef WIN32


Loading…
Cancel
Save