-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfileio.R
44 lines (42 loc) · 1003 Bytes
/
fileio.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
if (require(inline, quietly=TRUE)){
globals="
#include <stdio.h>
#include <string.h>
#include <ctype.h>
FILE *con;
#define MAX_STRING_SIZE 7168
char input_string[MAX_STRING_SIZE];
"
io <- cfunction(
otherdefs = globals,
sig=list(Rfopen=signature(file="character"), Rfgets=signature(), Rfclose=signature()),
body=list(
'
const char *fname = CHAR(STRING_ELT(file,0));
con = fopen(fname,"r");
return ScalarLogical((con!=NULL)? TRUE: FALSE);
',
'
if (fgets(input_string, MAX_STRING_SIZE-1, con )!=NULL){
input_string[strlen(input_string)-1] = 0;
return ScalarString(mkChar(input_string));
} else {
return R_NilValue;
}
',
'
fclose(con);
return ScalarLogical(TRUE);
'
)
)
file <- function(fname, open="r"){
io$Rfopen(fname)
}
readLines <- function(con, n=1){
io$Rfgets()
}
close <- function(con){
io$Rfclose()
}
}