1/*
2 * C-type: short
3 * Scilab type: double or int16
4 */
5
6%fragment(SWIG_AsVal_frag(short), "header", fragment="SWIG_SciDoubleOrInt16_AsShort", fragment="<limits.h>") {
7#define SWIG_AsVal_short(scilabValue, valuePointer) SWIG_SciDoubleOrInt16_AsShort(pvApiCtx, scilabValue, valuePointer, SWIG_Scilab_GetFuncName())
8}
9%fragment("SWIG_SciDoubleOrInt16_AsShort", "header") {
10SWIGINTERN int
11SWIG_SciDoubleOrInt16_AsShort(void *pvApiCtx, int iVar, short *psValue, char *fname) {
12  SciErr sciErr;
13  int iType = 0;
14  int iRows = 0;
15  int iCols = 0;
16  int *piAddrVar = NULL;
17
18  sciErr = getVarAddressFromPosition(pvApiCtx, iVar, &piAddrVar);
19  if (sciErr.iErr) {
20    printError(&sciErr, 0);
21    return SWIG_ERROR;
22  }
23
24  sciErr = getVarType(pvApiCtx, piAddrVar, &iType);
25  if (sciErr.iErr) {
26    printError(&sciErr, 0);
27    return SWIG_ERROR;
28  }
29
30  if (iType == sci_ints) {
31    int iPrec = 0;
32    short *psData = NULL;
33
34    sciErr = getMatrixOfIntegerPrecision(pvApiCtx, piAddrVar, &iPrec);
35    if (sciErr.iErr) {
36      printError(&sciErr, 0);
37      return SWIG_ERROR;
38    }
39    if (iPrec != SCI_INT16) {
40      Scierror(SCILAB_API_ARGUMENT_ERROR, _("%s: Wrong type for input argument #%d: A 16-bit signed integer or a double expected.\n"), fname, iVar);
41      return SWIG_TypeError;
42    }
43    sciErr = getMatrixOfInteger16(pvApiCtx, piAddrVar, &iRows, &iCols, &psData);
44    if (sciErr.iErr) {
45      printError(&sciErr, 0);
46      return SWIG_ERROR;
47    }
48    if (iRows * iCols != 1) {
49      Scierror(SCILAB_API_ARGUMENT_ERROR, _("%s: Wrong size for input argument #%d: A 16-bit signed integer or a double expected.\n"), fname, iVar);
50      return SWIG_TypeError;
51    }
52    *psValue = *psData;
53  }
54  else if (iType == sci_matrix) {
55    double *pdData = NULL;
56    double dValue = 0.0f;
57
58    sciErr = getMatrixOfDouble(pvApiCtx, piAddrVar, &iRows, &iCols, &pdData);
59    if (sciErr.iErr) {
60      printError(&sciErr, 0);
61      return SWIG_ERROR;
62    }
63    if (iRows * iCols != 1) {
64      Scierror(SCILAB_API_ARGUMENT_ERROR, _("%s: Wrong size for input argument #%d: A 16-bit signed integer or a double expected.\n"), fname, iVar);
65      return SWIG_TypeError;
66    }
67    dValue = *pdData;
68    if (dValue != floor(dValue)) {
69      Scierror(SCILAB_API_ARGUMENT_ERROR, _("%s: Incorrect value for input argument #%d: The double value cannot be converted to a 16-bit signed integer.\n"), fname, iVar);
70      return SWIG_ValueError;
71    }
72    if ((dValue < SHRT_MIN) || (dValue > SHRT_MAX)) {
73      Scierror(SCILAB_API_ARGUMENT_ERROR, _("%s: Overflow error for input argument #%d: The double value cannot be converted to a 16-bit signed integer.\n"), fname, iVar);
74      return SWIG_OverflowError;
75    }
76    *psValue = (short) dValue;
77  }
78  else {
79    Scierror(SCILAB_API_ARGUMENT_ERROR, _("%s: Wrong type for input argument #%d: A 16-bit signed integer or a double expected.\n"), fname, iVar);
80    return SWIG_TypeError;
81  }
82
83  return SWIG_OK;
84}
85}
86
87%fragment(SWIG_From_frag(short), "header", fragment="SWIG_SciDouble_FromShort") {
88#define SWIG_From_short(scilabValue) SWIG_SciDouble_FromShort(pvApiCtx, SWIG_Scilab_GetOutputPosition(), scilabValue, SWIG_Scilab_GetFuncName())
89}
90%fragment("SWIG_SciDouble_FromShort", "header") {
91SWIGINTERN int
92SWIG_SciDouble_FromShort(void *pvApiCtx, int iVarOut, short sValue, char *fname) {
93  if (createScalarDouble(pvApiCtx,
94    SWIG_NbInputArgument(pvApiCtx) + iVarOut, (double) sValue))
95    return SWIG_ERROR;
96  return SWIG_OK;
97}
98}
99
100/*
101 * C-type: short[]
102 * Scilab type: double or int16 matrix
103 */
104%fragment("SWIG_SciDoubleOrInt16_AsShortArrayAndSize", "header") {
105SWIGINTERN int
106SWIG_SciDoubleOrInt16_AsShortArrayAndSize(void *pvApiCtx, int iVar, int *iRows, int *iCols, short **psValue, char *fname) {
107  SciErr sciErr;
108  int iType = 0;
109  int iPrec = 0;
110  int *piAddrVar = NULL;
111
112  sciErr = getVarAddressFromPosition(pvApiCtx, iVar, &piAddrVar);
113  if (sciErr.iErr) {
114    printError(&sciErr, 0);
115    return SWIG_ERROR;
116  }
117
118  sciErr = getVarType(pvApiCtx, piAddrVar, &iType);
119  if (sciErr.iErr) {
120    printError(&sciErr, 0);
121    return SWIG_ERROR;
122  }
123
124  if (iType == sci_matrix) {
125    double *pdData = NULL;
126    int size = 0;
127    int i;
128
129    sciErr = getMatrixOfDouble(pvApiCtx, piAddrVar, iRows, iCols, &pdData);
130    if (sciErr.iErr) {
131      printError(&sciErr, 0);
132      return SWIG_ERROR;
133    }
134
135    size = (*iRows) * (*iCols);
136    *psValue = (short*) malloc(size * sizeof(int*));
137    for (i = 0; i < size; i++)
138      (*psValue)[i] = (short) pdData[i];
139  }
140  else if (iType == sci_ints) {
141    int iPrec = 0;
142    sciErr = getMatrixOfIntegerPrecision(pvApiCtx, piAddrVar, &iPrec);
143    if (sciErr.iErr) {
144      printError(&sciErr, 0);
145      return SWIG_ERROR;
146    }
147    if (iPrec != SCI_INT16) {
148      Scierror(SCILAB_API_ARGUMENT_ERROR, _("%s: Wrong type for input argument #%d: A 16-bit signed integer or a double matrix expected.\n"), fname, iVar);
149      return SWIG_ERROR;
150    }
151
152    sciErr = getMatrixOfInteger16(pvApiCtx, piAddrVar, iRows, iCols, psValue);
153    if (sciErr.iErr) {
154      printError(&sciErr, 0);
155      return SWIG_ERROR;
156    }
157  }
158  else {
159    Scierror(SCILAB_API_ARGUMENT_ERROR, _("%s: Wrong type for input argument #%d: A 16-bit signed integer or a double matrix expected.\n"), fname, iVar);
160    return SWIG_ERROR;
161  }
162
163  return SWIG_OK;
164}
165}
166%fragment("SWIG_SciDouble_FromShortArrayAndSize", "header") {
167SWIGINTERN int
168SWIG_SciDouble_FromShortArrayAndSize(void *pvApiCtx, int iVarOut, int iRows, int iCols, short *psValue) {
169  SciErr sciErr;
170  int i;
171  double *pdValues = NULL;
172
173  pdValues = (double*) malloc(iRows * iCols * sizeof(double));
174  for (i=0; i<iRows * iCols; i++)
175    pdValues[i] = psValue[i];
176
177  sciErr = createMatrixOfDouble(pvApiCtx, SWIG_NbInputArgument(pvApiCtx) + iVarOut, iRows, iCols, pdValues);
178  if (sciErr.iErr) {
179    printError(&sciErr, 0);
180    free(pdValues);
181    return SWIG_ERROR;
182  }
183
184  free(pdValues);
185  return SWIG_OK;
186}
187}
188
189