Initial addition.
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@4313 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
eeace2a394
commit
6901abdf17
70 changed files with 9710 additions and 0 deletions
59
Examples/chicken/multimap/Makefile.in
Normal file
59
Examples/chicken/multimap/Makefile.in
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
@SET_MAKE@
|
||||
|
||||
srcdir = @srcdir@
|
||||
top_srcdir = @top_srcdir@
|
||||
VPATH = @srcdir@
|
||||
SOURCE_DIR = $(top_srcdir)/Examples/chicken/multimap
|
||||
TOP = ../..
|
||||
CC = @CC@
|
||||
CXX = @CXX@
|
||||
OBJEXT = @OBJEXT@
|
||||
EXEEXT = @EXEEXT@
|
||||
SWIG = $(TOP)/../swig$(EXEEXT)
|
||||
CHICKGEN = example_wrap.$(OBJEXT) csi.$(OBJEXT) precsi.$(OBJEXT) \
|
||||
oexample.$(OBJEXT)
|
||||
CHICKSRC = csi.c precsi.c oexample.c
|
||||
SRCS = $(CHICKGEN) $(SOURCE_DIR)/example.c
|
||||
TARGET = multimap$(EXEEXT)
|
||||
INCLUDE = -I$(SOURCE_DIR)
|
||||
SWIGOPT =
|
||||
|
||||
all:: $(TARGET)
|
||||
|
||||
.SUFFIXES: .cxx
|
||||
|
||||
.c.o:
|
||||
$(CC) $(INCLUDE) $(CFLAGS) -c -o $*.o $<
|
||||
|
||||
.cxx.o:
|
||||
$(CXX) $(INCLUDE) $(CFLAGS) -c -o $*.o $<
|
||||
|
||||
csi.c:
|
||||
$(MAKE) -f $(TOP)/Makefile TARGET='csi.c' \
|
||||
INTERFACE='precsi' chicken_csi
|
||||
|
||||
precsi.c: $(SOURCE_DIR)/precsi.scm
|
||||
$(MAKE) -f $(TOP)/Makefile TARGET='precsi.c' \
|
||||
INTERFACE='$<' chicken
|
||||
|
||||
example_wrap.c example.scm: $(SOURCE_DIR)/example.i
|
||||
(test $< -nt example.i && cp -p $< example.i) || echo example.i is up-to-date
|
||||
$(MAKE) -f $(TOP)/Makefile SWIG='$(SWIG)' SWIGOPT='$(SWIGOPT)' \
|
||||
INCLUDE='$(INCLUDE)' INTERFACE='example.i' chicken_c
|
||||
|
||||
oexample.c: example.scm
|
||||
$(MAKE) -f $(TOP)/Makefile TARGET='oexample.c' \
|
||||
INTERFACE='$<' chicken
|
||||
|
||||
$(TARGET): $(SRCS)
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
INCLUDE='$(INCLUDE)' SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' \
|
||||
INTERFACE='$(INTERFACE)' chicken_static
|
||||
|
||||
clean::
|
||||
rm -f *_wrap* *.$(OBJEXT) core *~ *.so *.stackdump STACKTRACE
|
||||
rm -f $(CHICKGEN) $(CHICKSRC)
|
||||
rm -f example.scm
|
||||
rm -f $(TARGET)
|
||||
|
||||
check: all
|
||||
53
Examples/chicken/multimap/example.c
Normal file
53
Examples/chicken/multimap/example.c
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
/* File : example.c */
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
|
||||
/* Compute the greatest common divisor of positive integers */
|
||||
int gcd(int x, int y) {
|
||||
int g;
|
||||
g = y;
|
||||
while (x > 0) {
|
||||
g = x;
|
||||
x = y % x;
|
||||
y = g;
|
||||
}
|
||||
return g;
|
||||
}
|
||||
|
||||
int gcdmain(int argc, char *argv[]) {
|
||||
int x,y;
|
||||
if (argc != 3) {
|
||||
printf("usage: gcd x y\n");
|
||||
return -1;
|
||||
}
|
||||
x = atoi(argv[1]);
|
||||
y = atoi(argv[2]);
|
||||
printf("gcd(%d,%d) = %d\n", x,y,gcd(x,y));
|
||||
return 0;
|
||||
}
|
||||
|
||||
int count(char *bytes, int len, char c) {
|
||||
int i;
|
||||
int count = 0;
|
||||
for (i = 0; i < len; i++) {
|
||||
if (bytes[i] == c) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
void capitalize(char *str, int len) {
|
||||
int i;
|
||||
for (i = 0; i < len; i++) {
|
||||
str[i] = toupper(str[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void circle(double x, double y) {
|
||||
double a = x*x + y*y;
|
||||
if (a > 1.0) {
|
||||
printf("Bad points %g, %g\n", x,y);
|
||||
} else {
|
||||
printf("Good points %g, %g\n", x,y);
|
||||
}
|
||||
}
|
||||
90
Examples/chicken/multimap/example.i
Normal file
90
Examples/chicken/multimap/example.i
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
%include exception.i
|
||||
%include typemaps.i
|
||||
|
||||
extern int gcd(int x, int y);
|
||||
|
||||
%typemap(chicken,in) (int argc, char *argv[]) {
|
||||
int i;
|
||||
if (!C_swig_is_vector ($input)) {
|
||||
swig_barf (SWIG_BARF1_BAD_ARGUMENT_TYPE, "Argument $input is not a vector");
|
||||
}
|
||||
$1 = C_header_size ($input);
|
||||
$2 = (char **) malloc(($1+1)*sizeof(char *));
|
||||
for (i = 0; i < $1; i++) {
|
||||
C_word o = C_block_item ($input, i);
|
||||
if (!C_swig_is_string (o)) {
|
||||
char err[50];
|
||||
free($2);
|
||||
sprintf (err, "$input[%d] is not a string", i);
|
||||
swig_barf (SWIG_BARF1_BAD_ARGUMENT_TYPE, err);
|
||||
}
|
||||
$2[i] = C_c_string (o);
|
||||
}
|
||||
$2[i] = 0;
|
||||
}
|
||||
|
||||
%typemap(chicken,freear) (int argc, char *argv[]) {
|
||||
free($2);
|
||||
}
|
||||
extern int gcdmain(int argc, char *argv[]);
|
||||
|
||||
%typemap(chicken,in) (char *bytes, int len) {
|
||||
if (!C_swig_is_string ($input)) {
|
||||
swig_barf (SWIG_BARF1_BAD_ARGUMENT_TYPE, "Argument $input is not a string");
|
||||
}
|
||||
$1 = C_c_string ($input);
|
||||
$2 = C_header_size ($input);
|
||||
}
|
||||
|
||||
extern int count(char *bytes, int len, char c);
|
||||
|
||||
|
||||
/* This example shows how to wrap a function that mutates a string */
|
||||
|
||||
%typemap(chicken,in) (char *str, int len)
|
||||
%{ if (!C_swig_is_string ($input)) {
|
||||
swig_barf (SWIG_BARF1_BAD_ARGUMENT_TYPE, "Argument $input is not a string");
|
||||
}
|
||||
$2 = C_header_size ($input);
|
||||
$1 = (char *) malloc ($2+1);
|
||||
memmove ($1, C_c_string ($input), $2);
|
||||
%}
|
||||
|
||||
/* Return the mutated string as a new object. Notice the if MANY construct ... they must be at column 0. */
|
||||
|
||||
%typemap(chicken,argout,fragment="list_output_helper",chicken_words="0") (char *str, int len) (C_word *scmstr)
|
||||
%{ scmstr = C_alloc (C_SIZEOF_STRING ($2));
|
||||
/*if MANY*/
|
||||
$result = list_output_helper (&known_space, $result, C_string (&scmstr, $2, $1));
|
||||
/*else*/
|
||||
$result = C_string (&scmstr, $2, $1);
|
||||
/*endif*/
|
||||
free ($1);
|
||||
%}
|
||||
|
||||
extern void capitalize (char *str, int len);
|
||||
|
||||
/* A multi-valued constraint. Force two arguments to lie
|
||||
inside the unit circle */
|
||||
|
||||
%typemap(check) (double cx, double cy) {
|
||||
double a = $1*$1 + $2*$2;
|
||||
if (a > 1.0) {
|
||||
SWIG_exception (SWIG_ValueError, "cx and cy must be in unit circle");
|
||||
}
|
||||
}
|
||||
|
||||
extern void circle (double cx, double cy);
|
||||
|
||||
/* Test out multiple return values */
|
||||
|
||||
extern int squareCubed (int n, int *OUTPUT);
|
||||
%{
|
||||
/* Returns n^3 and set n2 to n^2 */
|
||||
int squareCubed (int n, int *n2) {
|
||||
*n2 = n * n;
|
||||
return (*n2) * n;
|
||||
};
|
||||
%}
|
||||
24
Examples/chicken/multimap/precsi.scm
Normal file
24
Examples/chicken/multimap/precsi.scm
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
(declare (unit precsi))
|
||||
(declare (uses example))
|
||||
|
||||
;; display prelude to csi
|
||||
(display "multimap\n\n")
|
||||
|
||||
(display " A SWIG example for the CHICKEN compiler\n")
|
||||
(display " Author: Jonah Beckford, November 2002\n\n")
|
||||
|
||||
(display "C Procedures:\n")
|
||||
(display " int gcd (int n, int m);\n")
|
||||
(display " int gcdmain (int argc, char *argv[]);")
|
||||
(display " int count (char *bytes, int len, char c);")
|
||||
(display " void capitalize (char *str, int len);")
|
||||
(display " void circle (double x, double y);")
|
||||
(display "\n")
|
||||
|
||||
(display "Scheme Procedures:\n")
|
||||
(display " (example-gcd %n %m)\n")
|
||||
(display " (example-gcdmain '(%string %string ...))\n")
|
||||
(display " (example-count %string %character)\n")
|
||||
(display " (example-capitalize %string)\n")
|
||||
(display " (example-circle %x %y)\n")
|
||||
(display "\n")
|
||||
54
Examples/chicken/multimap/test-multimap.scm
Normal file
54
Examples/chicken/multimap/test-multimap.scm
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
;; run with './multimap test-multimap.scm'
|
||||
;; feel free to uncomment and comment sections
|
||||
|
||||
(display "(example-gcd 90 12): ")
|
||||
(display (example-gcd 90 12))
|
||||
(display "\n")
|
||||
|
||||
(display "(example-gcd 90 'a): ")
|
||||
;;(display (example-gcd 90 'a))
|
||||
(display "\n")
|
||||
|
||||
(display "(example-gcd 'b 12): ")
|
||||
;;(display (example-gcd 'b 12))
|
||||
(display "\n")
|
||||
|
||||
(display "(example-circle 0.5 0.5): ")
|
||||
(example-circle 0.5 0.5)
|
||||
(display "\n")
|
||||
|
||||
(display "(example-circle 1.0 1.0): ")
|
||||
;;(example-circle 1.0 1.0)
|
||||
(display "\n")
|
||||
|
||||
(display "(example-circle 1 1): ")
|
||||
;;(example-circle 1 1)
|
||||
(display "\n")
|
||||
|
||||
(display "(example-capitalize \"will this be all capital letters?\"): ")
|
||||
(display (example-capitalize "will this be all capital letters?"))
|
||||
(display "\n")
|
||||
|
||||
(display "(example-capitalize 'a): ")
|
||||
;;(display (example-capitalize 'a))
|
||||
(display "\n")
|
||||
|
||||
(display "(example-count \"jumpity little spider\" #\\t): ")
|
||||
(display (example-count "jumpity little spider" #\t))
|
||||
(display "\n")
|
||||
|
||||
(display "(example-gcdmain '#(\"hi\" \"there\")): ")
|
||||
(display (example-gcdmain '#("hi" "there")))
|
||||
(display "\n")
|
||||
|
||||
(display "(example-gcdmain '#(\"gcd\" \"9\" \"28\")): ")
|
||||
(example-gcdmain '#("gcd" "9" "28"))
|
||||
(display "\n")
|
||||
|
||||
(display "(example-gcdmain '#(\"gcd\" \"12\" \"90\")): ")
|
||||
(example-gcdmain '#("gcd" "12" "90"))
|
||||
(display "\n")
|
||||
|
||||
(display "(example-squarecubed 3: ")
|
||||
(display (example-squarecubed 3))
|
||||
(display "\n")
|
||||
Loading…
Add table
Add a link
Reference in a new issue