add array example
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2009-sploving@11478 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
8217f59d55
commit
a19aea6b4b
16 changed files with 456 additions and 240 deletions
17
Examples/scilab/funcptr/Makefile
Normal file
17
Examples/scilab/funcptr/Makefile
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
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)' scilab
|
||||
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile scilab_clean
|
||||
rm -f *.sce *.so lib*lib.c *_wrap.c
|
||||
|
||||
check: all
|
||||
$(MAKE) -f $(TOP)/Makefile scilab_run
|
||||
20
Examples/scilab/funcptr/example.c
Normal file
20
Examples/scilab/funcptr/example.c
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
/* File : example.c */
|
||||
|
||||
int do_op(int a, int b, int (*op)(int,int)) {
|
||||
return (*op)(a,b);
|
||||
}
|
||||
|
||||
int add(int a, int b) {
|
||||
return a+b;
|
||||
}
|
||||
|
||||
int sub(int a, int b) {
|
||||
return a-b;
|
||||
}
|
||||
|
||||
int mul(int a, int b) {
|
||||
return a*b;
|
||||
}
|
||||
|
||||
int (*funcvar)(int,int) = add;
|
||||
|
||||
9
Examples/scilab/funcptr/example.h
Normal file
9
Examples/scilab/funcptr/example.h
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
/* file: example.h */
|
||||
|
||||
extern int do_op(int,int, int (*op)(int,int));
|
||||
extern int add(int,int);
|
||||
extern int sub(int,int);
|
||||
extern int mul(int,int);
|
||||
|
||||
extern int (*funcvar)(int,int);
|
||||
|
||||
11
Examples/scilab/funcptr/example.i
Normal file
11
Examples/scilab/funcptr/example.i
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
%{
|
||||
#include "example.h"
|
||||
%}
|
||||
|
||||
/* Wrap a function taking a pointer to a function */
|
||||
extern int do_op(int a, int b, int (*op)(int, int));
|
||||
|
||||
extern int (*funcvar)(int,int);
|
||||
|
||||
20
Examples/scilab/funcptr/runme.sci
Normal file
20
Examples/scilab/funcptr/runme.sci
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
// builder the *.so
|
||||
exec builder.sce;
|
||||
|
||||
// loader the *.so
|
||||
exec loader.sce;
|
||||
|
||||
a = 37
|
||||
b = 42
|
||||
|
||||
// Now call our C function with a bunch of callbacks
|
||||
|
||||
printf("Trying some C callback functions\n");
|
||||
printf(" a = %i\n", a);
|
||||
printf(" b = %i\n", b);
|
||||
printf(" ADD(a,b) = %i\n", do_op(a,b,funcvar_get()));
|
||||
|
||||
exit
|
||||
|
||||
|
||||
|
||||
17
Examples/scilab/matrix/Makefile
Normal file
17
Examples/scilab/matrix/Makefile
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
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)' scilab
|
||||
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile scilab_clean
|
||||
rm -f *.sce *.so lib*lib.c *_wrap.c
|
||||
|
||||
check: all
|
||||
$(MAKE) -f $(TOP)/Makefile scilab_run
|
||||
61
Examples/scilab/matrix/example.c
Normal file
61
Examples/scilab/matrix/example.c
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
/* FILE : matrix.c : some simple 4x4 matrix operations */
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
double **new_matrix() {
|
||||
|
||||
int i;
|
||||
double **M;
|
||||
|
||||
M = (double **) malloc(4*sizeof(double *));
|
||||
M[0] = (double *) malloc(16*sizeof(double));
|
||||
|
||||
for (i = 0; i < 4; i++) {
|
||||
M[i] = M[0] + 4*i;
|
||||
}
|
||||
return M;
|
||||
}
|
||||
|
||||
void destroy_matrix(double **M) {
|
||||
|
||||
free(M[0]);
|
||||
free(M);
|
||||
|
||||
}
|
||||
|
||||
void print_matrix(double **M) {
|
||||
|
||||
int i,j;
|
||||
|
||||
for (i = 0; i < 4; i++) {
|
||||
for (j = 0; j < 4; j++) {
|
||||
printf("%10g ", M[i][j]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void mat_mult(double **m1, double **m2, double **m3) {
|
||||
|
||||
int i,j,k;
|
||||
double temp[4][4];
|
||||
|
||||
for (i = 0; i < 4; i++)
|
||||
for (j = 0; j < 4; j++) {
|
||||
temp[i][j] = 0;
|
||||
for (k = 0; k < 4; k++)
|
||||
temp[i][j] += m1[i][k]*m2[k][j];
|
||||
}
|
||||
|
||||
for (i = 0; i < 4; i++)
|
||||
for (j = 0; j < 4; j++)
|
||||
m3[i][j] = temp[i][j];
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
36
Examples/scilab/matrix/example.i
Normal file
36
Examples/scilab/matrix/example.i
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
%module example
|
||||
// FILE : matrix.i
|
||||
|
||||
%{
|
||||
|
||||
void set_m(double **M, int i, int j, double val) {
|
||||
M[i][j] = val;
|
||||
}
|
||||
|
||||
double get_m(double **M, int i, int j) {
|
||||
return M[i][j];
|
||||
}
|
||||
%}
|
||||
|
||||
%inline {
|
||||
/*** Matrix Operations ***/
|
||||
|
||||
extern double **new_matrix();
|
||||
/* Creates a new matrix and returns a pointer to it */
|
||||
|
||||
extern void destroy_matrix(double **M);
|
||||
/* Destroys the matrix M */
|
||||
|
||||
extern void print_matrix(double **M);
|
||||
/* Prints out the matrix M */
|
||||
|
||||
extern void set_m(double **M, int i, int j, double val);
|
||||
/* Sets M[i][j] = val*/
|
||||
|
||||
extern double get_m(double **M, int i, int j);
|
||||
/* Returns M[i][j] */
|
||||
|
||||
extern void mat_mult(double **a, double **b, double **c);
|
||||
/* Multiplies matrix a by b and places the result in c*/
|
||||
|
||||
}
|
||||
41
Examples/scilab/matrix/runme.sci
Normal file
41
Examples/scilab/matrix/runme.sci
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
// builder the *.so
|
||||
exec builder.sce
|
||||
|
||||
// loader the *.so
|
||||
exec loader.sce
|
||||
|
||||
// creat a new matrix
|
||||
x = new_matrix();
|
||||
for i = 0 : 3;
|
||||
for j = 0 : 3;
|
||||
set_m(x, i, j, i+j);
|
||||
end;
|
||||
end;
|
||||
|
||||
// print the matrix
|
||||
print_matrix(x);
|
||||
|
||||
// another matrix
|
||||
y = new_matrix();
|
||||
for i = 0 : 3;
|
||||
for j = 0 : 3;
|
||||
set_m(y, i, j, i-j);
|
||||
end;
|
||||
end;
|
||||
|
||||
// print the matrix
|
||||
print_matrix(y);
|
||||
|
||||
// mat_mult the two matrix, and the result is stored in a new matrix
|
||||
z = new_matrix();
|
||||
|
||||
mat_mult(x, y, z);
|
||||
|
||||
print_matrix(z);
|
||||
|
||||
//destroy the matrix
|
||||
destroy_matrix(x);
|
||||
destroy_matrix(y);
|
||||
destroy_matrix(z);
|
||||
|
||||
exit
|
||||
|
|
@ -10,10 +10,10 @@ extern int divide(int, int, int *);
|
|||
/* This example illustrates a couple of different techniques
|
||||
for manipulating C pointers */
|
||||
|
||||
/* First we'll use the pointer library
|
||||
/* First we'll use the pointer library */
|
||||
extern void add(int *x, int *y, int *result);
|
||||
%include cpointer.i
|
||||
%pointer_functions(int, intp);*/
|
||||
%pointer_functions(int, intp);
|
||||
|
||||
/* Next we'll use some typemaps */
|
||||
|
||||
|
|
|
|||
|
|
@ -1,9 +1,31 @@
|
|||
// builder the *.so
|
||||
exec builder.sce
|
||||
|
||||
//loader the *.so
|
||||
// loader the *.so
|
||||
exec loader.sce
|
||||
|
||||
// First create some objects using the pointer library.
|
||||
printf("Testing the pointer library\n");
|
||||
a = new_intp();
|
||||
b = new_intp();
|
||||
c = new_intp();
|
||||
intp_assign(a,37);
|
||||
intp_assign(b,42);
|
||||
|
||||
a,b,c
|
||||
|
||||
// Call the add() function with some pointers
|
||||
add(a,b,c);
|
||||
|
||||
// Now get the result
|
||||
r = intp_value(c);
|
||||
printf(" 37 + 42 = %i\n",r);
|
||||
|
||||
// Clean up the pointers
|
||||
delete_intp(a);
|
||||
delete_intp(b);
|
||||
delete_intp(c);
|
||||
|
||||
//Now try the typemap library
|
||||
//This should be much easier. Now how it is no longer
|
||||
//necessary to manufacture pointers.
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@
|
|||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "sciprint.h"
|
||||
#include "example.h"
|
||||
|
||||
int ivar = 0;
|
||||
short svar = 0;
|
||||
|
|
@ -22,15 +22,20 @@ unsigned char ucvar = 0;
|
|||
char cvar = 0;
|
||||
float fvar = 0;
|
||||
double dvar = 0;
|
||||
char *strvar=0;
|
||||
char *strvar = 0;
|
||||
const char cstrvar[] = "Goodbye";
|
||||
int *iptrvar = 0;
|
||||
char name[5] = "Dave";
|
||||
double *Foo1;
|
||||
double *Foo2;
|
||||
int *pivar;
|
||||
short *psvar;
|
||||
char **foo;
|
||||
char path[256] = "/home/beazley";
|
||||
|
||||
|
||||
/* Global variables involving a structure */
|
||||
Point *ptptr = 0;
|
||||
Point pt = { 10, 20 };
|
||||
|
||||
/* A variable that we will make read-only in the interface */
|
||||
int status = 1;
|
||||
|
||||
/* A debugging function to print out their values */
|
||||
|
||||
void print_vars() {
|
||||
|
|
@ -45,9 +50,46 @@ void print_vars() {
|
|||
printf("fvar = %g\n", fvar);
|
||||
printf("dvar = %g\n", dvar);
|
||||
printf("cvar = %c\n", cvar);
|
||||
printf("strvar = %s\n",strvar);
|
||||
printf("strvar = %s\n", strvar ? strvar : "(null)");
|
||||
printf("cstrvar = %s\n", cstrvar ? cstrvar : "(null)");
|
||||
printf("iptrvar = %p\n", iptrvar);
|
||||
printf("name = %c%c%c%c%c\n", name[0],name[1],name[2],name[3],name[4]);
|
||||
//printf("ptptr = %p %s\n", ptptr, Point_print( ptptr ) );
|
||||
printf("pt = (%d, %d)\n", pt.x, pt.y);
|
||||
printf("status = %d\n", status);
|
||||
}
|
||||
|
||||
/* A function to create an integer (to test iptrvar) */
|
||||
|
||||
int *new_int(int value) {
|
||||
int *ip = (int *) malloc(sizeof(int));
|
||||
*ip = value;
|
||||
return ip;
|
||||
}
|
||||
|
||||
int value_int(int *value) {
|
||||
return *value;
|
||||
}
|
||||
|
||||
/* A function to create a point */
|
||||
|
||||
Point *new_Point(int x, int y) {
|
||||
Point *p = (Point *) malloc(sizeof(Point));
|
||||
p->x = x;
|
||||
p->y = y;
|
||||
return p;
|
||||
}
|
||||
|
||||
char * Point_print(Point *p) {
|
||||
static char buffer[256];
|
||||
if (p) {
|
||||
sprintf(buffer,"(%d,%d)", p->x,p->y);
|
||||
} else {
|
||||
sprintf(buffer,"null");
|
||||
}
|
||||
return buffer;
|
||||
}
|
||||
|
||||
void pt_print() {
|
||||
printf("(%d, %d)\n", pt.x, pt.y);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,8 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
%{
|
||||
#include "example.h"
|
||||
%}
|
||||
|
||||
#pragma SWIG nowarn=SWIGWARN_TYPEMAP_SWIGTYPELEAK
|
||||
|
||||
|
|
@ -17,17 +20,33 @@
|
|||
extern float fvar;
|
||||
extern double dvar;
|
||||
extern char *strvar;
|
||||
// extern const char cstrvar[];
|
||||
extern int *iptrvar;
|
||||
extern char name[256];
|
||||
extern double *Foo1;
|
||||
extern double *Foo2;
|
||||
extern int *pivar;
|
||||
extern short *psvar;
|
||||
extern char **foo;
|
||||
|
||||
extern Point *ptptr;
|
||||
extern Point pt;
|
||||
|
||||
%}
|
||||
|
||||
|
||||
/* Some read-only variables */
|
||||
|
||||
%immutable;
|
||||
|
||||
%inline %{
|
||||
extern int status;
|
||||
extern char path[256];
|
||||
%}
|
||||
|
||||
%mutable;
|
||||
|
||||
/* Some helper functions to make it easier to test */
|
||||
%inline %{
|
||||
extern void print_vars();
|
||||
extern int *new_int(int value);
|
||||
extern Point *new_Point(int x, int y);
|
||||
extern char *Point_print(Point *p);
|
||||
extern void pt_print();
|
||||
%}
|
||||
|
||||
|
|
|
|||
|
|
@ -18,17 +18,10 @@ cvar_set ("S");
|
|||
fvar_set (3.14159);
|
||||
dvar_set (2.1828);
|
||||
strvar_set("Hello World");
|
||||
name_set ("Bill");
|
||||
Foo1_set([1,2,3;4,5,6]);
|
||||
Foo2_set([1+2*%i,2+3*%i;3+4*%i,7+8*%i]);
|
||||
pivar_set(int32([ 1 2 3 4 5;
|
||||
6 7 8 9 10;
|
||||
11 12 13 14 15]));
|
||||
psvar_set(int16([ 1 2 3 4 5;
|
||||
6 7 8 9 10;
|
||||
11 12 13 14 15]));
|
||||
foo_set(["sample", "strings", "manipulation"; "with","gateway","API"]);
|
||||
|
||||
iptrvar= new_int(37);
|
||||
ptptr = new_Point(37,42);
|
||||
name_set ("Bill");
|
||||
// Now print out the values of the variables
|
||||
|
||||
printf("Variables (values printed from Scilab)\n");
|
||||
|
|
@ -45,13 +38,10 @@ printf("fvar = %f\n", fvar_get());
|
|||
printf("dvar = %f\n", dvar_get());
|
||||
printf("cvar = %s\n", cvar_get());
|
||||
printf("strvar = %s\n", strvar_get());
|
||||
printf("name = %s\n", name_get());
|
||||
Foo1_get()
|
||||
Foo2_get()
|
||||
pivar_get()
|
||||
psvar_get()
|
||||
foo_get()
|
||||
|
||||
iptrvar
|
||||
printf("name = %s\n", name_get());
|
||||
printf("ptptr = %s\n", Point_print(ptptr));
|
||||
printf("\nVariables (values printed from C)\n");
|
||||
|
||||
print_vars()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue