Here is complete answer, relying on two other StackOverflow answers:
- one for one of a bazillion possible ways to read a file line by line and write the result out
- and one for the somewhat for interesting part of getting md5 from Boost so that we don't have to link.
This now works with one simple sourceCpp()
call provided you have CRAN package BH
installed.
Demo
> sourceCpp("~/git/stackoverflow/65838609/answer.cpp")
> hasher("/home/edd/git/stackoverflow/65838609/answer.cpp", "/tmp/md5.txt")
[1] TRUE
>
Code
#define BOOST_NO_AUTO_PTR
#include <Rcpp.h>
#include <fstream>
#include <boost/uuid/detail/md5.hpp>
#include <boost/algorithm/hex.hpp>
// [[Rcpp::depends(BH)]]
using boost::uuids::detail::md5;
// [[Rcpp::export]]
bool hasher(const std::string& infilename, const std::string& outfilename) {
// cf https://stackoverflow.com/questions/48545330/c-fastest-way-to-read-file-line-by-line
// https://stackoverflow.com/questions/55070320/how-to-calculate-md5-of-a-file-using-boost
std::ifstream infile(infilename);
std::ofstream outfile(outfilename);
std::string line;
while (std::getline(infile, line)) {
// line contains the current line
md5 hash;
md5::digest_type digest;
hash.process_bytes(line.data(), line.size());
hash.get_digest(digest);
const auto charDigest = reinterpret_cast<const char *>(&digest);
std::string res;
boost::algorithm::hex(charDigest, charDigest + sizeof(md5::digest_type), std::back_inserter(res));
outfile << res << '
';
}
outfile.close();
infile.close();
return true;
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…