0
|
1 /** ===========================================================================
|
|
2 ** R-Package: rdyncall
|
|
3 ** File: src/rutils.c
|
|
4 ** Description: misc utility functions to work with low-level data structures in R
|
|
5 **/
|
|
6
|
|
7 // uses: DATAPTR macro
|
|
8 #define USE_RINTERNALS
|
|
9 #include <Rinternals.h>
|
|
10 #include <stddef.h>
|
|
11
|
|
12 SEXP r_isnullptr(SEXP x)
|
|
13 {
|
|
14 return ScalarLogical( ( R_ExternalPtrAddr(x) == NULL ) ? TRUE : FALSE );
|
|
15 }
|
|
16
|
|
17 SEXP r_asextptr(SEXP x)
|
|
18 {
|
|
19 if (isVector(x)) {
|
|
20 return R_MakeExternalPtr( DATAPTR(x), R_NilValue, x );
|
|
21 }
|
|
22 error("expected a vector type");
|
|
23 return R_NilValue; /* dummy */
|
|
24 }
|
|
25
|
|
26 SEXP r_offsetPtr(SEXP x, SEXP offset)
|
|
27 {
|
|
28 if ( LENGTH(offset) == 0 ) error("offset is missing");
|
|
29 ptrdiff_t offsetval = INTEGER(offset)[0];
|
|
30 unsigned char* ptr = 0;
|
|
31 if (isVector(x)) {
|
|
32 ptr = (unsigned char*) DATAPTR(x);
|
|
33 } else if (TYPEOF(x) == EXTPTRSXP ) {
|
|
34 ptr = (unsigned char*) R_ExternalPtrAddr(x);
|
|
35 } else {
|
|
36 error("unsupported type");
|
|
37 }
|
|
38 return R_MakeExternalPtr( ptr + offsetval , R_NilValue, x );
|
|
39 }
|
|
40
|