]>
Commit | Line | Data |
---|---|---|
c90f71dd RD |
1 | // |
2 | // array.i | |
3 | // Dave Beazley | |
4 | // November 30, 1996 | |
5 | // | |
6 | // This SWIG library file provides access to C arrays. | |
7 | ||
8 | %module carray | |
9 | ||
10 | %section "SWIG C Array Module",info,after,pre,nosort,skip=1,chop_left=3,chop_right=0,chop_top=0,chop_bottom=0 | |
11 | ||
12 | %text %{ | |
13 | %include array.i | |
14 | ||
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 : | |
17 | ||
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 | |
22 | ||
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. | |
26 | %} | |
27 | ||
28 | // Grab the SWIG exception library | |
29 | ||
30 | #ifndef AUTODOC | |
31 | %include exception.i | |
32 | #endif | |
33 | ||
34 | // A Typemap used to check input arguments. | |
35 | ||
36 | %typemap(check) int *, double *, float *, char **, short *, long * { | |
37 | if (!$target) { | |
38 | SWIG_exception(SWIG_ValueError,"Received a NULL Pointer"); | |
39 | } | |
40 | } | |
41 | ||
42 | %typemap(ret) int *, double *, float *, char **, short *, long * { | |
43 | if (!$source) { | |
44 | SWIG_exception(SWIG_MemoryError,"Out of memory."); | |
45 | } | |
46 | } | |
47 | ||
48 | // ----------------------------------------------------------------------- | |
49 | // Integer array support | |
50 | // ----------------------------------------------------------------------- | |
51 | ||
52 | %subsection "Integer Arrays" | |
53 | %text %{ | |
54 | The following functions provide access to integer arrays (mapped | |
55 | onto the C 'int' datatype. | |
56 | %} | |
57 | ||
58 | %{ | |
59 | #include <limits.h> | |
60 | ||
61 | /* Create a new integer array */ | |
62 | ||
63 | static int *int_array(int size) { | |
64 | #ifdef __cplusplus | |
65 | return new int[size]; | |
66 | #else | |
67 | return (int *) malloc(size*sizeof(int)); | |
68 | #endif | |
69 | } | |
70 | ||
71 | /* Destroy an integer array */ | |
72 | ||
73 | static void int_destroy(int *array) { | |
74 | if (array) { | |
75 | #ifdef __cplusplus | |
76 | delete array; | |
77 | #else | |
78 | free(array); | |
79 | #endif | |
80 | } | |
81 | } | |
82 | ||
83 | /* Return an element */ | |
84 | ||
85 | static int int_get(int *array, int index) { | |
86 | if (array) { | |
87 | return array[index]; | |
88 | } else { | |
89 | return INT_MIN; | |
90 | } | |
91 | } | |
92 | ||
93 | /* Set an element */ | |
94 | ||
95 | static int int_set(int *array, int index, int value) { | |
96 | if (array) { | |
97 | return (array[index] = value); | |
98 | } else { | |
99 | return INT_MIN; | |
100 | } | |
101 | } | |
102 | ||
103 | %} | |
104 | ||
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++. */ | |
108 | ||
109 | void int_destroy(int *array); | |
110 | /* Destroys the given array. */ | |
111 | ||
112 | int int_get(int *array, int index); | |
113 | /* Returns the value of array[index]. */ | |
114 | ||
115 | int int_set(int *array, int index, int value); | |
116 | /* Sets array[index] = value. Returns value. */ | |
117 | ||
118 | ||
119 | // ----------------------------------------------------------------------- | |
120 | // Floating point | |
121 | // ----------------------------------------------------------------------- | |
122 | ||
123 | %subsection "Floating Point Arrays" | |
124 | /* The following functions provide access to arrays of floats and doubles. */ | |
125 | ||
126 | ||
127 | %{ | |
128 | #include <float.h> | |
129 | ||
130 | /* Create a new float array */ | |
131 | ||
132 | static float *float_array(int size) { | |
133 | #ifdef __cplusplus | |
134 | return new float[size]; | |
135 | #else | |
136 | return (float *) malloc(size*sizeof(float)); | |
137 | #endif | |
138 | } | |
139 | ||
140 | /* Destroy an array */ | |
141 | ||
142 | static void float_destroy(float *array) { | |
143 | if (array) { | |
144 | #ifdef __cplusplus | |
145 | delete array; | |
146 | #else | |
147 | free(array); | |
148 | #endif | |
149 | } | |
150 | } | |
151 | ||
152 | /* Return an element */ | |
153 | ||
154 | static float float_get(float *array, int index) { | |
155 | if (array) { | |
156 | return array[index]; | |
157 | } else { | |
158 | return FLT_MIN; | |
159 | } | |
160 | } | |
161 | ||
162 | /* Set an element */ | |
163 | ||
164 | static float float_set(float *array, int index, float value) { | |
165 | if (array) { | |
166 | return (array[index] = value); | |
167 | } else { | |
168 | return FLT_MIN; | |
169 | } | |
170 | } | |
171 | ||
172 | /* Create a new double array */ | |
173 | ||
174 | static double *double_array(int size) { | |
175 | #ifdef __cplusplus | |
176 | return new double[size]; | |
177 | #else | |
178 | return (double *) malloc(size*sizeof(double)); | |
179 | #endif | |
180 | } | |
181 | ||
182 | /* Destroy an array */ | |
183 | ||
184 | static void double_destroy(double *array) { | |
185 | if (array) { | |
186 | #ifdef __cplusplus | |
187 | delete array; | |
188 | #else | |
189 | free(array); | |
190 | #endif | |
191 | } | |
192 | } | |
193 | ||
194 | /* Return an element */ | |
195 | ||
196 | static double double_get(double *array, int index) { | |
197 | if (array) { | |
198 | return array[index]; | |
199 | } else { | |
200 | return FLT_MIN; | |
201 | } | |
202 | } | |
203 | ||
204 | /* Set an element */ | |
205 | ||
206 | static double double_set(double *array, int index, double value) { | |
207 | if (array) { | |
208 | return (array[index] = value); | |
209 | } else { | |
210 | return FLT_MIN; | |
211 | } | |
212 | } | |
213 | ||
214 | %} | |
215 | ||
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++. */ | |
219 | ||
220 | void double_destroy(double *array); | |
221 | /* Destroys the given array. */ | |
222 | ||
223 | double double_get(double *array, int index); | |
224 | /* Returns the value of array[index]. */ | |
225 | ||
226 | double double_set(double *array, int index, double value); | |
227 | /* Sets array[index] = value. Returns value. */ | |
228 | ||
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++. */ | |
232 | ||
233 | void float_destroy(float *array); | |
234 | /* Destroys the given array. */ | |
235 | ||
236 | float float_get(float *array, int index); | |
237 | /* Returns the value of array[index]. */ | |
238 | ||
239 | float float_set(float *array, int index, float value); | |
240 | /* Sets array[index] = value. Returns value. */ | |
241 | ||
242 | // ----------------------------------------------------------------------- | |
243 | // Character strings | |
244 | // ----------------------------------------------------------------------- | |
245 | ||
246 | %subsection "String Arrays" | |
247 | ||
248 | %text %{ | |
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. | |
252 | %} | |
253 | ||
254 | #if defined(SWIGTCL) | |
255 | %text %{ | |
256 | To convert from a Tcl list into a 'char **', the following code can be used : | |
257 | ||
258 | # $list is a list | |
259 | set args [string_array expr {[llength $list] + 1}] | |
260 | set i 0 | |
261 | foreach a $list { | |
262 | string_set $args $i $a | |
263 | incr i 1 | |
264 | } | |
265 | string_set $i "" | |
266 | # $args is now a char ** type | |
267 | %} | |
268 | #elif defined(SWIGPERL) | |
269 | ||
270 | %text %{ | |
271 | To convert from a Perl list into a 'char **', code similar to the following | |
272 | can be used : | |
273 | ||
274 | # @list is a list | |
275 | my $l = scalar(@list); | |
276 | my $args = string_array($l+1); | |
277 | my $i = 0; | |
278 | foreach $arg (@list) { | |
279 | string_set($args,$i,$arg); | |
280 | $i++; | |
281 | } | |
282 | string_set($args,$i,""); | |
283 | ||
284 | (of course, there is always more than one way to do it) | |
285 | %} | |
286 | #elif defined(SWIGPYTHON) | |
287 | ||
288 | %text %{ | |
289 | To convert from a Python list to a 'char **', code similar to the following | |
290 | can be used : | |
291 | ||
292 | # 'list' is a list | |
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),"") | |
297 | %} | |
298 | ||
299 | #endif | |
300 | ||
301 | %{ | |
302 | /* Create character string arrays */ | |
303 | ||
304 | static char **string_array(int size) { | |
305 | char **a; | |
306 | int i; | |
307 | #ifdef __cplusplus | |
308 | a = new char *[size]; | |
309 | #else | |
310 | a = (char **) malloc(size*sizeof(char *)); | |
311 | #endif | |
312 | for (i = 0; i < size; i++) | |
313 | a[i] = 0; | |
314 | return a; | |
315 | } | |
316 | ||
317 | /* Destroy a string array */ | |
318 | ||
319 | static void string_destroy(char **array) { | |
320 | int i = 0; | |
321 | if (array) { | |
322 | while (array[i]) { | |
323 | #ifdef __cplusplus | |
324 | delete array[i]; | |
325 | #else | |
326 | free(array[i]); | |
327 | #endif | |
328 | i++; | |
329 | } | |
330 | #ifdef __cplusplus | |
331 | delete array; | |
332 | #else | |
333 | free(array); | |
334 | #endif | |
335 | } | |
336 | } | |
337 | ||
338 | /* Get an element */ | |
339 | ||
340 | static char *string_get(char **array_string, int index) { | |
341 | if (array_string) | |
342 | if (array_string[index]) return (array_string[index]); | |
343 | else return ""; | |
344 | else | |
345 | return ""; | |
346 | } | |
347 | ||
348 | /* Set an element */ | |
349 | ||
350 | static char *string_set(char **array_string, int index, char * val) { | |
351 | if (array_string) { | |
352 | if (array_string[index]) { | |
353 | #ifdef __cplusplus | |
354 | delete array_string[index]; | |
355 | #else | |
356 | free(array_string[index]); | |
357 | #endif | |
358 | } | |
359 | if (strlen(val) > 0) { | |
360 | #ifdef __cplusplus | |
361 | array_string[index] = new char[strlen(val)+1]; | |
362 | #else | |
363 | array_string[index] = (char *) malloc(strlen(val)+1); | |
364 | #endif | |
365 | strcpy(array_string[index],val); | |
366 | return array_string[index]; | |
367 | } else { | |
368 | array_string[index] = 0; | |
369 | return val; | |
370 | } | |
371 | } else return val; | |
372 | } | |
373 | ||
374 | %} | |
375 | ||
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. */ | |
380 | ||
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) */ | |
386 | ||
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. */ | |
390 | ||
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 | |
396 | destroyed. */ | |
397 | ||
398 | ||
399 | %typemap(check) int *, double *, float *, char **, short *, long * = PREVIOUS; | |
400 | %typemap(out) int *, double *, float *, char **, short *, long * = PREVIOUS; | |
401 |