annotate R/rdc/demo/stdio.R @ 18:3bfef07b0cd9

- cleanup of non-working java binding, restart work, somewhat different interface
author cslag
date Wed, 30 Mar 2016 02:05:06 +0200
parents 0cfcc391201f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
1 if (.Platform$OS.type == "windows") {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
2 .libC <- "/windows/system32/msvcrt"
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
3 } else if ( Sys.info()[["sysname"]] == "Darwin" ) {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
4 .libC <- "/usr/lib/libc.dylib"
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
5 } else {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
6 .libC <- "/lib/libc.so.6"
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
7 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
8
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
9 .libC <- rdcLoad(.libC)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
10 .fopen <- rdcFind(.libC, "fopen")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
11 .fwrite <- rdcFind(.libC, "fwrite")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
12 .fread <- rdcFind(.libC, "fread")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
13 .fclose <- rdcFind(.libC, "fclose")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
14 .fprintf <- rdcFind(.libC, "fprintf")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
15
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
16 fopen <- function(name, mode)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
17 rdcCall(.fopen, "SS)p",name,mode)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
18 fread <- function(buf, size, count, fp)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
19 rdcCall(.fread,"piip)i", buf, size, count, fp)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
20 fwrite <- function(buf, size, count, fp)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
21 rdcCall(.fwrite, "piip)i", buf, size, count, fp)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
22 fclose <- function(fp)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
23 rdcCall(.fclose, "p)i", fp)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
24
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
25
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
26
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
27 do.write <- function(filename, x)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
28 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
29 fh <- fopen(filename, "wb")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
30 error <- FALSE
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
31 offset <- 0L
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
32 size <- rdcSizeOf("double")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
33 count <- length(x)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
34 while( count > 0 && !error )
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
35 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
36 nwritten <- fwrite( rdcDataPtr(x,offset), size, count, fh )
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
37 if (nwritten < 0)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
38 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
39 error <- TRUE
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
40 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
41 else
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
42 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
43 count <- count - nwritten
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
44 offset <- offset + nwritten * size
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
45 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
46 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
47 fclose(fh)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
48 if (error)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
49 stop("fwrite error")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
50 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
51
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
52 do.read <- function(filename, x)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
53 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
54 fh <- fopen(filename, "rb")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
55 error <- FALSE
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
56 offset <- 0L
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
57 size <- rdcSizeOf("double")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
58 count <- length(x)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
59 while (size > 0 && !error )
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
60 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
61 nread <- fread( rdcDataPtr(x,offset), size, count, fh )
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
62 if (nread < 0)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
63 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
64 error <- TRUE
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
65 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
66 else
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
67 {
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
68 size <- size - nread
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
69 offset <- offset + nread * size
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
70 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
71 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
72 fclose(fh)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
73 if (error)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
74 stop("fread error")
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
75 }
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
76
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
77 filename <- tempfile()
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
78 x <- rnorm(1000)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
79 do.write(filename, x)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
80 y <- numeric(1000)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
81 do.read(filename,y)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
82 identical(x,y)
0cfcc391201f initial from svn dyncall-1745
Daniel Adler
parents:
diff changeset
83