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