Browse Source

add slurp() function from pEpEngine's test code.

JSON-23
Roker 5 years ago
parent
commit
c93cba8833
3 changed files with 34 additions and 1 deletions
  1. +1
    -0
      server/Makefile
  2. +27
    -0
      server/pep-utils.cc
  3. +6
    -1
      server/pep-utils.hh

+ 1
- 0
server/Makefile View File

@ -36,6 +36,7 @@ libjson-adapter.a: \
security-token.o \
nfc_sets.o base64.o \
nulllogger.o \
pep-utils.o \
daemonize.o
ar rcs $@ $^


+ 27
- 0
server/pep-utils.cc View File

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

+ 6
- 1
server/pep-utils.hh View File

@ -3,12 +3,12 @@
#include <list>
#include <mutex>
#include <condition_variable>
#include <string>
namespace pEp
{
namespace utility
{
// a thread-safe queue of T elements. Deleter is a functor that is called in clear() for each elements
// interface differs from std::queue because "top() and pop() if not empty()" does not work atomically!
// elements must be copied without exceptions!
@ -138,6 +138,11 @@ namespace pEp
return _q.empty();
}
};
// 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::util
} // end of namespace pEp


Loading…
Cancel
Save