Created
July 24, 2019 21:02
-
-
Save traversc/e04911a86c8d581b058815d4aa7e7366 to your computer and use it in GitHub Desktop.
Using file descriptors directly in R
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cerrno> | |
#include <fcntl.h> | |
#include <cstdio> | |
#include <unistd.h> | |
#include <string.h> | |
#include <array> | |
#include <Rcpp.h> | |
using namespace Rcpp; | |
// [[Rcpp::plugins(cpp11)]] | |
// https://cboard.cprogramming.com/showthread.php?t=93683 | |
int fd_is_valid(int fd) { | |
return fcntl(fd, F_GETFD) != -1 || errno != EBADF; | |
} | |
// [[Rcpp::export]] | |
int makeFdForWrite(std::string filename) { | |
int fd = open(filename.c_str(), O_WRONLY | O_CREAT, 0644); | |
if(!fd_is_valid(fd)) Rcpp::stop("somthing went wrong :("); | |
return fd; | |
} | |
// [[Rcpp::export]] | |
void writeToFd(int fd) { | |
if(!fd_is_valid(fd)) Rcpp::stop("somthing went wrong :("); | |
std::array<unsigned char, 17> mydata = | |
{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}; | |
int bytes_written = write(fd, mydata.data(), mydata.size()); | |
if(bytes_written != mydata.size()) Rcpp::stop("somthing went wrong :("); | |
close(fd); | |
} | |
// [[Rcpp::export]] | |
int makeFdForRead(std::string filename) { | |
int fd = open(filename.c_str(), O_RDONLY); | |
if(!fd_is_valid(fd)) Rcpp::stop("somthing went wrong :("); | |
return fd; | |
} | |
// [[Rcpp::export]] | |
std::vector<unsigned char> readFromFd(int fd) { | |
if(!fd_is_valid(fd)) Rcpp::stop("somthing went wrong :("); | |
std::vector<unsigned char> output; | |
std::array<unsigned char, 4> temp = {0,0,0,0}; | |
int bytes_read; | |
while( (bytes_read = read(fd, temp.data(), 4)) > 0 ) { | |
output.insert(std::end(output), std::begin(temp), std::end(temp)); | |
} | |
close(fd); | |
return output; | |
} | |
/*** R | |
setwd("~") | |
write_fd <- makeFdForWrite("fdtest.bin") | |
writeToFd(write_fd) | |
readBin("fdtest.bin", what="raw", n=1e9) | |
read_fd <- makeFdForRead("fdtest.bin") | |
readFromFd(read_fd) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment