Browse Source

add function slurp(), coming from JsonAdapter

LPA-1
Roker 4 years ago
parent
commit
785c8991a1
2 changed files with 36 additions and 0 deletions
  1. +22
    -0
      slurp.cc
  2. +14
    -0
      slurp.hh

+ 22
- 0
slurp.cc View File

@ -0,0 +1,22 @@
#include "slurp.hh"
#include <fstream>
#include <sstream>
#include <stdexcept>
namespace pEp
{
std::string slurp(const std::string& filename)
{
std::ifstream input(filename.c_str(), std::ios_base::binary);
if(!input)
{
throw std::runtime_error("Cannot read file \"" + filename + "\"! ");
}
std::stringstream sstr;
sstr << input.rdbuf();
return sstr.str();
}
} // end of namespace pEp

+ 14
- 0
slurp.hh View File

@ -0,0 +1,14 @@
#ifndef PEP_LIB_SLURP_HH
#define PEP_LIB_SLURP_HH
#include <string>
namespace pEp
{
// reads a whole file and returns it as std::string
// throws std::runtime_error() if the file cannot be read. Empty file is not an error.
std::string slurp(const std::string& filename);
} // end of namespace pEp
#endif // PEP_LIB_SLURP_HH

Loading…
Cancel
Save