diff options
Diffstat (limited to 'Examples/chicken/multimap')
| -rw-r--r-- | Examples/chicken/multimap/Makefile | 30 | ||||
| -rw-r--r-- | Examples/chicken/multimap/example.c | 53 | ||||
| -rw-r--r-- | Examples/chicken/multimap/example.i | 96 | ||||
| -rw-r--r-- | Examples/chicken/multimap/test-multimap.scm | 59 |
4 files changed, 238 insertions, 0 deletions
diff --git a/Examples/chicken/multimap/Makefile b/Examples/chicken/multimap/Makefile new file mode 100644 index 0000000..dace61a --- /dev/null +++ b/Examples/chicken/multimap/Makefile @@ -0,0 +1,30 @@ +TOP = ../.. +SWIG = $(TOP)/../preinst-swig +INTERFACE = example.i +SRCS = example.c +CXXSRCS = +TARGET = multimap +INCLUDE = +SWIGOPT = +CFLAGS = +VARIANT = + +# uncomment the following two lines to build a static exe +#CHICKEN_MAIN = test-multimap.scm +#VARIANT = _static + +all:: $(TARGET) + +$(TARGET): $(INTERFACE) $(SRCS) + $(MAKE) -f $(TOP)/Makefile \ + SRCS='$(SRCS)' CXXSRCS='$(CXXSRCS)' CHICKEN_MAIN='$(CHICKEN_MAIN)' \ + INCLUDE='$(INCLUDE)' SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' \ + SWIG='$(SWIG)' INTERFACE='$(INTERFACE)' CHICKENOPTS='$(CHICKENOPTS)' chicken$(VARIANT) + +clean:: + $(MAKE) -f $(TOP)/Makefile chicken_clean + rm -f example.scm + rm -f $(TARGET) + +check:: + env LD_LIBRARY_PATH=.:$$LD_LIBRARY_PATH csi test-multimap.scm diff --git a/Examples/chicken/multimap/example.c b/Examples/chicken/multimap/example.c new file mode 100644 index 0000000..b8360fa --- /dev/null +++ b/Examples/chicken/multimap/example.c @@ -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] = (char)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); + } +} diff --git a/Examples/chicken/multimap/example.i b/Examples/chicken/multimap/example.i new file mode 100644 index 0000000..02567f4 --- /dev/null +++ b/Examples/chicken/multimap/example.i @@ -0,0 +1,96 @@ +/* File : example.i */ +%module example + +%{ +extern int gcd(int x, int y); +extern int gcdmain(int argc, char *argv[]); +extern int count(char *bytes, int len, char c); +extern void capitalize (char *str, int len); +extern void circle (double cx, double cy); +extern int squareCubed (int n, int *OUTPUT); +%} + +%include exception.i +%include typemaps.i + +extern int gcd(int x, int y); + +%typemap(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(freearg) (int argc, char *argv[]) { + free($2); +} +extern int gcdmain(int argc, char *argv[]); + +%typemap(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(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(argout) (char *str, int len) (C_word *scmstr) +%{ scmstr = C_alloc (C_SIZEOF_STRING ($2)); + SWIG_APPEND_VALUE(C_string (&scmstr, $2, $1)); + 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; +}; +%} diff --git a/Examples/chicken/multimap/test-multimap.scm b/Examples/chicken/multimap/test-multimap.scm new file mode 100644 index 0000000..3a6b46e --- /dev/null +++ b/Examples/chicken/multimap/test-multimap.scm @@ -0,0 +1,59 @@ +;; run with './multimap test-multimap.scm' +;; feel free to uncomment and comment sections + +(load-library 'example "multimap.so") + +(display "(gcd 90 12): ") +(display (gcd 90 12)) +(display "\n") + +(display "(circle 0.5 0.5): ") +(display (circle 0.5 0.5)) +(display "\n") + +(display "(circle 1.0 1.0): ") +(handle-exceptions exvar + (if (= (car exvar) 9) + (display "success: exception thrown") + (display "an incorrect exception was thrown")) + (begin + (circle 1.0 1.0) + (display "an exception was not thrown when it should have been"))) +(display "\n") + +(display "(circle 1 1): ") +(handle-exceptions exvar + (if (= (car exvar) 9) + (display "success: exception thrown") + (display "an incorrect exception was thrown")) + (begin + (circle 1 1) + (display "an exception was not thrown when it should have been"))) +(display "\n") + +(display "(capitalize \"will this be all capital letters?\"): ") +(display (capitalize "will this be all capital letters?")) +(display "\n") + +(display "(count \"jumpity little spider\" #\\t): ") +(display (count "jumpity little spider" #\t)) +(display "\n") + +(display "(gcdmain '#(\"hi\" \"there\")): ") +(display (gcdmain '#("hi" "there"))) +(display "\n") + +(display "(gcdmain '#(\"gcd\" \"9\" \"28\")): ") +(gcdmain '#("gcd" "9" "28")) +(display "\n") + +(display "(gcdmain '#(\"gcd\" \"12\" \"90\")): ") +(gcdmain '#("gcd" "12" "90")) +(display "\n") + +(display "squarecubed 3: ") +(call-with-values (lambda() (squareCubed 3)) + (lambda (a b) (printf "~A ~A" a b))) +(display "\n") + +(exit) |
