Skip to content

Commit

Permalink
Import C function from RCurl to avoid R CMD check problem
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed Aug 23, 2014
1 parent 49b5740 commit 25eebe3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,5 @@ exportClasses(Token)
exportClasses(Token1.0)
exportClasses(Token2.0)
import(stringr)
useDynLib(httr,close_file)
useDynLib(httr,writer)
3 changes: 2 additions & 1 deletion R/write-function.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ write_opts.write_disk <- function(x) {
)
}
#' @export
#' @useDynLib httr close_file
write_term.write_disk <- function(x) {
.Call("R_closeCFILE", x$file@ref, PACKAGE = "RCurl")
.Call(close_file, x$file@ref)
x$file <- NULL
path(x$path)
}
Expand Down
16 changes: 16 additions & 0 deletions src/writer.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include <stdio.h>
#include <R.h>
#include <Rdefines.h>

// From http://stackoverflow.com/questions/17329288/
// License: http://creativecommons.org/licenses/by-sa/3.0/
Expand All @@ -7,3 +9,17 @@ size_t writer(void *buffer, size_t size, size_t nmemb, void *stream) {
fwrite(buffer, size, nmemb, (FILE *) stream);
return size * nmemb;
}

// From: RCurl
// BSD_3_clause
// YEAR: 2001-2014
// COPYRIGHT HOLDER: Duncan Temple Lang
// ORGANIZATION: Bell Labs, Lucent Technologies; University of California
void close_file(SEXP r_file) {
FILE *f = (FILE *) R_ExternalPtrAddr(r_file);
if (f) {
fflush(f);
fclose(f);
R_SetExternalPtrAddr(r_file, NULL);
}
}

0 comments on commit 25eebe3

Please sign in to comment.