comparison R/rdyncall/src/rutils_str.c @ 0:0cfcc391201f

initial from svn dyncall-1745
author Daniel Adler
date Thu, 19 Mar 2015 22:26:28 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:0cfcc391201f
1 /** ===========================================================================
2 ** R-Package: rdyncall
3 ** File: src/rutils_str.c
4 ** Description: Support functions for handling C string data types.
5 **/
6
7 #define USE_RINTERNALS
8 #include <Rdefines.h>
9 #include <Rinternals.h>
10 #include <R_ext/RS.h>
11
12 /* String utils */
13
14 SEXP r_ptr2str(SEXP extptr)
15 {
16 void* addr = R_ExternalPtrAddr(extptr);
17 if (addr == NULL) {
18 return R_NilValue;
19 }
20 return mkString(addr);
21 }
22
23 SEXP r_strptr(SEXP x)
24 {
25 return R_MakeExternalPtr( (void*) CHAR(STRING_ELT(x, 0)), R_NilValue, x );
26 }
27
28 void do_free(SEXP x)
29 {
30 void* addr = R_ExternalPtrAddr(x);
31 R_Free(addr);
32 }
33
34 SEXP r_strarrayptr(SEXP s)
35 {
36 int i;
37 int n;
38 const char ** ptrs;
39
40 n = LENGTH(s);
41
42 // allocate array
43 ptrs = R_Calloc(n, const char*);
44
45 // copy cstring pointers into array
46 for( i=0 ; i<n ; ++i ) ptrs[i] = CHAR( STRING_ELT(s, i) );
47
48 // create external pointer pointing to array
49 SEXP x = PROTECT( R_MakeExternalPtr( ptrs, R_NilValue, s ) );
50 R_RegisterCFinalizerEx( x, do_free, TRUE );
51 UNPROTECT(1);
52 return x;
53 }