summaryrefslogtreecommitdiff
path: root/Examples/guile/multimap
diff options
context:
space:
mode:
Diffstat (limited to 'Examples/guile/multimap')
-rw-r--r--Examples/guile/multimap/Makefile18
-rw-r--r--Examples/guile/multimap/example.c53
-rw-r--r--Examples/guile/multimap/example.i87
-rw-r--r--Examples/guile/multimap/runme.scm30
4 files changed, 188 insertions, 0 deletions
diff --git a/Examples/guile/multimap/Makefile b/Examples/guile/multimap/Makefile
new file mode 100644
index 0000000..dc9c66d
--- /dev/null
+++ b/Examples/guile/multimap/Makefile
@@ -0,0 +1,18 @@
+TOP = ../..
+SWIG = $(TOP)/../preinst-swig
+SRCS = example.c
+TARGET = example
+INTERFACE = example.i
+
+all::
+ $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
+ TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' guile
+
+static::
+ $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
+ TARGET='my-guile' INTERFACE='$(INTERFACE)' guile_static
+
+clean::
+ $(MAKE) -f $(TOP)/Makefile TARGET='$(TARGET)' guile_clean
+
+check: all
diff --git a/Examples/guile/multimap/example.c b/Examples/guile/multimap/example.c
new file mode 100644
index 0000000..b8360fa
--- /dev/null
+++ b/Examples/guile/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/guile/multimap/example.i b/Examples/guile/multimap/example.i
new file mode 100644
index 0000000..7337d1e
--- /dev/null
+++ b/Examples/guile/multimap/example.i
@@ -0,0 +1,87 @@
+/* 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;
+ SCM *v;
+ if (!(SCM_NIMP($input) && SCM_VECTORP($input))) {
+ SWIG_exception(SWIG_ValueError, "Expecting a vector");
+ return 0;
+ }
+ $1 = SCM_LENGTH($input);
+ if ($1 == 0) {
+ SWIG_exception(SWIG_ValueError, "Vector must contain at least 1 element");
+ }
+ $2 = (char **) malloc(($1+1)*sizeof(char *));
+ v = SCM_VELTS($input);
+ for (i = 0; i < $1; i++) {
+ if (!(SCM_NIMP(v[i]) && SCM_STRINGP(v[i]))) {
+ free($2);
+ SWIG_exception(SWIG_ValueError, "Vector items must be strings");
+ return 0;
+ }
+ $2[i] = SCM_CHARS(v[i]);
+ }
+ $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 (!(SCM_NIMP($input) && SCM_STRINGP($input))) {
+ SWIG_exception(SWIG_ValueError, "Expecting a string");
+ }
+ $1 = SCM_CHARS($input);
+ $2 = SCM_LENGTH($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) {
+ size_t temp;
+ $1 = gh_scm2newstr($input,&temp);
+ $2 = temp;
+}
+
+/* Return the mutated string as a new object. */
+
+%typemap(argout) (char *str, int len) {
+ SWIG_APPEND_VALUE(gh_str2scm($1,$2));
+ if ($1) scm_must_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,"$1_name and $2_name must be in unit circle");
+ }
+}
+
+extern void circle(double cx, double cy);
+
+
diff --git a/Examples/guile/multimap/runme.scm b/Examples/guile/multimap/runme.scm
new file mode 100644
index 0000000..edc1972
--- /dev/null
+++ b/Examples/guile/multimap/runme.scm
@@ -0,0 +1,30 @@
+;;; Test out some multi-argument typemaps
+
+(use-modules (example))
+
+; Call the GCD function
+
+(define x 42)
+(define y 105)
+(define g (gcd x y))
+
+(display "The gcd of ")
+(display x)
+(display " and ")
+(display y)
+(display " is ")
+(display g)
+(newline)
+
+; Call the gcdmain() function
+(gcdmain #("gcdmain" "42" "105"))
+
+; Call the count function
+(display (count "Hello World" #\l))
+(newline)
+
+; Call the capitalize function
+(display (capitalize "hello world"))
+(newline)
+
+