1 /*
2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2015 - Scilab Enterprises - Antoine ELIAS
4 *
5  * Copyright (C) 2012 - 2016 - Scilab Enterprises
6  *
7  * This file is hereby licensed under the terms of the GNU GPL v2.0,
8  * pursuant to article 5.3.4 of the CeCILL v.2.1.
9  * This file was originally licensed under the terms of the CeCILL v2.1,
10  * and continues to be available under such terms.
11  * For more information, see the COPYING file which you should have received
12  * along with this program.
13 *
14 */
15 #include <ctype.h>
16 #include "api_scilab.h"
17 #include "Scierror.h"
18 #include "localization.h"
19 #include "sciprint.h"
20 #include "sci_malloc.h"
21 #include "os_string.h"
22 
23 const char fname[] = "struct_test";
24 
sci_struct_test(scilabEnv env,int nin,scilabVar * in,int nopt,scilabOpt opt,int nout,scilabVar * out)25 int sci_struct_test(scilabEnv env, int nin, scilabVar* in, int nopt, scilabOpt opt, int nout, scilabVar* out)
26 {
27     int i = 0;
28     //input
29     scilabVar in1 = NULL;
30     int size1 = 0;
31     wchar_t** fields = NULL;
32     scilabVar in2 = NULL;
33     int size2 = 0;
34     //output
35     scilabVar out1 = NULL;
36 
37     //goal is to take string vector and list from intput to
38     //create a struct with fields names from string and
39     //fields data from list.
40 
41     if (nin != 2)
42     {
43         Scierror(999, _("%s: Wrong number of input arguments: %d expected.\n"), fname, 2);
44         return STATUS_ERROR;
45     }
46 
47     if (nout != 1)
48     {
49         Scierror(999, _("%s: Wrong number of output arguments: %d expected.\n"), fname, 1);
50         return STATUS_ERROR;
51     }
52 
53     //in1
54     in1 = in[0];
55     if (scilab_isString(env, in1) == 0 || scilab_isVector(env, in1) == 0)
56     {
57         Scierror(999, _("%s: Wrong type for input argument #%d: A string vector expected.\n"), fname, 1);
58         return STATUS_ERROR;
59     }
60 
61     size1 = scilab_getSize(env, in1);
62     scilab_getStringArray(env, in1, &fields);
63 
64     //in2
65     in2 = in[1];
66     if (scilab_isList(env, in2) == 0)
67     {
68         Scierror(999, _("%s: Wrong type for input argument #%d: A list expected.\n"), fname, 2);
69         return STATUS_ERROR;
70     }
71 
72     size2 = scilab_getSize(env, in2);
73 
74     if (size1 != size2)
75     {
76         Scierror(999, _("%s: Arguments #%d and #%d: Same sizes expected.\n"), fname, 1, 2);
77         return STATUS_ERROR;
78     }
79 
80     out1 = scilab_createStruct(env);
81 
82     for (i = 0; i < size1; ++i)
83     {
84         scilab_addField(env, out1, fields[i]);
85         scilab_setStructMatrix2dData(env, out1, fields[i], 0, 0, scilab_getListItem(env, in2, i));
86     }
87 
88     out[0] = out1;
89     return STATUS_OK;
90 }
91