aboutsummaryrefslogtreecommitdiff
path: root/Examples/guile
diff options
context:
space:
mode:
authorMatthias Köppe <mkoeppe@mail.math.uni-magdeburg.de>2000-09-21 21:03:32 +0000
committerMatthias Köppe <mkoeppe@mail.math.uni-magdeburg.de>2000-09-21 21:03:32 +0000
commit6d4a3945ddc5ecc5e2e7cbc5536b814715b5cf75 (patch)
tree2bdb5633c86429251cbaeea19b35f419ff58d217 /Examples/guile
parent4baedcf6505b8a2658a224bfbf50b1137fbdd50d (diff)
downloadswig-6d4a3945ddc5ecc5e2e7cbc5536b814715b5cf75.tar.gz
[Guile] New example.
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@872 626c5289-ae23-0410-ae9c-e8d60b6d4f22
Diffstat (limited to 'Examples/guile')
-rw-r--r--Examples/guile/port/Makefile20
-rw-r--r--Examples/guile/port/README2
-rw-r--r--Examples/guile/port/port.c18
-rw-r--r--Examples/guile/port/port.i11
-rw-r--r--Examples/guile/port/port.scm32
5 files changed, 83 insertions, 0 deletions
diff --git a/Examples/guile/port/Makefile b/Examples/guile/port/Makefile
new file mode 100644
index 000000000..668c66bd3
--- /dev/null
+++ b/Examples/guile/port/Makefile
@@ -0,0 +1,20 @@
+CC = gcc
+SRCS = port.c
+TARGET = port
+IFILE = port.i
+MKDIR = ..
+
+
+all::
+ $(MAKE) -f $(MKDIR)/Makefile \
+ SRCS='$(SRCS)' \
+ TARGET=$(TARGET) \
+ IFILE=$(IFILE) \
+ CC=$(CC) \
+ MODULE=$(MODULE) \
+ sub-all
+
+clean::
+ rm -f $(TARGET) *_wrap* *~ .~* core test.out
+
+check: all
diff --git a/Examples/guile/port/README b/Examples/guile/port/README
new file mode 100644
index 000000000..5ed0199e2
--- /dev/null
+++ b/Examples/guile/port/README
@@ -0,0 +1,2 @@
+This example illustrates the translation from Scheme file ports to
+temporary FILE streams. Read the source and run ./port -s port.scm
diff --git a/Examples/guile/port/port.c b/Examples/guile/port/port.c
new file mode 100644
index 000000000..95867b687
--- /dev/null
+++ b/Examples/guile/port/port.c
@@ -0,0 +1,18 @@
+#include <stdio.h>
+#include <errno.h>
+
+void print_int(FILE *f, int i)
+{
+ if (fprintf(f, "%d\n", i)<0)
+ perror("print_int");
+}
+
+int read_int(FILE *f)
+{
+ int i;
+ if (fscanf(f, "%d", &i)!=1) {
+ fprintf(stderr, "read_int: error reading from file\n");
+ perror("read_int");
+ }
+ return i;
+}
diff --git a/Examples/guile/port/port.i b/Examples/guile/port/port.i
new file mode 100644
index 000000000..553cfba23
--- /dev/null
+++ b/Examples/guile/port/port.i
@@ -0,0 +1,11 @@
+%include guilemain.i
+
+/* Include the required FILE * typemaps */
+%include ports.i
+
+%{
+#include <stdio.h>
+%}
+
+void print_int(FILE *f, int i);
+int read_int(FILE *f);
diff --git a/Examples/guile/port/port.scm b/Examples/guile/port/port.scm
new file mode 100644
index 000000000..68e9b8e85
--- /dev/null
+++ b/Examples/guile/port/port.scm
@@ -0,0 +1,32 @@
+;; Call with standard output
+(print-int (current-output-port) 314159)
+
+;; Redirection to a file. Note that the port is automatically flushed
+;; (via force-output) before calling the C function, and that the C
+;; function gets a temporary "FILE" stream, which is closed after the
+;; call. So you can simply mix Scheme and C output.
+(with-output-to-file "test.out"
+ (lambda ()
+ (display 4711)
+ (newline)
+ (print-int (current-output-port) 314159)
+ (display 815)
+ (newline)))
+
+;; Redirection to a string or soft port won't work --
+;; we can only handle file ports.
+(catch #t
+ (lambda ()
+ (with-output-to-string
+ (lambda ()
+ (print-int (current-output-port) 314159))))
+ (lambda args
+ (write args) (newline)))
+
+;; Read from a file port. Note that it is a bad idea to mix Scheme and
+;; C input because of buffering.
+(with-input-from-file "test.out"
+ (lambda ()
+ (display (read-int (current-input-port)))
+ (newline)))
+