From 785c8991a103953df7da18a275b482a6e8028235 Mon Sep 17 00:00:00 2001 From: Roker Date: Wed, 17 Oct 2018 11:22:21 +0200 Subject: [PATCH] add function slurp(), coming from JsonAdapter --- slurp.cc | 22 ++++++++++++++++++++++ slurp.hh | 14 ++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 slurp.cc create mode 100644 slurp.hh diff --git a/slurp.cc b/slurp.cc new file mode 100644 index 0000000..15519ab --- /dev/null +++ b/slurp.cc @@ -0,0 +1,22 @@ +#include "slurp.hh" +#include +#include +#include + +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 diff --git a/slurp.hh b/slurp.hh new file mode 100644 index 0000000..e990a99 --- /dev/null +++ b/slurp.hh @@ -0,0 +1,14 @@ +#ifndef PEP_LIB_SLURP_HH +#define PEP_LIB_SLURP_HH + +#include + +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 \ No newline at end of file