diff options
Diffstat (limited to 'Examples/php/variables')
-rw-r--r-- | Examples/php/variables/Makefile | 24 | ||||
-rw-r--r-- | Examples/php/variables/example.c | 95 | ||||
-rw-r--r-- | Examples/php/variables/example.h | 34 | ||||
-rw-r--r-- | Examples/php/variables/example.i | 44 | ||||
-rw-r--r-- | Examples/php/variables/runme.php | 96 | ||||
-rw-r--r-- | Examples/php/variables/runme.php4.old | 80 |
6 files changed, 373 insertions, 0 deletions
diff --git a/Examples/php/variables/Makefile b/Examples/php/variables/Makefile new file mode 100644 index 0000000..0862ce5 --- /dev/null +++ b/Examples/php/variables/Makefile @@ -0,0 +1,24 @@ +TOP = ../.. +SWIG = $(TOP)/../preinst-swig +SRCS = example.c +TARGET = example +INTERFACE = example.i +LIBS = +SWIGOPT = + +all:: + $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \ + SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \ + php + +static:: + $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \ + SWIGOPT='$(SWIGOPT)' TARGET='myphp' INTERFACE='$(INTERFACE)' \ + php_static + +clean:: + $(MAKE) -f $(TOP)/Makefile php_clean + rm -f $(TARGET).php + +check: all + $(MAKE) -f $(TOP)/Makefile php_run diff --git a/Examples/php/variables/example.c b/Examples/php/variables/example.c new file mode 100644 index 0000000..3114c7c --- /dev/null +++ b/Examples/php/variables/example.c @@ -0,0 +1,95 @@ +/* File : example.c */ + +/* I'm a file containing some C global variables */ + +/* Deal with Microsoft's attempt at deprecating C standard runtime functions */ +#if !defined(SWIG_NO_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER) +# define _CRT_SECURE_NO_DEPRECATE +#endif + +#include <stdio.h> +#include <stdlib.h> +#include "example.h" + +int ivar = 0; +short svar = 0; +long lvar = 0; +unsigned int uivar = 0; +unsigned short usvar = 0; +unsigned long ulvar = 0; +signed char scvar = 0; +unsigned char ucvar = 0; +char cvar = 0; +float fvar = 0; +double dvar = 0; +char *strvar = 0; +const char cstrvar[] = "Goodbye"; +int *iptrvar = 0; +char name[5] = "Dave"; +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() { + printf("ivar = %d\n", ivar); + printf("svar = %d\n", svar); + printf("lvar = %ld\n", lvar); + printf("uivar = %u\n", uivar); + printf("usvar = %u\n", usvar); + printf("ulvar = %lu\n", ulvar); + printf("scvar = %d\n", scvar); + printf("ucvar = %u\n", ucvar); + printf("fvar = %g\n", fvar); + printf("dvar = %g\n", dvar); + printf("cvar = %c\n", cvar); + 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); +} diff --git a/Examples/php/variables/example.h b/Examples/php/variables/example.h new file mode 100644 index 0000000..3b3659c --- /dev/null +++ b/Examples/php/variables/example.h @@ -0,0 +1,34 @@ +/* File: example.h */ + +typedef struct { + int x,y; +} Point; + +/* Some global variable declarations */ +extern int ivar; +extern short svar; +extern long lvar; +extern unsigned int uivar; +extern unsigned short usvar; +extern unsigned long ulvar; +extern signed char scvar; +extern unsigned char ucvar; +extern char cvar; +extern float fvar; +extern double dvar; +extern char *strvar; +extern const char cstrvar[]; +extern int *iptrvar; +extern char name[5]; + +extern Point *ptptr; +extern Point pt; + +extern int status; +extern char path[256]; + +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(); diff --git a/Examples/php/variables/example.i b/Examples/php/variables/example.i new file mode 100644 index 0000000..3edbb72 --- /dev/null +++ b/Examples/php/variables/example.i @@ -0,0 +1,44 @@ +/* File : example.i */ +%module example +%{ +#include "example.h" +%} + +/* Some global variable declarations */ +extern int ivar; +extern short svar; +extern long lvar; +extern unsigned int uivar; +extern unsigned short usvar; +extern unsigned long ulvar; +extern signed char scvar; +extern unsigned char ucvar; +extern char cvar; +extern float fvar; +extern double dvar; +extern char *strvar; +extern const char cstrvar[]; +extern int *iptrvar; +extern char name[5]; + +extern Point *ptptr; +extern Point pt; + +/* Some read-only variables */ + +%immutable; +extern int status; +extern char path[256]; +%mutable; + +/* Some helper functions to make it easier to test */ +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(); + + + + diff --git a/Examples/php/variables/runme.php b/Examples/php/variables/runme.php new file mode 100644 index 0000000..bbfeb61 --- /dev/null +++ b/Examples/php/variables/runme.php @@ -0,0 +1,96 @@ +<?php + + require "example.php"; + echo "\nVariables (values printed from C)\n"; + + print_vars(); + + echo "Variables (values printed from PHP)\n"; + + echo "ivar = ".ivar_get()."\n"; + echo "svar = ".svar_get()."\n"; + echo "lvar = ".lvar_get()."\n"; + echo "uivar = ".uivar_get()."\n"; + echo "usvar = ".usvar_get()."\n"; + echo "ulvar = ".ulvar_get()."\n"; + echo "scvar = ".scvar_get()."\n"; + echo "ucvar = ".ucvar_get()."\n"; + echo "cvar = ".cvar_get()."\n"; + echo "fvar = ".fvar_get()."\n"; + echo "dvar = ".dvar_get()."\n"; + echo "strvar = ".strvar_get()."\n"; + echo "cstrvar = ".cstrvar_get()."\n"; + echo "iptrvar = ".iptrvar_get()."\n"; + echo "name = \"".name_get()."\"\n"; + echo "ptptr = ".ptptr_get() , point_print(ptptr_get()) , "\n"; + echo "pt = ".pt_get(), point_print(pt_get()) , "\n"; + + /* Try to set the values of some global variables */ +$a = "42.14"; + + ivar_set($a); +echo "a = $a\n"; + svar_set(-31000); + lvar_set(65537); + uivar_set(123456); + usvar_set(61000); + ulvar_set(654321); + scvar_set(-13); + ucvar_set(251); + cvar_set("S"); + fvar_set(3.14159); + dvar_set(2.1828); + strvar_set("Hello World"); + iptrvar_set(new_int(37)); + ptptr_set(new_point(37,42)); + name_set("B"); + + echo "Variables (values printed from PHP)\n"; + + echo "ivar = ".ivar_get()."\n"; + echo "svar = ".svar_get()."\n"; + echo "lvar = ".lvar_get()."\n"; + echo "uivar = ".uivar_get()."\n"; + echo "usvar = ".usvar_get()."\n"; + echo "ulvar = ".ulvar_get()."\n"; + echo "scvar = ".scvar_get()."\n"; + echo "ucvar = ".ucvar_get()."\n"; + echo "cvar = ".cvar_get()."\n"; + echo "fvar = ".fvar_get()."\n"; + echo "dvar = ".dvar_get()."\n"; + echo "strvar = ".strvar_get()."\n"; + echo "cstrvar = ".cstrvar_get()."\n"; + echo "iptrvar = ".iptrvar_get()."\n"; + echo "name = ".name_get()."\n"; + echo "ptptr = ".ptptr_get() , point_print(ptptr_get()) , "\n"; + echo "pt = ".pt_get(), point_print(pt_get()) , "\n"; + + echo "\nVariables (values printed from C)\n"; + + print_vars(); + + echo "\nI'm going to try and update a structure variable.\n"; + + pt_set(ptptr_get()); + + echo "The new value is \n"; + + pt_print(); + + echo "You should see the value", point_print(ptptr_get()), "\n"; + + echo "\nNow I'm going to try and modify some read only variables\n"; + + echo "Trying to set 'path'\n"; + + //path_set("Whoa!"); + echo "Path = ".path_get()."\n"; + + echo "Trying to set 'status'\n"; + + /* And this */ + //status_set(0); + echo "Status = ".status_get()."\n"; + +?> + diff --git a/Examples/php/variables/runme.php4.old b/Examples/php/variables/runme.php4.old new file mode 100644 index 0000000..9a6bfb3 --- /dev/null +++ b/Examples/php/variables/runme.php4.old @@ -0,0 +1,80 @@ +<?php +### THIS VERSION was written for when php global vars fakingly mirrored +### the wrapped global vars, but it was very inefficient. +### For now we don't do this (pending some changes to php itself) so +### we use accessor functions instead; WE KEEP THIS version around ready +### for when those php changes are made and we can switch back. +### Specifically we want $_GLOBALS variable overloading like object +### property overloading + + require "example.php"; + + /* Try to set the values of some global variables */ + + $ivar = 42; + $svar = -31000; + $lvar = 65537; + $uivar = 123456; + $usvar = 61000; + $ulvar = 654321; + $scvar = -13; + $ucvar = 251; + $cvar = "S"; + $fvar = 3.14159; + $dvar = 2.1828; + $strvar = "Hello World"; + $cstrvar = "Goodbye"; + $iptrvar = new_int(37); + $ptptr = new_point(37,42); + $name = "Bill"; + + echo "Variables (values printed from PHP)\n"; + + echo "ivar = $ivar\n"; + echo "svar = $svar\n"; + echo "lvar = $lvar\n"; + echo "uivar = $uivar\n"; + echo "usvar = $usvar\n"; + echo "ulvar = $ulvar\n"; + echo "scvar = $scvar\n"; + echo "ucvar = $ucvar\n"; + echo "cvar = $cvar\n"; + echo "fvar = $fvar\n"; + echo "dvar = $dvar\n"; + echo "strvar = $strvar\n"; + echo "cstrvar = $cstrvar\n"; + echo "iptrvar = $iptrvar\n"; + echo "name = $name\n"; + echo "ptptr = $ptptr" , point_print($ptptr) , "\n"; + echo "pt = $pt" , point_print($pt) , "\n"; + + echo "\nVariables (values printed from C)\n"; + + print_vars(); + + echo "\nI'm going to try and update a structure variable.\n"; + + $pt = $ptptr; + + echo "The new value is \n"; + + pt_print(); + + echo "You should see the value", point_print($ptptr), "\n"; + + echo "\nNow I'm going to try and modify some read only variables\n"; + + echo "Trying to set 'path'\n"; + + /* Sadly this works */ + $path = "Whoa!"; + echo "Path = $path\n"; + + echo "Trying to set 'status'\n"; + + /* And this */ + $status = 0; + echo "Status = $status\n"; + +?> + |