+static inline std::string stream2string(const std::istream &istream) {
+ return dynamic_cast<std::ostringstream &&>(std::ostringstream{} << istream.rdbuf()).str();
+}
+
+static memcached_return_t memcp(const client_options &opt, memcached_st &memc, const char *key,
+ const memcp_file &file) {
+ std::ifstream fstream{};
+ std::istream *istream = check_istream(opt, file.path, fstream);
+
+ if (!istream){
+ return MEMCACHED_ERROR;
+ }
+
+ const char *mode;
+ memcached_return_t rc;
+ auto data = stream2string(*istream);
+ if (file.op == memcp_file::mode::REPLACE) {
+ mode = "replace";
+ rc = memcached_replace(&memc, key, strlen(key), data.c_str(), data.length(),
+ file.expire, file.flags);
+ } else if (file.op == memcp_file::mode::ADD) {
+ mode = "add";
+ rc = memcached_add(&memc, key, strlen(key), data.c_str(), data.length(),
+ file.expire, file.flags);
+ } else {
+ mode = "set";
+ rc = memcached_set(&memc, key, strlen(key), data.c_str(), data.length(),
+ file.expire, file.flags);
+ }
+
+ if (!memcached_success(rc)) {
+ auto error = memcached_last_error(&memc)
+ ? memcached_last_error_message(&memc)
+ : memcached_strerror(&memc, rc);
+ std::cerr << "Error occurred during memcached_" << mode <<"('" << key << "'): " << error << "\n";
+ }
+ return rc;
+}
+
+static void add_file(std::vector<memcp_file> &files, const client_options &opt, char *file) {