comparison R/rdyncall/src/rpack.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/rpack.c
4 ** Description: (un-)packing of C structure data
5 ** TODO
6 ** - support for bitfields
7 **/
8
9 // #define USE_RINTERNALS
10 #include <Rinternals.h>
11 #include <string.h>
12 #include <stddef.h>
13 #include "rdyncall_signature.h"
14 /** ---------------------------------------------------------------------------
15 ** C-Function: r_dataptr
16 ** Description: retrieve the 'data' pointer on an R expression.
17 ** R-Calling Convention: .Call
18 **
19 **/
20
21 static char* r_dataptr(SEXP x, SEXP off, size_t element_size)
22 {
23 if ( LENGTH(off) == 0 ) error("missing offset");
24 char* p = NULL;
25 ptrdiff_t o = INTEGER(off)[0], s = 0;
26
27 switch(TYPEOF(x))
28 {
29 case CHARSXP: p = (char*) CHAR(x); s = LENGTH(x)*sizeof(char); break;
30 case LGLSXP: p = (char*) LOGICAL(x); s = LENGTH(x)*sizeof(Rboolean); break;
31 case INTSXP: p = (char*) INTEGER(x); s = LENGTH(x)*sizeof(int); break;
32 case REALSXP: p = (char*) REAL(x); s = LENGTH(x)*sizeof(double); break;
33 case CPLXSXP: p = (char*) COMPLEX(x); s = LENGTH(x)*sizeof(Rcomplex); break;
34 case STRSXP: p = (char*) CHAR( STRING_ELT(x,0) ); s = strlen(p)*sizeof(char); break;
35 case RAWSXP: p = (char*) RAW(x); s = LENGTH(x)*sizeof(char); break;
36 case EXTPTRSXP: return (char*) R_ExternalPtrAddr(x) + o; break;
37 default: error("invalid object type"); break;
38 }
39 if (p == NULL) error("NULL address pointer");
40 if (o < 0 || o+element_size > s) error("offset %d is out-of-bounds of the R object (max size %d)", o, s);
41 return p + o;
42 }
43
44
45
46 /** ---------------------------------------------------------------------------
47 ** C-Function: r_pack
48 ** Description: pack R data type into a C data type
49 ** R-Calling Convention: .Call
50 **
51 **/
52 SEXP r_pack(SEXP ptr_x, SEXP offset, SEXP sig_x, SEXP value_x)
53 {
54 int type_of = TYPEOF(value_x);
55 const char* sig = CHAR(STRING_ELT(sig_x,0) );
56 switch(sig[0])
57 {
58 case DC_SIGCHAR_BOOL:
59 {
60 int* Bp = (int*) r_dataptr(ptr_x, offset, sizeof(Rboolean));
61 switch(type_of)
62 {
63 case LGLSXP: *Bp = (int) LOGICAL(value_x)[0]; break;
64 case INTSXP: *Bp = (int) ( INTEGER(value_x)[0] == 0) ? 0 : 1; break;
65 case REALSXP: *Bp = (int) ( REAL(value_x)[0] == 0.0) ? 0 : 1; break;
66 case RAWSXP: *Bp = (int) ( RAW(value_x)[0] == 0) ? 0 : 1; break;
67 default: error("value mismatch with 'B' pack type");
68 }
69 }
70 break;
71 case DC_SIGCHAR_CHAR:
72 {
73 char* cp = (char*) r_dataptr(ptr_x, offset, sizeof(char));
74 switch(type_of)
75 {
76 case LGLSXP: *cp = (char) LOGICAL(value_x)[0]; break;
77 case INTSXP: *cp = (char) INTEGER(value_x)[0]; break;
78 case REALSXP: *cp = (char) REAL(value_x)[0]; break;
79 case RAWSXP: *cp = (char) RAW(value_x)[0]; break;
80 default: error("value mismatch with 'c' pack type");
81 }
82 }
83 break;
84 case DC_SIGCHAR_UCHAR:
85 {
86 unsigned char* cp = (unsigned char*) r_dataptr(ptr_x,offset,sizeof(unsigned char));
87 switch(type_of)
88 {
89 case LGLSXP: *cp = (unsigned char) LOGICAL(value_x)[0]; break;
90 case INTSXP: *cp = (unsigned char) INTEGER(value_x)[0]; break;
91 case REALSXP: *cp = (unsigned char) REAL(value_x)[0]; break;
92 case RAWSXP: *cp = (unsigned char) RAW(value_x)[0]; break;
93 default: error("value mismatch with 'C' pack type");
94 }
95 }
96 break;
97 case DC_SIGCHAR_SHORT:
98 {
99 short* sp = (short*) r_dataptr(ptr_x,offset,sizeof(short));
100 switch(type_of)
101 {
102 case LGLSXP: *sp = (short) LOGICAL(value_x)[0]; break;
103 case INTSXP: *sp = (short) INTEGER(value_x)[0]; break;
104 case REALSXP: *sp = (short) REAL(value_x)[0]; break;
105 case RAWSXP: *sp = (short) RAW(value_x)[0]; break;
106 default: error("value mismatch with 's' pack type");
107 }
108 }
109 break;
110 case DC_SIGCHAR_USHORT:
111 {
112 unsigned short* sp = (unsigned short*) r_dataptr(ptr_x,offset,sizeof(unsigned short));
113 switch(type_of)
114 {
115 case LGLSXP: *sp = (unsigned short) LOGICAL(value_x)[0]; break;
116 case INTSXP: *sp = (unsigned short) INTEGER(value_x)[0]; break;
117 case REALSXP: *sp = (unsigned short) REAL(value_x)[0]; break;
118 case RAWSXP: *sp = (unsigned short) RAW(value_x)[0]; break;
119 default: error("value mismatch with 'S' pack type");
120 }
121 }
122 break;
123 case DC_SIGCHAR_INT:
124 {
125 int* ip = (int*) r_dataptr(ptr_x,offset,sizeof(int));
126 switch(type_of)
127 {
128 case LGLSXP: *ip = (int) LOGICAL(value_x)[0]; break;
129 case INTSXP: *ip = (int) INTEGER(value_x)[0]; break;
130 case REALSXP: *ip = (int) REAL(value_x)[0]; break;
131 case RAWSXP: *ip = (int) RAW(value_x)[0]; break;
132 default: error("value mismatch with 'i' pack type");
133 }
134 }
135 break;
136 case DC_SIGCHAR_UINT:
137 {
138 unsigned int* ip = (unsigned int*) r_dataptr(ptr_x,offset,sizeof(unsigned int));
139 switch(type_of)
140 {
141 case LGLSXP: *ip = (unsigned int) LOGICAL(value_x)[0]; break;
142 case INTSXP: *ip = (unsigned int) INTEGER(value_x)[0]; break;
143 case REALSXP: *ip = (unsigned int) REAL(value_x)[0]; break;
144 case RAWSXP: *ip = (unsigned int) RAW(value_x)[0]; break;
145 default: error("value mismatch with 'I' pack type");
146 }
147 }
148 break;
149 case DC_SIGCHAR_LONG:
150 {
151 long* ip = (long*) r_dataptr(ptr_x,offset,sizeof(long));
152 switch(type_of)
153 {
154 case LGLSXP: *ip = (long) LOGICAL(value_x)[0]; break;
155 case INTSXP: *ip = (long) INTEGER(value_x)[0]; break;
156 case REALSXP: *ip = (long) REAL(value_x)[0]; break;
157 case RAWSXP: *ip = (long) RAW(value_x)[0]; break;
158 default: error("value mismatch with 'j' pack type");
159 }
160 }
161 break;
162 case DC_SIGCHAR_ULONG:
163 {
164 unsigned long* ip = (unsigned long*) r_dataptr(ptr_x,offset,sizeof(unsigned long));
165 switch(type_of)
166 {
167 case LGLSXP: *ip = (unsigned long) LOGICAL(value_x)[0]; break;
168 case INTSXP: *ip = (unsigned long) INTEGER(value_x)[0]; break;
169 case REALSXP: *ip = (unsigned long) REAL(value_x)[0]; break;
170 case RAWSXP: *ip = (unsigned long) RAW(value_x)[0]; break;
171 default: error("value mismatch with 'J' pack type");
172 }
173 }
174 break;
175 case DC_SIGCHAR_LONGLONG:
176 {
177 long long* Lp = (long long*) r_dataptr(ptr_x,offset,sizeof(long long));
178 switch(type_of)
179 {
180 case LGLSXP: *Lp = (long long) LOGICAL(value_x)[0]; break;
181 case INTSXP: *Lp = (long long) INTEGER(value_x)[0]; break;
182 case REALSXP: *Lp = (long long) REAL(value_x)[0]; break;
183 case RAWSXP: *Lp = (long long) RAW(value_x)[0]; break;
184 default: error("value mismatch with 'l' pack type");
185 }
186 }
187 break;
188 case DC_SIGCHAR_ULONGLONG:
189 {
190 unsigned long long* Lp = (unsigned long long*) r_dataptr(ptr_x,offset,sizeof(unsigned long long));
191 switch(type_of)
192 {
193 case LGLSXP: *Lp = (unsigned long long) LOGICAL(value_x)[0]; break;
194 case INTSXP: *Lp = (unsigned long long) INTEGER(value_x)[0]; break;
195 case REALSXP: *Lp = (unsigned long long) REAL(value_x)[0]; break;
196 case RAWSXP: *Lp = (unsigned long long) RAW(value_x)[0]; break;
197 default: error("value mismatch with 'L' pack type");
198 }
199 }
200 break;
201 case DC_SIGCHAR_FLOAT:
202 {
203 float* fp = (float*) r_dataptr(ptr_x,offset,sizeof(float));
204 switch(type_of)
205 {
206 case LGLSXP: *fp = (float) LOGICAL(value_x)[0]; break;
207 case INTSXP: *fp = (float) INTEGER(value_x)[0]; break;
208 case REALSXP: *fp = (float) REAL(value_x)[0]; break;
209 case RAWSXP: *fp = (float) RAW(value_x)[0]; break;
210 default: error("value mismatch with 'f' pack type");
211 }
212 }
213 break;
214 case DC_SIGCHAR_DOUBLE:
215 {
216 double* dp = (double*) r_dataptr(ptr_x,offset,sizeof(double));
217 switch(type_of)
218 {
219 case LGLSXP: *dp = (double) LOGICAL(value_x)[0]; break;
220 case INTSXP: *dp = (double) INTEGER(value_x)[0]; break;
221 case REALSXP: *dp = (double) REAL(value_x)[0]; break;
222 case RAWSXP: *dp = (double) RAW(value_x)[0]; break;
223 default: error("value mismatch with 'd' pack type");
224 }
225 }
226 break;
227 case DC_SIGCHAR_POINTER:
228 case '*':
229 {
230 void** pp = (void**) r_dataptr(ptr_x,offset,sizeof(void*));
231 switch(type_of)
232 {
233 case NILSXP: *pp = (void*) 0; break;
234 case CHARSXP: *pp = (void*) CHAR(value_x); break;
235 case LGLSXP: *pp = (void*) LOGICAL(value_x); break;
236 case INTSXP: *pp = (void*) INTEGER(value_x); break;
237 case REALSXP: *pp = (void*) REAL(value_x); break;
238 case CPLXSXP: *pp = (void*) COMPLEX(value_x); break;
239 case STRSXP: *pp = (void*) CHAR( STRING_ELT(value_x,0) ); break;
240 case EXTPTRSXP:*pp = (void*) R_ExternalPtrAddr(value_x); break;
241 case RAWSXP: *pp = (void*) RAW(value_x); break;
242 default: error("value type mismatch with 'p' pack type");
243 }
244 }
245 break;
246 case DC_SIGCHAR_STRING:
247 {
248 char** Sp = (char**) r_dataptr(ptr_x,offset,sizeof(char*));
249 switch(type_of)
250 {
251 case NILSXP: *Sp = (char*) NULL; break;
252 case CHARSXP: *Sp = (char*) CHAR(value_x); break;
253 case STRSXP: *Sp = (char*) CHAR( STRING_ELT(value_x,0) ); break;
254 case EXTPTRSXP:*Sp = (char*) R_ExternalPtrAddr(value_x); break;
255 default: error("value type mismatch with 'Z' pack type");
256 }
257 }
258 break;
259 case DC_SIGCHAR_SEXP:
260 {
261 SEXP* px = (SEXP*) r_dataptr(ptr_x,offset,sizeof(SEXP*));
262 *px = value_x;
263 }
264 break;
265 default: error("invalid signature");
266 }
267 return R_NilValue;
268 }
269
270 /** ---------------------------------------------------------------------------
271 ** C-Function: r_unpack
272 ** Description: unpack elements from C-like structures to R values.
273 ** R-Calling Convention: .Call
274 **
275 **/
276 SEXP r_unpack(SEXP ptr_x, SEXP offset, SEXP sig_x)
277 {
278 char* ptr = NULL;
279 const char* sig = CHAR(STRING_ELT(sig_x,0) );
280 switch(sig[0])
281 {
282 case DC_SIGCHAR_BOOL:
283 ptr = r_dataptr(ptr_x,offset,sizeof(Rboolean));
284 return ScalarLogical( ((int*)ptr)[0] );
285 case DC_SIGCHAR_CHAR:
286 ptr = r_dataptr(ptr_x,offset,sizeof(char));
287 return ScalarInteger( ( (char*)ptr)[0] );
288 case DC_SIGCHAR_UCHAR:
289 ptr = r_dataptr(ptr_x,offset,sizeof(unsigned char));
290 return ScalarInteger( ( (unsigned char*)ptr)[0] );
291 case DC_SIGCHAR_SHORT:
292 ptr = r_dataptr(ptr_x,offset,sizeof(short));
293 return ScalarInteger( ( (short*)ptr)[0] );
294 case DC_SIGCHAR_USHORT:
295 ptr = r_dataptr(ptr_x,offset,sizeof(unsigned short));
296 return ScalarInteger( ( (unsigned short*)ptr)[0] );
297 case DC_SIGCHAR_INT:
298 ptr = r_dataptr(ptr_x,offset,sizeof(int));
299 return ScalarInteger( ( (int*)ptr )[0] );
300 case DC_SIGCHAR_UINT:
301 ptr = r_dataptr(ptr_x,offset,sizeof(unsigned int));
302 return ScalarReal( (double) ( (unsigned int*)ptr )[0] );
303 case DC_SIGCHAR_LONG:
304 ptr = r_dataptr(ptr_x,offset,sizeof(long));
305 return ScalarReal( (double) ( (long*)ptr )[0] );
306 case DC_SIGCHAR_ULONG:
307 ptr = r_dataptr(ptr_x,offset,sizeof(unsigned long));
308 return ScalarReal( (double) ( (unsigned long*) ptr )[0] );
309 case DC_SIGCHAR_FLOAT:
310 ptr = r_dataptr(ptr_x,offset,sizeof(float));
311 return ScalarReal( (double) ( (float*) ptr )[0] );
312 case DC_SIGCHAR_DOUBLE:
313 ptr = r_dataptr(ptr_x,offset,sizeof(double));
314 return ScalarReal( ((double*)ptr)[0] );
315 case DC_SIGCHAR_LONGLONG:
316 ptr = r_dataptr(ptr_x,offset,sizeof(long long));
317 return ScalarReal( (double) ( ((long long*)ptr)[0] ) );
318 case DC_SIGCHAR_ULONGLONG:
319 ptr = r_dataptr(ptr_x,offset,sizeof(unsigned long long));
320 return ScalarReal( (double) ( ((unsigned long long*)ptr)[0] ) );
321 case '*':
322 case DC_SIGCHAR_POINTER:
323 ptr = r_dataptr(ptr_x,offset,sizeof(void*));
324 return R_MakeExternalPtr( ((void**)ptr)[0] , R_NilValue, R_NilValue );
325 case DC_SIGCHAR_STRING: {
326 ptr = r_dataptr(ptr_x,offset,sizeof(char*));
327 char* s = ( (char**) ptr )[0];
328 if (s == NULL) return R_MakeExternalPtr( 0, R_NilValue, R_NilValue );
329 return mkString(s);
330 }
331 case DC_SIGCHAR_SEXP:
332 return (SEXP) ptr;
333 default: error("invalid signature");
334 }
335 return R_NilValue;
336 }