File: sci_format.c

package info (click to toggle)
scilab 5.2.2-9
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 334,832 kB
  • ctags: 52,586
  • sloc: xml: 526,945; ansic: 223,590; fortran: 163,080; java: 56,934; cpp: 33,840; tcl: 27,936; sh: 20,397; makefile: 9,908; ml: 9,451; perl: 1,323; cs: 614; lisp: 30
file content (338 lines) | stat: -rw-r--r-- 8,645 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
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
/*
 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 * Copyright (C) 2008 - DIGITEO - Allan CORNET
 * 
 * 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 "gw_core.h"
#include "stack-c.h"
#include "MALLOC.h"
#include "localization.h"
#include "Scierror.h"
#include "core_math.h"
/*--------------------------------------------------------------------------*/
#define e_type_format "e"
#define v_type_format "v"
#define format_MIN 2
#define format_MAX 25
#define format_e_MAX 8
#define mode_variable 1
#define mode_e 0
/*--------------------------------------------------------------------------*/
static void setVariableFormat(int numberDigits);
static void set_e_Format(int numberDigits);
static void getFormat(int *e_mode, int *numberDigits);
static void setVariableMode(void);
static int sci_format_norhs(char *fname);
static int sci_format_onerhs(char *fname);
static int sci_format_tworhs(char *fname);
/*--------------------------------------------------------------------------*/
int C2F(sci_format)(char *fname,unsigned long fname_len)
{
	Rhs = Max(0,Rhs);
	CheckRhs(0,2);
	CheckLhs(0,1);

	switch (Rhs)
	{
		case 2:
			return sci_format_tworhs(fname);
		case 1:
			return sci_format_onerhs(fname);
		break;
		case 0: default:
			return sci_format_norhs(fname);
	}
}
/*--------------------------------------------------------------------------*/
static void setVariableFormat(int numberDigits)
{
	int numberDigitsAdjusted = numberDigits;

	C2F(iop).lct[5] = mode_variable; /* set 'v' mode */

	numberDigitsAdjusted = Min( Max(format_MIN, numberDigits), format_MAX );
	C2F(iop).lct[6] = numberDigitsAdjusted;
}
/*--------------------------------------------------------------------------*/
static void setVariableMode(void)
{
	C2F(iop).lct[5] = mode_variable; /* set 'v' mode */
}
/*--------------------------------------------------------------------------*/
static void set_e_Format(int numberDigits)
{
	 int numberDigitsAdjusted = numberDigits;

	 C2F(iop).lct[5] = mode_e; /* set 'e' mode */

	 numberDigitsAdjusted = Min( Max(format_MIN, numberDigits), format_MAX );
     numberDigitsAdjusted = Max( numberDigitsAdjusted, format_e_MAX );

     C2F(iop).lct[6] = numberDigitsAdjusted;
}
/*--------------------------------------------------------------------------*/
static void getFormat(int *e_mode, int *numberDigits)
{
	*e_mode = C2F(iop).lct[5];
	*numberDigits = C2F(iop).lct[6];
}
/*--------------------------------------------------------------------------*/
static int sci_format_norhs(char *fname)
{
	int n1 = 0, m1 = 0;
	int *paramoutINT = NULL;
	paramoutINT = (int*)MALLOC(sizeof(int)*2);

	getFormat(&paramoutINT[0], &paramoutINT[1]);

	n1 = 1; m1 = 2;
	CreateVarFromPtr(Rhs+1, MATRIX_OF_INTEGER_DATATYPE, &n1, &m1, &paramoutINT);
	LhsVar(1) = Rhs+1;
	C2F(putlhsvar)();

	if (paramoutINT) {FREE(paramoutINT); paramoutINT = NULL;}
	return 0;
}
/*--------------------------------------------------------------------------*/
static int sci_format_onerhs(char *fname)
{
	if ( GetType(1) == sci_strings )
	{
		int n1 = 0, m1 = 0, l1 = 0;
		char *param = NULL;

		GetRhsVar(1,STRING_DATATYPE,&m1,&n1,&l1);
		param = cstk(l1);

		if ( ( strcmp(e_type_format, param) == 0 ) || ( strcmp(v_type_format, param) == 0 ) )
		{
			int previous_mode = 0;
			int previous_numberDigits = 0;

			getFormat(&previous_mode, &previous_numberDigits);

			if ( strcmp(e_type_format, param) == 0 )
			{
				set_e_Format(previous_numberDigits);
			}
			else /* v_type_format */
			{
				setVariableFormat(previous_numberDigits);
			}

			LhsVar(1) = 0;
			C2F(putlhsvar)();
		}
		else
		{
			Scierror(999,_("%s: Wrong values for input argument #%d: '%s' or '%s' expected.\n"),fname,1,e_type_format,v_type_format);
			return 0;
		}
	}
	else if ( GetType(1) == sci_matrix )
	{
		int n1 = 0, m1 = 0, l1 = 0;
		double dvalue;
		int value = 0;

		int mode = 0;
		int previous_numberDigits = 0;

		GetRhsVar(1,MATRIX_OF_DOUBLE_DATATYPE,&m1,&n1,&l1);

		if ( (m1  != 1) || (n1  != 1) )
		{
			Scierror(999,_("%s: Wrong size for input argument #%d: A scalar expected.\n"),fname,2);
			return 0;
		}

		dvalue = *stk(l1);
		value = (int) dvalue;

		if (dvalue != (double)value)
		{
			Scierror(999,_("%s: Wrong values for input argument #%d: A int expected.\n"),fname,1);
			return 0;
		}

		if ( (value < format_MIN) || (value > format_MAX) )
		{
			Scierror(999,_("%s: Wrong values for input argument #%d: Must be in the interval [%d, %d].\n"),fname,1,format_MIN,format_MAX);
			return 0;
		}

		getFormat(&mode, &previous_numberDigits);

		if (mode == mode_e)
		{
			set_e_Format(value);
		}
		else /* mode_variable */
		{
			setVariableFormat(value);
		}

		LhsVar(1) = 0;
		C2F(putlhsvar)();
	}
	else
	{
		Scierror(999,_("%s: Wrong type for input argument #%d: A string or a scalar expected.\n"),fname,1);
		return 0;
	}
	return 0;
}
/*--------------------------------------------------------------------------*/
static int sci_format_tworhs(char *fname)
{
	/* format(1,10) */
	if ((GetType(1) == sci_matrix) && (GetType(2) == sci_matrix))
	{
		int n1 = 0, m1 = 0, l1 = 0, m1n1 = 0;
		int n2 = 0, m2 = 0, l2 = 0, m2n2 = 0;

		GetRhsVar(1,MATRIX_OF_DOUBLE_DATATYPE,&m1,&n1,&l1);
		m1n1 = m1 * n1;

		GetRhsVar(2,MATRIX_OF_DOUBLE_DATATYPE,&m2,&n2,&l2);
		m2n2 = m2 * n2;

		if ( (m1n1 == 1) && (m2n2 == 1) )
		{
			double type_value_d = *stk(l2);
			double v_value_d = *stk(l1);
			int v_value = (int)v_value_d;

			if ( (type_value_d != (double)0) && (type_value_d != (double)1) )
			{
				Scierror(999,_("%s: Wrong value for input argument #%d: '0' or '1' expected.\n"),fname,2);
				return 0;
			}

			if (v_value_d != (double)v_value)
			{
				Scierror(999,_("%s: Wrong value for input argument #%d: A int expected.\n"),fname,1);
				return 0;
			}

			if ( (v_value < format_MIN) || (v_value > format_MAX) )
			{
				Scierror(999,_("%s: Wrong value for input argument #%d: Must be in the interval [%d, %d].\n"),fname,2,format_MIN,format_MAX);
				return 0;
			}

			if (type_value_d == 0)
			{
				setVariableFormat(v_value);
			}
			else
			{
				set_e_Format(v_value);
			}

			setVariableMode();

			LhsVar(1) = 0;
			C2F(putlhsvar)();
		}
		else
		{
			Scierror(999,_("%s: Wrong size for inputs arguments.\n"),fname);
		}
	}
	/* format('e',10) & format(10,'e') syntax */
	else if ( ((GetType(1) == sci_strings) && (GetType(2) == sci_matrix)) ||
		 ((GetType(1) == sci_matrix) && (GetType(2) == sci_strings)) )
	{
		int n1 = 0, m1 = 0, l1 = 0;
		char *param = NULL;

		int n2 = 0, m2 = 0, l2 = 0;
		int value = 0;
		

		if (GetType(1) == sci_strings)
		{
			double dvalue = 0;

			GetRhsVar(1,STRING_DATATYPE,&m1,&n1,&l1);
			param = cstk(l1);

			GetRhsVar(2,MATRIX_OF_DOUBLE_DATATYPE,&m2,&n2,&l2);

			if ( (m2  != 1) || (n2  != 1) )
			{
				Scierror(999,_("%s: Wrong size for input argument #%d: A scalar expected.\n"),fname,2);
				return 0;
			}

			dvalue = *stk(l2);
			value = (int)dvalue;
			if (dvalue != (double)value)
			{
				Scierror(999,_("%s: Wrong values for input argument #%d: A int expected.\n"),fname,2);
				return 0;
			}
		}
		else /* matrix */
		{
			double dvalue = 0;

			GetRhsVar(1,MATRIX_OF_DOUBLE_DATATYPE,&m1,&n1,&l1);

			if ( (m1  != 1) || (n1  != 1) )
			{
				Scierror(999,_("%s: Wrong size for input argument #%d: A scalar expected.\n"),fname,1);
				return 0;
			}

			dvalue = *stk(l1);
			value = (int)dvalue;
			if (dvalue != (double)value)
			{
				Scierror(999,_("%s: Wrong values for input argument #%d: A int expected.\n"),fname,1);
				return 0;
			}

			GetRhsVar(2,STRING_DATATYPE,&m2,&n2,&l2);
			param = cstk(l2);
		}

		if ( ( strcmp(e_type_format, param) == 0 ) || ( strcmp(v_type_format, param) == 0 ) )
		{
			if ( (value < format_MIN) || (value > format_MAX) )
			{
				Scierror(999,_("%s: Wrong value for input argument #%d: Must be in the interval [%d, %d].\n"),fname,1,format_MIN,format_MAX);
				return 0;
			}

			if ( strcmp(e_type_format, param) == 0 )
			{
				 set_e_Format(value);
			}
			else /* v_type_format */
			{
				 setVariableFormat(value);
			}

			LhsVar(1) = 0;
			C2F(putlhsvar)();
		}
		else
		{
			Scierror(999,_("%s: Wrong values for input argument #%d: '%s' or '%s' expected.\n"),fname,1,e_type_format,v_type_format);
		}
	}
	else
	{
		Scierror(999,_("%s: Wrong type for inputs arguments.\n"),fname);
	}
	return 0;
}
/*--------------------------------------------------------------------------*/