6 // This SWIG library file provides access to C arrays.
 
  10 %section "SWIG C Array Module",info,after,pre,nosort,skip=1,chop_left=3,chop_right=0,chop_top=0,chop_bottom=0
 
  15 This module provides scripting language access to various kinds of C/C++
 
  16 arrays. For each datatype, a collection of four functions are created :
 
  18    <type>_array(size)              : Create a new array of given size
 
  19    <type>_get(array, index)        : Get an element from the array
 
  20    <type>_set(array, index, value) : Set an element in the array
 
  21    <type>_destroy(array)           : Destroy an array
 
  23 The functions in this library are only low-level accessor functions
 
  24 designed to directly access C arrays.  Like C, no bounds checking is
 
  25 performed so use at your own peril.
 
  28 // Grab the SWIG exception library
 
  34 // A Typemap used to check input arguments.
 
  36 %typemap(check) int *, double *, float *, char **, short *, long * {
 
  38     SWIG_exception(SWIG_ValueError,"Received a NULL Pointer");
 
  42 %typemap(ret) int *, double *, float *, char **, short *, long * {
 
  44     SWIG_exception(SWIG_MemoryError,"Out of memory.");
 
  48 // -----------------------------------------------------------------------
 
  49 // Integer array support
 
  50 // -----------------------------------------------------------------------
 
  52 %subsection "Integer Arrays"
 
  54 The following functions provide access to integer arrays (mapped
 
  55 onto the C 'int' datatype. 
 
  61 /* Create a new integer array */
 
  63   static int *int_array(int size) {
 
  67     return (int *) malloc(size*sizeof(int));
 
  71   /* Destroy an integer array */
 
  73   static void int_destroy(int *array) {
 
  83   /* Return an element */
 
  85   static int int_get(int *array, int index) {
 
  95   static int int_set(int *array, int index, int value) {
 
  97       return (array[index] = value);
 
 105 int *int_array(int nitems);
 
 106 /* Creates a new array of integers. nitems specifies the number of elements.
 
 107    The array is created using malloc() in C and new() in C++. */
 
 109 void int_destroy(int *array);
 
 110 /* Destroys the given array. */
 
 112 int int_get(int *array, int index);
 
 113 /* Returns the value of array[index]. */
 
 115 int int_set(int *array, int index, int value);
 
 116 /* Sets array[index] = value.  Returns value. */
 
 119 // -----------------------------------------------------------------------
 
 121 // -----------------------------------------------------------------------
 
 123 %subsection "Floating Point Arrays"
 
 124 /* The following functions provide access to arrays of floats and doubles. */
 
 130   /* Create a new float array */
 
 132   static float *float_array(int size) {
 
 134     return new float[size];
 
 136     return (float *) malloc(size*sizeof(float));
 
 140   /* Destroy an array */
 
 142   static void float_destroy(float *array) {
 
 152   /* Return an element */
 
 154   static float float_get(float *array, int index) {
 
 164   static float float_set(float *array, int index, float value) {
 
 166       return (array[index] = value);
 
 172   /* Create a new double array */
 
 174   static double *double_array(int size) {
 
 176     return new double[size];
 
 178     return (double *) malloc(size*sizeof(double));
 
 182   /* Destroy an array */
 
 184   static void double_destroy(double *array) {
 
 194   /* Return an element */
 
 196   static double double_get(double *array, int index) {
 
 206   static double double_set(double *array, int index, double value) {
 
 208       return (array[index] = value);
 
 216 double *double_array(int nitems);
 
 217 /* Creates a new array of doubles. nitems specifies the number of elements.
 
 218    The array is created using malloc() in C and new() in C++. */
 
 220 void double_destroy(double *array);
 
 221 /* Destroys the given array. */
 
 223 double double_get(double *array, int index);
 
 224 /* Returns the value of array[index]. */
 
 226 double double_set(double *array, int index, double value);
 
 227 /* Sets array[index] = value.  Returns value. */
 
 229 float *float_array(int nitems);
 
 230 /* Creates a new array of floats. nitems specifies the number of elements.
 
 231    The array is created using malloc() in C and new() in C++. */
 
 233 void float_destroy(float *array);
 
 234 /* Destroys the given array. */
 
 236 float float_get(float *array, int index);
 
 237 /* Returns the value of array[index]. */
 
 239 float float_set(float *array, int index, float value);
 
 240 /* Sets array[index] = value.  Returns value. */
 
 242 // -----------------------------------------------------------------------
 
 244 // -----------------------------------------------------------------------
 
 246 %subsection "String Arrays"
 
 249 The following functions provide support for the 'char **' datatype.   This
 
 250 is primarily used to handle argument lists and other similar structures that
 
 251 need to be passed to a C/C++ function.
 
 256 To convert from a Tcl list into a 'char **', the following code can be used :
 
 259      set args [string_array expr {[llength $list] + 1}]
 
 262         string_set $args $i $a
 
 266      # $args is now a char ** type
 
 268 #elif defined(SWIGPERL)
 
 271 To convert from a Perl list into a 'char **', code similar to the following
 
 275     my $l = scalar(@list);
 
 276     my $args = string_array($l+1);
 
 278     foreach $arg (@list) {
 
 279         string_set($args,$i,$arg);
 
 282     string_set($args,$i,"");
 
 284 (of course, there is always more than one way to do it)
 
 286 #elif defined(SWIGPYTHON)
 
 289 To convert from a Python list to a 'char **', code similar to the following
 
 293     args = string_array(len(list)+1)
 
 294     for i in range(0,len(list)):
 
 295         string_set(args,i,list[i])
 
 296     string_set(args,len(list),"")
 
 302 /* Create character string arrays */
 
 304 static char **string_array(int size) {
 
 308   a = new char *[size];
 
 310   a = (char **) malloc(size*sizeof(char *));
 
 312   for (i = 0; i < size; i++)
 
 317 /* Destroy a string array */
 
 319 static void string_destroy(char **array) {
 
 340 static char *string_get(char **array_string, int index) {
 
 342     if (array_string[index]) return (array_string[index]);
 
 350 static char *string_set(char **array_string, int index, char * val) {
 
 352     if (array_string[index]) {
 
 354         delete array_string[index];
 
 356         free(array_string[index]);
 
 359     if (strlen(val) > 0) {
 
 361       array_string[index] = new char[strlen(val)+1];
 
 363       array_string[index] = (char *) malloc(strlen(val)+1);
 
 365       strcpy(array_string[index],val);
 
 366       return array_string[index];
 
 368       array_string[index] = 0;
 
 376 char **string_array(int nitems);
 
 377 /* Creates a new array of strings. nitems specifies the number of elements.
 
 378    The array is created using malloc() in C and new() in C++. Each element
 
 379    of the array is set to NULL upon initialization. */
 
 381 void string_destroy(char **array);
 
 382 /* Destroys the given array. Each element of the array is assumed to be
 
 383    a NULL-terminated string allocated with malloc() or new().  All of
 
 384    these strings will be destroyed as well. (It is probably only safe to
 
 385    use this function on an array created by string_array) */
 
 387 char *string_get(char **array, int index);
 
 388 /* Returns the value of array[index]. Returns a string of zero length
 
 389    if the corresponding element is NULL. */
 
 391 char *string_set(char **array, int index, char *value);
 
 392 /* Sets array[index] = value.  value is assumed to be a NULL-terminated
 
 393    string.  A string of zero length is mapped into a NULL value.  When
 
 394    setting the value, the value will be copied into a new string allocated
 
 395    with malloc() or new().  Any previous value in the array will be
 
 399 %typemap(check) int *, double *, float *, char **, short *, long * = PREVIOUS;
 
 400 %typemap(out) int *, double *, float *, char **, short *, long * = PREVIOUS;