-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsciconv_read.c
executable file
·499 lines (411 loc) · 13 KB
/
sciconv_read.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/*
This file is part of Sciscipy.
Sciscipy is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Sciscipy is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Foobar. If not, see <http://www.gnu.org/licenses/>.
Copyright (c) 2009, Vincent Guffens.
*/
/*
Types defined in scilab:
1 : real or complex constant matrix.
2 : polynomial matrix.
4 : boolean matrix.
5 : sparse matrix.
6 : sparse boolean matrix.
8 : matrix of integers stored on 1 2 or 4 bytes
9 : matrix of graphic handles
10 : matrix of character strings.
11 : un-compiled function.
13 : compiled function.
14 : function library.
15 : list.
16 : typed list (tlist)
17 : mlist
128 : pointer
NOTE: the way numpy vectors and matrices are created leads
to a memory leak.
*/
#include "sciconv_read.h"
#include "deallocator.h"
#include "util.h"
struct sciconv_read_struct *sciconv_read_list = NULL ;
PyObject *
sciconv_read (int *addr, int var_type)
{
char er_msg[BUFSIZE] ;
struct sciconv_read_struct *conv = sciconv_read_list ;
while (conv)
{
if (conv->scitype == var_type)
{
return conv->conv_func(addr) ;
}
conv = conv->next ;
}
snprintf(er_msg, BUFSIZE, "Type %i not supported", var_type) ;
PyErr_SetString(PyExc_TypeError, er_msg) ;
return NULL ;
} ;
#if NUMPY == 1
static PyObject * create_numpyarray(double *cxtmp, int m, int n)
{
PyObject *array ;
npy_intp dim[2], mn ;
if (m == 1 || n == 1)
{
mn = m * n ;
array = PyArray_NewFromDescr(&PyArray_Type, \
PyArray_DescrFromType(PyArray_DOUBLE), \
1, \
&mn, \
NULL, \
(void *) cxtmp, \
NPY_OWNDATA | NPY_FARRAY, \
NULL
) ;
attach_deallocator(array, cxtmp) ;
return array ;
}
dim[0] = m ;
dim[1] = n ;
array = PyArray_NewFromDescr(&PyArray_Type, \
PyArray_DescrFromType(PyArray_DOUBLE), \
2, \
dim, \
NULL, \
(void *) cxtmp, \
NPY_OWNDATA | NPY_FARRAY, \
NULL
) ;
attach_deallocator(array, cxtmp) ;
return array ;
}
static PyObject * create_cnumpyarray(double *cxtmp, double *cxtmp_img, int m, int n)
{
PyObject * array ;
int i, j ;
npy_intp dim[2], mn ;
complex * cxtmp_transpose ;
cxtmp_transpose = (complex*) malloc(2 * m * n * sizeof(complex));
dim[0] = m ;
dim[1] = n ;
if (!cxtmp_transpose)
{
PyErr_SetString(PyExc_MemoryError, "out of memory") ;
return NULL ;
}
for (i = 0; i < m; ++i)
for (j = 0; j < n; ++j)
{
cxtmp_transpose[i * n + j][0] = cxtmp[j * m + i] ;
cxtmp_transpose[i * n + j][1] = cxtmp_img[j * m + i] ;
}
if (m == 1 || n == 1)
{
mn = m * n ;
array = PyArray_NewFromDescr(&PyArray_Type, \
PyArray_DescrFromType(PyArray_CDOUBLE), \
1, \
&mn, \
NULL, \
(void *) cxtmp_transpose, \
NPY_OWNDATA | NPY_CARRAY, \
NULL
) ;
}
else
array = PyArray_NewFromDescr(&PyArray_Type, \
PyArray_DescrFromType(PyArray_CDOUBLE), \
2, \
dim, \
NULL, \
(void *) cxtmp_transpose, \
NPY_OWNDATA | NPY_CARRAY, \
NULL
) ;
free(cxtmp) ;
attach_deallocator(array, cxtmp_transpose) ;
return array ;
}
#else
static PyObject * create_listmatrix(double *cxtmp, double *cxtmp_img, int m, int n, int is_complex)
{
int i, j ;
PyObject *new_list, *new_line ;
Py_complex new_complex ;
if (m == 1 || n == 1)
{
new_list = PyList_New(m * n) ;
for (i = 0 ; i < m * n ; i++)
{
if (cxtmp_img != NULL)
{
new_complex.real = cxtmp[i] ;
new_complex.imag = cxtmp_img[i] ;
PyList_SET_ITEM(new_list, i, Py_BuildValue("D", &new_complex)) ;
}
else
{
PyList_SET_ITEM(new_list, i, Py_BuildValue("d", cxtmp[i])) ;
}
}
}
else
{
new_list = PyList_New(m) ;
for (i = 0 ; i < m ; i++)
{
new_line = PyList_New(n) ;
for (j = 0 ; j < n ; j++)
if (cxtmp_img != NULL)
{
new_complex.real = cxtmp[j * m + i] ;
new_complex.imag = cxtmp_img[j * m + i] ;
PyList_SET_ITEM(new_line, j, Py_BuildValue("D", &new_complex)) ;
}
else
{
PyList_SET_ITEM(new_line, j, Py_BuildValue("d", cxtmp[j * m + i])) ;
}
PyList_SET_ITEM(new_list, i, new_line) ;
}
}
free(cxtmp) ;
return new_list ;
};
#endif
/**
* Type 1 : real or complex constant matrix.
* @param name: the name of the scilab variable we want to read
* @return: A list of list
*/
static PyObject * read_matrix(int *addr)
{
int m, n ;
SciErr sciErr ;
double *cxtmp = NULL ;
double *cx = NULL, *cx_img = NULL;
double *cxtmp_img = NULL ;
PyObject * matrix ;
if (!isVarComplex(pvApiCtx, addr))
{
sciErr = getMatrixOfDouble(pvApiCtx, addr, &m, &n, NULL) ;
}
else
{
sciErr = getComplexMatrixOfDouble(pvApiCtx, addr, &m, &n, NULL, NULL) ;
}
if (sciErr.iErr)
{
PyErr_SetString(PyExc_TypeError, getErrorMessage(sciErr)) ;
return 0;
}
cx = (double*)malloc((m * n) * sizeof(double));
if (!cx)
{
PyErr_SetString(PyExc_MemoryError, "out of memory") ;
return NULL ;
}
if (!isVarComplex(pvApiCtx, addr))
{
sciErr = getMatrixOfDouble(pvApiCtx, addr, &m, &n, &cxtmp) ;
if (sciErr.iErr)
{
free(cx);
PyErr_SetString(PyExc_TypeError, "Error in readmatrix") ;
return 0;
}
memcpy(cx, cxtmp, sizeof(double) * n * m) ;
#if NUMPY == 1
matrix = create_numpyarray(cx, m, n) ;
#else
matrix = create_listmatrix(cx, NULL, m, n) ;
#endif
}
else
{
cx_img = (double*)malloc((m * n) * sizeof(double));
if (!cx_img)
{
free(cx) ;
free(cxtmp) ;
PyErr_SetString(PyExc_MemoryError, "out of memory") ;
return NULL ;
}
sciErr = getComplexMatrixOfDouble(pvApiCtx, addr, &m, &n, &cxtmp, &cxtmp_img) ;
if (sciErr.iErr)
{
free(cx) ;
free(cx_img);
PyErr_SetString(PyExc_TypeError, "Error in readmatrix") ;
return 0;
}
memcpy(cx, cxtmp, sizeof(double) * n * m) ;
memcpy(cx_img, cxtmp_img, sizeof(double) * n * m) ;
#if NUMPY == 1
matrix = create_cnumpyarray(cx, cx_img, m, n) ;
#else
matrix = create_listmatrix(cx, cx_img, m, n) ;
#endif
free(cx_img);
}
return matrix ;
}
/**
* Type 10 : Matrix of string.
* @param name: the name of the scilab variable we want to read
* @return: A list of string
*/
static PyObject * read_string(int *addr)
{
int m = 0, n = 0 ;
int i = 0 ;
int x = 0, y = 0 ;
char ** variable_from_scilab = NULL ;
SciErr sciErr;
sciErr = getMatrixOfString(pvApiCtx, addr, &m, &n, NULL, NULL) ;
if (sciErr.iErr)
{
PyErr_SetString(PyExc_TypeError, getErrorMessage(sciErr)) ;
return 0;
}
int *piLen = (int*)malloc(sizeof(int) * m * n);
PyObject *new_list ;
sciErr = getMatrixOfString(pvApiCtx, addr, &m, &n, piLen, NULL) ;
if (sciErr.iErr)
{
PyErr_SetString(PyExc_TypeError, getErrorMessage(sciErr)) ;
return 0;
}
variable_from_scilab = (char **) malloc(sizeof(char*) * (m * n)) ;
for (i = 0; i < m * n; i++)
{
variable_from_scilab[i] = (char*) malloc(sizeof(char) * (piLen[i])) ;
}
i = 0;
new_list = PyList_New(m * n) ;
sciErr = getMatrixOfString(pvApiCtx, addr, &m, &n, piLen, variable_from_scilab) ;
if (sciErr.iErr)
{
PyErr_SetString(PyExc_TypeError, getErrorMessage(sciErr)) ;
return 0;
}
for (x = 0; x < m; x++)
{
for (y = 0; y < n; y++)
{
char *tmpStr = variable_from_scilab[x * m + y] ;
PyList_SET_ITEM(new_list, i, Py_BuildValue("s", tmpStr)) ;
free(tmpStr) ;
i++;
}
}
return new_list ;
}
/**
* Type 16 : tlist (typed list).
*
* A tlist x = tlist(['test','a','b'],12,'item')
* is transformed in python in
* x = { "__tlist_name" : "test",
* "a" : 12,
* "b" : "item",
* }
*
* @param tlist_address: the address of the scilab variable we want to read
* @return: A dictionary
*/
static PyObject * read_tlist(int *tlist_address)
{
SciErr sciErr ;
PyObject *new_dict = NULL ;
PyObject *key_list = NULL ;
int nb_item = 0, i;
sciErr = getListItemNumber(pvApiCtx, tlist_address, &nb_item) ;
if (sciErr.iErr)
{
goto handle_error ;
}
new_dict = PyDict_New() ;
for (i = 1 ; i <= nb_item; ++i)
{
PyObject *py_item ;
int *item_address = NULL ;
int sci_type = 0 ;
sciErr = getListItemAddress(pvApiCtx, tlist_address, i, &item_address) ;
if (sciErr.iErr)
{
goto handle_error ;
}
sciErr = getVarType(pvApiCtx, item_address, &sci_type) ;
if (sciErr.iErr)
{
goto handle_error ;
}
py_item = sciconv_read (item_address, sci_type) ;
if (i == 1)
{
if (sci_type != 10)
{
PyErr_SetString(PyExc_TypeError, "First tlist item must be string") ;
return 0 ;
}
key_list = py_item ;
PyDict_SetItem(new_dict, Py_BuildValue("s", TLIST_NAME), \
PyList_GetItem(key_list, i - 1)) ;
}
else
{
PyObject *next_item = NULL ;
next_item = PyList_GetItem(key_list, i - 1) ;
if (next_item == NULL)
{
PyErr_SetString(PyExc_TypeError, "Cannot read tlist (wrong number of key)") ;
return 0 ;
}
PyDict_SetItem(new_dict, PyList_GetItem(key_list, i - 1), py_item) ;
}
}
return new_dict;
handle_error:
PyErr_SetString(PyExc_TypeError, getErrorMessage(sciErr)) ;
return 0;
}
/**
* Add a new converter to the list
* @param new_type: A scilab type number
* @param func: The converter function
*/
static void sciconv_read_add(int new_type, PyObject * (*func)(char*))
{
struct sciconv_read_struct *new_conv = \
(struct sciconv_read_struct*) malloc(sizeof(struct sciconv_read_struct)) ;
new_conv->scitype = new_type ;
new_conv->conv_func = func ;
if (sciconv_read_list == NULL)
{
sciconv_read_list = new_conv ;
new_conv->next = NULL ;
return ;
}
new_conv->next = sciconv_read_list->next ;
sciconv_read_list->next = new_conv ;
}
/**
* Initialization
* Add all the known converters to the list
*/
void sciconv_read_init(void)
{
// Most used should come last
sciconv_read_add(16, read_tlist) ;
sciconv_read_add(10, read_string) ;
sciconv_read_add(1, read_matrix) ;
}