[Guile] New example.

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@872 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Matthias Köppe 2000-09-21 21:03:32 +00:00
commit 6d4a3945dd
5 changed files with 83 additions and 0 deletions

View file

@ -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

View file

@ -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

View file

@ -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;
}

View file

@ -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);

View file

@ -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)))