[Rd] Patch to address (PR#7853) -- tested briefly, seems to work

2005-05-11 Thread keithf
--- dotcode.c2004-09-05 02:31:57.0 -0700
+++ /usr/local/src/R-2.0.0/src/main/dotcode.c2005-05-11 
12:24:11.0 -0700
@@ -190,6 +190,7 @@
 static void *RObjToCPtr(SEXP s, int naok, int dup, int narg, int Fort, 
const char *name, R_toCConverter **converter,
   int targetType)
 {
+unsigned char *rawptr;
 int *iptr;
 float *sptr;
 double *rptr;
@@ -228,6 +229,16 @@
 }
 
 switch(TYPEOF(s)) {
+case RAWSXP:
+n = LENGTH(s);
+rawptr = RAW(s);
+if (dup) {
+rawptr = (unsigned char *) R_alloc(n, sizeof(unsigned char));
+for (i = 0; i < n; i++)
+rawptr[i] = RAW(s)[i];
+}
+return (void *) rawptr;
+break;
 case LGLSXP:
 case INTSXP:
 n = LENGTH(s);
@@ -329,6 +340,7 @@
 
 static SEXP CPtrToRObj(void *p, SEXP arg, int Fort, 
R_NativePrimitiveArgType type)
 {
+unsigned char *rawptr;
 int *iptr, n=length(arg);
 float *sptr;
 double *rptr;
@@ -339,6 +351,12 @@
 SEXP s, t;
 
 switch(type) {
+case RAWSXP:
+s = allocVector(type, n);
+rawptr = (unsigned char *)p;
+for (i = 0; i < n; i++)
+RAW(s)[i] = rawptr[i];
+break;
 case LGLSXP:
 case INTSXP:
 s = allocVector(type, n);

__
R-devel@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


[Rd] Accessing Raw Mode Vectors from .C (PR#7853)

2005-05-11 Thread keithf
Full_Name: Keith Frost
Version: 2.0.0
OS: Linux i686 (RH 9)
Submission from: (NULL) (66.162.141.10)



If I create the C function:

void test_raw(unsigned char *raw, int *out)
{
  int i;
  for (i = 0; i <= 255; i++)
out[i] = raw[i];
}

and dyn.load the resulting library, then call

.C("test_raw", as.raw(255:0), integer(256), DUP=FALSE)[[2]];

the result is 
  [1]  24   0   0 224 160 116  31   8   0 158  93   8 160 184 107   8   0   1
 [19]   0   0 111  34  41   0 255 254 253 252 251 250 249 248 247 246 245 244
 [37] 243 242 241 240 239 238 237 236 235 234 233 232 231 230 229 228 227 226
 [55] 225 224 223 222 221 220 219 218 217 216 215 214 213 212 211 210 209 208
... <10 deleted lines> ...
[253]  27  26  25  24

It appears that the C code gets a pointer which points 25 bytes before the
beginning of the raw vector data in memory.  I know that this is not documented
to work, but it would be a *very* nice feature to have, and it looks like it
*almost* works  Hence this bug report.

__
R-devel@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel