rewrite hypermat api for scilab 6
[scilab.git] / scilab / modules / api_scilab / tests / unit_tests / hypermatIntExample.c
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2013 - Scilab Enterprises
4  *
5  * This file must be used under the terms of the CeCILL.
6  * This source file is licensed as described in the file COPYING, which
7  * you should have received as part of this distribution. The terms
8  * are also available at
9  * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
10  *
11  */
12
13 #include <api_scilab.h>
14 #include "localization.h"
15
16 int hypermatIntExample(char *fname, void* pvApiCtx)
17 {
18     SciErr sciErr;
19     int* piAddr = NULL;
20     int iType   = 0;
21     int iRet    = 0;
22
23     CheckInputArgument(pvApiCtx, 1, 1);
24     CheckOutputArgument(pvApiCtx, 0, 1);
25
26     sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr);
27     if (sciErr.iErr)
28     {
29         printError(&sciErr, 0);
30         return 0;
31     }
32
33     if (isEmptyMatrix(pvApiCtx, piAddr))
34     {
35         iRet = createEmptyMatrix(pvApiCtx, nbInputArgument(pvApiCtx) + 1);
36         if (iRet)
37         {
38             return iRet;
39         }
40
41         AssignOutputVariable(pvApiCtx, 1) = 0;
42     }
43     else if (isHypermatType(pvApiCtx, piAddr))
44     {
45         int * dims = NULL;
46         int ndims;
47         void * data = NULL;
48         int htype = 0;
49         int precision;
50
51         sciErr = getHypermatType(pvApiCtx, piAddr, &htype);
52         if (sciErr.iErr)
53         {
54             printError(&sciErr, 0);
55             return sciErr.iErr;
56         }
57
58         if (htype == sci_ints)
59         {
60             sciErr = getHypermatOfIntegerPrecision(pvApiCtx, piAddr, &precision);
61             if (sciErr.iErr)
62             {
63                 printError(&sciErr, 0);
64                 return sciErr.iErr;
65             }
66
67             switch (precision)
68             {
69                 case SCI_INT8:
70                     sciErr = getHypermatOfInteger8(pvApiCtx, piAddr, &dims, &ndims, (char*)&data);
71                     if (sciErr.iErr)
72                     {
73                         printError(&sciErr, 0);
74                         return sciErr.iErr;
75                     }
76
77                     sciErr = createHypermatOfInteger8(pvApiCtx, nbInputArgument(pvApiCtx) + 1, dims, ndims, (const char*)data);
78                     if (sciErr.iErr)
79                     {
80                         printError(&sciErr, 0);
81                         return sciErr.iErr;
82                     }
83                     break;
84                 case SCI_UINT8:
85                     sciErr = getHypermatOfUnsignedInteger8(pvApiCtx, piAddr, &dims, &ndims, (unsigned char*)&data);
86                     if (sciErr.iErr)
87                     {
88                         printError(&sciErr, 0);
89                         return sciErr.iErr;
90                     }
91
92                     sciErr = createHypermatOfUnsignedInteger8(pvApiCtx, nbInputArgument(pvApiCtx) + 1, dims, ndims, (const unsigned char*)data);
93                     if (sciErr.iErr)
94                     {
95                         printError(&sciErr, 0);
96                         return sciErr.iErr;
97                     }
98                     break;
99                 case SCI_INT16:
100                     sciErr = getHypermatOfInteger16(pvApiCtx, piAddr, &dims, &ndims, (short*)&data);
101                     if (sciErr.iErr)
102                     {
103                         printError(&sciErr, 0);
104                         return sciErr.iErr;
105                     }
106
107                     sciErr = createHypermatOfInteger16(pvApiCtx, nbInputArgument(pvApiCtx) + 1, dims, ndims, (const short*)data);
108                     if (sciErr.iErr)
109                     {
110                         printError(&sciErr, 0);
111                         return sciErr.iErr;
112                     }
113                     break;
114                 case SCI_UINT16:
115                     sciErr = getHypermatOfUnsignedInteger16(pvApiCtx, piAddr, &dims, &ndims, (unsigned short*)&data);
116                     if (sciErr.iErr)
117                     {
118                         printError(&sciErr, 0);
119                         return sciErr.iErr;
120                     }
121
122                     sciErr = createHypermatOfUnsignedInteger16(pvApiCtx, nbInputArgument(pvApiCtx) + 1, dims, ndims, (const unsigned short*)data);
123                     if (sciErr.iErr)
124                     {
125                         printError(&sciErr, 0);
126                         return sciErr.iErr;
127                     }
128                     break;
129                 case SCI_INT32:
130                     sciErr = getHypermatOfInteger32(pvApiCtx, piAddr, &dims, &ndims, (int*)&data);
131                     if (sciErr.iErr)
132                     {
133                         printError(&sciErr, 0);
134                         return sciErr.iErr;
135                     }
136
137                     sciErr = createHypermatOfInteger32(pvApiCtx, nbInputArgument(pvApiCtx) + 1, dims, ndims, (const int*)data);
138                     if (sciErr.iErr)
139                     {
140                         printError(&sciErr, 0);
141                         return sciErr.iErr;
142                     }
143                     break;
144                 case SCI_UINT32:
145                     sciErr = getHypermatOfUnsignedInteger32(pvApiCtx, piAddr, &dims, &ndims, (unsigned int*)&data);
146                     if (sciErr.iErr)
147                     {
148                         printError(&sciErr, 0);
149                         return sciErr.iErr;
150                     }
151
152                     sciErr = createHypermatOfUnsignedInteger32(pvApiCtx, nbInputArgument(pvApiCtx) + 1, dims, ndims, (const unsigned int*)data);
153                     if (sciErr.iErr)
154                     {
155                         printError(&sciErr, 0);
156                         return sciErr.iErr;
157                     }
158                     break;
159                 case SCI_INT64:
160                     sciErr = getHypermatOfInteger64(pvApiCtx, piAddr, &dims, &ndims, (long long*)&data);
161                     if (sciErr.iErr)
162                     {
163                         printError(&sciErr, 0);
164                         return sciErr.iErr;
165                     }
166
167                     sciErr = createHypermatOfInteger64(pvApiCtx, nbInputArgument(pvApiCtx) + 1, dims, ndims, (const long long*)data);
168                     if (sciErr.iErr)
169                     {
170                         printError(&sciErr, 0);
171                         return sciErr.iErr;
172                     }
173                     break;
174                 case SCI_UINT64:
175                     sciErr = getHypermatOfUnsignedInteger64(pvApiCtx, piAddr, &dims, &ndims, (unsigned long long*)&data);
176                     if (sciErr.iErr)
177                     {
178                         printError(&sciErr, 0);
179                         return sciErr.iErr;
180                     }
181
182                     sciErr = createHypermatOfUnsignedInteger64(pvApiCtx, nbInputArgument(pvApiCtx) + 1, dims, ndims, (const unsigned long long*)data);
183                     if (sciErr.iErr)
184                     {
185                         printError(&sciErr, 0);
186                         return sciErr.iErr;
187                     }
188                     break;
189             }
190         }
191         else
192         {
193             Scierror(999, _("%s: Wrong type for input argument #%d: An integer expected.\n"), fname, 1);
194             return 1;
195         }
196
197         AssignOutputVariable(pvApiCtx, 1) = nbInputArgument(pvApiCtx) + 1;
198     }
199
200     return 0;
201 }