File: integer_writing_api.c

package info (click to toggle)
scilab 5.3.3-10
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 330,656 kB
file content (250 lines) | stat: -rw-r--r-- 6,642 bytes parent folder | download | duplicates (2)
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
/*
 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 * Copyright (C) 2009-2010 - DIGITEO - Scilab Consortium Operational Team
 *
 * This file must be used under the terms of the CeCILL.
 * This source file is licensed as described in the file COPYING, which
 * you should have received as part of this distribution. The terms
 * are also available at
 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
 *
 */

#include "stack-c.h"
#include "Scierror.h"
#include "localization.h"
#include "sciprint.h"
#include "api_scilab.h"
#include "MALLOC.h"

void* create_output(int _iCoeff, int _iSize, int _iRows, int _iCols, void* _pvDataIn);
int read_integer(char *fname,unsigned long fname_len)
{
	SciErr sciErr;
	//output variable info
	int iRows8					= 0;
	int iCols8					= 0;
	int iRows16					= 0;
	int iCols16					= 0;
	int iRows32					= 0;
	int iCols32					= 0;
	int iRowsu8					= 0;
	int iColsu8					= 0;
	int iRowsu16				= 0;
	int iColsu16				= 0;
	int iRowsu32				= 0;
	int iColsu32				= 0;
	int iPrec					= 0;
	int* piAddr8				= NULL;
	int* piAddr16				= NULL;
	int* piAddr32				= NULL;
	int* piAddru8				= NULL;
	int* piAddru16				= NULL;
	int* piAddru32				= NULL;
	char* pcData				= NULL;
	short* psData				= NULL;
	int* piData					= NULL;
	unsigned char* pucData		= NULL;
	unsigned short* pusData		= NULL;
	unsigned int* puiData		= NULL;
	char* pcDataOut				= NULL;
	short* psDataOut			= NULL;
	int* piDataOut				= NULL;
	unsigned char* pucDataOut	= NULL;
	unsigned short* pusDataOut	= NULL;
	unsigned int* puiDataOut	= NULL;
	//check input/ouput arguments count
	CheckRhs(6,6);
	CheckLhs(6,6);
	//get varialbe address
	sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr8);
	if(sciErr.iErr)
	{
		printError(&sciErr, 0);
		return 0;
	}
	sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddru8);
	if(sciErr.iErr)
	{
		printError(&sciErr, 0);
		return 0;
	}
	sciErr = getVarAddressFromPosition(pvApiCtx, 3, &piAddr16);
	if(sciErr.iErr)
	{
		printError(&sciErr, 0);
		return 0;
	}
	sciErr = getVarAddressFromPosition(pvApiCtx, 4, &piAddru16);
	if(sciErr.iErr)
	{
		printError(&sciErr, 0);
		return 0;
	}
	sciErr = getVarAddressFromPosition(pvApiCtx, 5, &piAddr32);
	if(sciErr.iErr)
	{
		printError(&sciErr, 0);
		return 0;
	}
	sciErr = getVarAddressFromPosition(pvApiCtx, 6, &piAddru32);
	if(sciErr.iErr)
	{
		printError(&sciErr, 0);
		return 0;
	}
	//check variable precision
	sciErr = getMatrixOfIntegerPrecision(pvApiCtx, piAddr8, &iPrec);
	if(sciErr.iErr || iPrec != SCI_INT8)
	{
		printError(&sciErr, 0);
		return 0;
	}
	//check variable precision
	sciErr = getMatrixOfIntegerPrecision(pvApiCtx, piAddru8, &iPrec);
	if(sciErr.iErr || iPrec != SCI_UINT8)
	{
		printError(&sciErr, 0);
		return 0;
	}
	//check variable precision
	sciErr = getMatrixOfIntegerPrecision(pvApiCtx, piAddr16, &iPrec);
	if(sciErr.iErr || iPrec != SCI_INT16)
	{
		printError(&sciErr, 0);
		return 0;
	}
	//check variable precision
	sciErr = getMatrixOfIntegerPrecision(pvApiCtx, piAddru16, &iPrec);
	if(sciErr.iErr || iPrec != SCI_UINT16)
	{
		printError(&sciErr, 0);
		return 0;
	}
	//check variable precision
	sciErr = getMatrixOfIntegerPrecision(pvApiCtx, piAddr32, &iPrec);
	if(sciErr.iErr || iPrec != SCI_INT32)
	{
		printError(&sciErr, 0);
		return 0;
	}
	//check variable precision
	sciErr = getMatrixOfIntegerPrecision(pvApiCtx, piAddru32, &iPrec);
	if(sciErr.iErr || iPrec != SCI_UINT32)
	{
		printError(&sciErr, 0);
		return 0;
	}
	//retrieve dimensions and data
	sciErr = getMatrixOfInteger8(pvApiCtx, piAddr8, &iRows8, &iCols8, &pcData);
	if(sciErr.iErr)
	{
		printError(&sciErr, 0);
		return 0;
	}
	//retrieve dimensions and data
	sciErr = getMatrixOfUnsignedInteger8(pvApiCtx, piAddru8, &iRowsu8, &iColsu8, &pucData);
	if(sciErr.iErr)
	{
		printError(&sciErr, 0);
		return 0;
	}
	//retrieve dimensions and data
	sciErr = getMatrixOfInteger16(pvApiCtx, piAddr16, &iRows16, &iCols16, &psData);
	if(sciErr.iErr)
	{
		printError(&sciErr, 0);
		return 0;
	}
	//retrieve dimensions and data
	sciErr = getMatrixOfUnsignedInteger16(pvApiCtx, piAddru16, &iRowsu16, &iColsu16, &pusData);
	if(sciErr.iErr)
	{
		printError(&sciErr, 0);
		return 0;
	}
	//retrieve dimensions and data
	sciErr = getMatrixOfInteger32(pvApiCtx, piAddr32, &iRows32, &iCols32, &piData);
	if(sciErr.iErr)
	{
		printError(&sciErr, 0);
		return 0;
	}
	//retrieve dimensions and data
	sciErr = getMatrixOfUnsignedInteger32(pvApiCtx, piAddru32, &iRowsu32, &iColsu32, &puiData);
	if(sciErr.iErr)
	{
		printError(&sciErr, 0);
		return 0;
	}
	//alloc and fill new variable
	pcDataOut   = (char*)create_output(2, 1, iRows8, iCols8, (void*)pcData);
	pucDataOut  = (unsigned char*)create_output(4, 1, iRowsu8, iColsu8, (void*)pucData);
	psDataOut   = (short*)create_output(8, 2, iRows16, iCols16, (void*)psData);
	pusDataOut  = (unsigned short*)create_output(16, 2, iRowsu16, iColsu16, (void*)pusData);
	piDataOut   = (int*)create_output(32, 4, iRows32, iCols32, (void*)piData);
	puiDataOut  = (unsigned int*)create_output(64, 4, iRowsu32, iColsu32, (void*)puiData);
	//create new variable
	sciErr = createMatrixOfInteger8(pvApiCtx, Rhs + 1, iRows8, iCols8, pcDataOut);
	if(sciErr.iErr)
	{
		printError(&sciErr, 0);
		return 0;
	}
	//create new variable
	sciErr = createMatrixOfUnsignedInteger8(pvApiCtx, Rhs + 2, iRowsu8, iColsu8, pucDataOut);
	if(sciErr.iErr)
	{
		printError(&sciErr, 0);
		return 0;
	}
	//create new variable
	sciErr = createMatrixOfInteger16(pvApiCtx, Rhs + 3, iRows16, iCols16, psDataOut);
	if(sciErr.iErr)
	{
		printError(&sciErr, 0);
		return 0;
	}
	//create new variable
	sciErr = createMatrixOfUnsignedInteger16(pvApiCtx, Rhs + 4, iRowsu16, iColsu16, pusDataOut);
	if(sciErr.iErr)
	{
		printError(&sciErr, 0);
		return 0;
	}
	//create new variable
	sciErr = createMatrixOfInteger32(pvApiCtx, Rhs + 5, iRows32, iCols32, piDataOut);
	if(sciErr.iErr)
	{
		printError(&sciErr, 0);
		return 0;
	}
	//create new variable
	sciErr = createMatrixOfUnsignedInteger32(pvApiCtx, Rhs + 6, iRowsu32, iColsu32, puiDataOut);
	if(sciErr.iErr)
	{
		printError(&sciErr, 0);
		return 0;
	}
	//assign allocated variables to Lhs position
	LhsVar(1) = Rhs + 1;
	LhsVar(2) = Rhs + 2;
	LhsVar(3) = Rhs + 3;
	LhsVar(4) = Rhs + 4;
	LhsVar(5) = Rhs + 5;
	LhsVar(6) = Rhs + 6;
	return 0;
}
void* create_output(int _iCoeff, int _iSize, int _iRows, int _iCols, void* _pvDataIn)
{
	int i = 0;
	void* pvDataOut = (void*)malloc(_iSize * _iRows * _iCols);
	for(i = 0 ; i < _iRows * _iCols ; i++)
	{
		int iVal = 0;
		memcpy(&iVal, (char*)_pvDataIn + i * _iSize, _iSize);
		iVal *= _iCoeff;
		memcpy((char*)pvDataOut + i * _iSize, &iVal, _iSize);
	}
	return pvDataOut;
}