2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2006 - INRIA - Fabrice Leray
4 * Copyright (C) 2006 - INRIA - Jean-Baptiste Silvy
5 * Copyright (C) 2009 - INRIA - Pierre Lando
6 * Copyright (C) 2011 - DIGITEO - Manuel Juliachs
8 * This file must be used under the terms of the CeCILL.
9 * This source file is licensed as described in the file COPYING, which
10 * you should have received as part of this distribution. The terms
11 * are also available at
12 * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
16 /*------------------------------------------------------------------------*/
17 /* file: GetCommandArg.h */
18 /* desc : tools to retrieve parameters within the command line for */
19 /* graphic routines. */
20 /*------------------------------------------------------------------------*/
23 #include "GetCommandArg.h"
24 #include "GetProperty.h"
25 #include "DefaultCommandArg.h"
26 #include "CurrentSubwin.h"
27 #include "localization.h"
29 #include "BuildObjects.h"
30 #include "api_scilab.h"
31 #include "sci_malloc.h"
33 static char logFlagsCpy[3] ; /* real logflags may use either this or the stack */
35 /*--------------------------------------------------------------------------*/
37 /*--------------------------------------------------------------------------*/
38 int get_style_arg(void* _pvCtx, char *fname, int pos, int n1, rhs_opts opts[], int ** style)
40 int m = 0, n = 0, first_opt = FirstOpt(_pvCtx), kopt = 0, un = 1, ix = 0, i = 0, l1 = 0;
42 if ( pos < first_opt ) /* regular argument */
47 getVarAddressFromPosition(_pvCtx, pos, &piAddr);
48 getVarType(_pvCtx, piAddr, &iType);
52 getMatrixOfDoubleAsInteger(_pvCtx, piAddr, &m, &n, &piData);
55 Scierror(999, _("%s: Wrong size for input argument #%d: %d < %d expected.\n"), fname, pos, m * n, n1);
59 if ( n1 == 1 && m * n == 1 )
61 *style = (int*)MALLOC(2 * sizeof(int));
62 (*style)[0] = piData[0];
67 *style = (int*)MALLOC(m * n * sizeof(int));
68 for (i = 0; i < m * n; i++)
70 (*style)[i] = piData[i];
74 else /* zero type argument --> default value */
77 *style = (int*)MALLOC(ix * sizeof(int));
80 for ( i = 0 ; i < n1 ; ++i )
86 else if ((kopt = FindOpt(_pvCtx, "style", opts)) >= 0)
88 /* optinal argument: style=value */
91 getMatrixOfDoubleAsInteger(_pvCtx, opts[kopt].piAddr, &m, &n, &piData);
94 Scierror(999, _("%s: Wrong size for input argument #%d: %d < %d expected.\n"), fname, kopt, m * n, n1);
98 if (n1 == 1 && m * n == 1)
100 *style = (int*)MALLOC(2 * sizeof(int));
101 (*style)[0] = piData[0];
106 *style = (int*)MALLOC(m * n * sizeof(int));
107 for (i = 0; i < m * n; i++)
109 (*style)[i] = piData[i];
113 else /* unspecified argument --> default value */
116 *style = (int*)MALLOC(ix * sizeof(int));
119 for (i = 0 ; i < n1 ; ++i)
127 /*--------------------------------------------------------------------------*/
129 /*--------------------------------------------------------------------------*/
130 int get_rect_arg(void* _pvCtx, char *fname, int pos, rhs_opts opts[], double ** rect)
132 int m, n, first_opt = FirstOpt(_pvCtx), kopt, i;
138 double* pdblData = NULL;
139 getVarAddressFromPosition(_pvCtx, pos, &piAddr);
140 getVarType(_pvCtx, piAddr, &iType);
144 getMatrixOfDouble(_pvCtx, piAddr, &m, &n, &pdblData);
147 Scierror(999, "%s: Wrong size for input argument #%d: %d expected\n", fname, pos, 4);
153 for (i = 0; i < 4; i++)
155 if (finite((*rect)[i]) == 0)
157 Scierror(999, "%s: Wrong values (Nan or Inf) for input argument: %d finite values expected\n", fname, 4);
164 /** global value can be modified **/
165 double zeros[4] = { 0.0, 0.0, 0.0, 0.0 };
167 *rect = getDefRect();
170 else if ((kopt = FindOpt(_pvCtx, "rect", opts)) >= 0) /* named argument: rect=value */
172 double* pdblData = NULL;
173 getMatrixOfDouble(_pvCtx, opts[kopt].piAddr, &m, &n, &pdblData);
176 Scierror(999, "%s: Wrong size for input argument #%d: %d expected\n", fname, kopt, 4);
182 for (i = 0; i < 4; i++)
184 if (finite((*rect)[i]) == 0)
186 Scierror(999, "%s: Wrong values (Nan or Inf) for input argument: %d finite values expected\n", fname, 4);
193 /** global value can be modified **/
194 double zeros[4] = { 0.0, 0.0, 0.0, 0.0 };
196 *rect = getDefRect();
201 /*--------------------------------------------------------------------------*/
202 int get_strf_arg(void* _pvCtx, char *fname, int pos, rhs_opts opts[], char ** strf)
204 int first_opt = FirstOpt(_pvCtx), kopt;
210 char* pstData = NULL;
211 getVarAddressFromPosition(_pvCtx, pos, &piAddr);
212 getVarType(_pvCtx, piAddr, &iType);
216 getAllocatedSingleString(_pvCtx, piAddr, &pstData);
217 if ((int)strlen(pstData) != 3)
219 freeAllocatedSingleString(pstData);
220 Scierror(999, _("%s: Wrong size for input argument #%d: String of %d characters expected.\n"), fname, pos, 3);
227 /* def value can be changed */
229 *strf = getDefStrf();
232 else if ((kopt = FindOpt(_pvCtx, "strf", opts)) >= 0)
234 char* pstData = NULL;
235 getAllocatedSingleString(_pvCtx, opts[kopt].piAddr, &pstData);
236 if ((int)strlen(pstData) != 3)
238 freeAllocatedSingleString(pstData);
239 Scierror(999, _("%s: Wrong size for input argument #%d: String of %d characters expected.\n"), fname, kopt, 3);
246 /* def value can be changed */
248 *strf = getDefStrf();
253 /*--------------------------------------------------------------------------*/
254 int get_legend_arg(void* _pvCtx, char *fname, int pos, rhs_opts opts[], char ** legend)
256 int first_opt = FirstOpt(_pvCtx), kopt;
262 char* pstData = NULL;
263 getVarAddressFromPosition(_pvCtx, pos, &piAddr);
264 getVarType(_pvCtx, piAddr, &iType);
268 getAllocatedSingleString(_pvCtx, piAddr, &pstData);
273 *legend = getDefLegend();
276 else if ((kopt = FindOpt(_pvCtx, "leg", opts)) >= 0)
278 char* pstData = NULL;
279 getAllocatedSingleString(_pvCtx, opts[kopt].piAddr, &pstData);
284 *legend = getDefLegend();
288 /*--------------------------------------------------------------------------*/
290 * retrieve the labels from the command line and store them into labels
292 int get_labels_arg(void* _pvCtx, char *fname, int pos, rhs_opts opts[], char ** labels)
294 int first_opt = FirstOpt(_pvCtx), kopt;
300 char* pstData = NULL;
301 getVarAddressFromPosition(_pvCtx, pos, &piAddr);
302 getVarType(_pvCtx, piAddr, &iType);
306 getAllocatedSingleString(_pvCtx, piAddr, &pstData);
311 /* jb silvy 03/2006 */
312 /* do not change the legend if one already exists */
313 if (sciGetLegendDefined(getOrCreateDefaultSubwin()))
319 *labels = getDefLegend();
323 else if ((kopt = FindOpt(_pvCtx, "leg", opts)) >= 0)
325 char* pstData = NULL;
326 getAllocatedSingleString(_pvCtx, opts[kopt].piAddr, &pstData);
331 /* jb silvy 03/2006 */
332 /* do not change the legend if one already exists */
333 if (sciGetLegendDefined(getOrCreateDefaultSubwin()))
339 *labels = getDefLegend();
345 /*--------------------------------------------------------------------------*/
346 int get_nax_arg(void* _pvCtx, int pos, rhs_opts opts[], int ** nax, BOOL * flagNax)
348 int i, m, n, first_opt = FirstOpt(_pvCtx), kopt;
355 getVarAddressFromPosition(_pvCtx, pos, &piAddr);
356 getVarType(_pvCtx, piAddr, &iType);
360 getMatrixOfDoubleAsInteger(_pvCtx, piAddr, &m, &n, &piData);
366 for (i = 0 ; i < 4; ++i)
368 // When i = 1 or 3 we talk about the number of ticks, this value can be -1 to say 'AutoTicks'
369 piData[i] = Max(piData[i], -(i % 2));
380 else if ((kopt = FindOpt(_pvCtx, "nax", opts)) >= 0)
384 getMatrixOfDoubleAsInteger(_pvCtx, opts[kopt].piAddr, &m, &n, &piData);
390 for (i = 0 ; i < 4; ++i)
392 // When i = 1 or 3 we talk about the number of ticks, this value can be -1 to say 'AutoTicks'
393 piData[i] = Max(piData[i], -(i % 2));
407 /*--------------------------------------------------------------------------*/
408 int get_zminmax_arg(void* _pvCtx, char *fname, int pos, rhs_opts opts[], double ** zminmax)
410 int m, n, first_opt = FirstOpt(_pvCtx), kopt;
416 double* pdblData = NULL;
417 getVarAddressFromPosition(_pvCtx, pos, &piAddr);
418 getVarType(_pvCtx, piAddr, &iType);
422 getMatrixOfDouble(_pvCtx, piAddr, &m, &n, &pdblData);
425 Scierror(999, "%s: Wrong size for input argument #%d: %d expected\n", fname, pos, 2);
432 /** global value can be modified **/
433 double zeros[2] = { 0.0, 0.0 };
434 setDefZminMax(zeros);
435 *zminmax = getDefZminMax();
438 else if ((kopt = FindOpt(_pvCtx, "zminmax", opts)) >= 0) /* named argument: rect=value */
440 double* pdblData = NULL;
441 getMatrixOfDouble(_pvCtx, opts[kopt].piAddr, &m, &n, &pdblData);
444 Scierror(999, "%s: Wrong size for input argument #%d: %d expected\n", fname, kopt, 2);
451 /** global value can be modified **/
452 double zeros[2] = { 0.0, 0.0 };
453 setDefZminMax(zeros);
454 *zminmax = getDefZminMax();
460 /*--------------------------------------------------------------------------*/
461 int get_colminmax_arg(void* _pvCtx, char *fname, int pos, rhs_opts opts[], int ** colminmax)
463 int m, n, first_opt = FirstOpt(_pvCtx), kopt;
470 getVarAddressFromPosition(_pvCtx, pos, &piAddr);
471 getVarType(_pvCtx, piAddr, &iType);
475 getMatrixOfDoubleAsInteger(_pvCtx, piAddr, &m, &n, &piData);
484 /** global value can be modified **/
485 int zeros[2] = { 0, 0 };
486 setDefColMinMax(zeros);
487 *colminmax = getDefColMinMax();
490 else if ((kopt = FindOpt(_pvCtx, "colminmax", opts)) >= 0)
494 getMatrixOfDoubleAsInteger(_pvCtx, opts[kopt].piAddr, &m, &n, &piData);
503 /** global value can be modified **/
504 int zeros[2] = { 0, 0 };
505 setDefColMinMax(zeros);
506 *colminmax = getDefColMinMax();
511 /*--------------------------------------------------------------------------*/
512 int get_colout_arg(void* _pvCtx, char *fname, int pos, rhs_opts opts[], int ** colout)
514 int m, n, first_opt = FirstOpt(_pvCtx), kopt;
521 getVarAddressFromPosition(_pvCtx, pos, &piAddr);
522 getVarType(_pvCtx, piAddr, &iType);
526 getMatrixOfDoubleAsInteger(_pvCtx, piAddr, &m, &n, &piData);
535 /** global value can be modified **/
536 int newDefCO[2] = { -1, -1 };
537 setDefColOut(newDefCO);
538 *colout = getDefColOut();
541 else if ((kopt = FindOpt(_pvCtx, "colout", opts)) >= 0)
545 getMatrixOfDoubleAsInteger(_pvCtx, opts[kopt].piAddr, &m, &n, &piData);
554 /** global value can be modified **/
555 int newDefCO[2] = { -1, -1 };
556 setDefColOut(newDefCO);
557 *colout = getDefColOut();
561 /*--------------------------------------------------------------------------*/
562 int get_with_mesh_arg(void* _pvCtx, char *fname, int pos, rhs_opts opts[], BOOL * withMesh)
564 int first_opt = FirstOpt(_pvCtx), kopt;
571 getVarAddressFromPosition(_pvCtx, pos, &piAddr);
572 getVarType(_pvCtx, piAddr, &iType);
576 getScalarBoolean(_pvCtx, piAddr, &iData);
581 /** global value can be modified **/
582 setDefWithMesh(FALSE);
583 *withMesh = getDefWithMesh();
586 else if ((kopt = FindOpt(_pvCtx, "mesh", opts)) >= 0)
590 getScalarBoolean(_pvCtx, opts[kopt].piAddr, &iData);
595 /** global value can be modified **/
596 setDefWithMesh(FALSE);
597 *withMesh = getDefWithMesh();
602 /*--------------------------------------------------------------------------*/
603 int get_logflags_arg(void* _pvCtx, char *fname, int pos, rhs_opts opts[], char ** logFlags)
610 if (pos < FirstOpt(_pvCtx)) //input argument */
612 //no idea of the real goal of this, how input var can have type == 0 Oo
613 if (getInputArgumentType(_pvCtx, pos) == 0)
615 *logFlags = getDefLogFlags();
619 getVarAddressFromPosition(_pvCtx, pos, &piAddr);
621 else if ((kopt = FindOpt(_pvCtx, "logflag", opts)) >= 0)//optional argument
623 piAddr = opts[kopt].piAddr;
628 *logFlags = getDefLogFlags();
632 getAllocatedSingleString(_pvCtx, piAddr, &pstLog);
633 iLog = (int)strlen(pstLog);
634 if (iLog != 2 && iLog != 3)
636 Scierror(999, "%s: Wrong size for input argument #%d: %d or %d expected\n", fname, pos, 2, 3);
642 if ((pstLog[0] != 'l' && pstLog[0] != 'n') || (pstLog[1] != 'l' && pstLog[1] != 'n'))
649 logFlagsCpy[0] = 'g';
650 logFlagsCpy[1] = pstLog[0];
651 logFlagsCpy[2] = pstLog[1];
652 *logFlags = logFlagsCpy;
656 if (((pstLog[0] != 'g') && (pstLog[0] != 'e') && (pstLog[0] != 'o')) ||
657 (pstLog[1] != 'l' && pstLog[1] != 'n') ||
658 (pstLog[2] != 'l' && pstLog[2] != 'n'))
670 /*--------------------------------------------------------------------------*/
671 int get_optional_double_arg(void* _pvCtx, char* fname, int pos, char* name, double** value, int sz, rhs_opts opts[])
673 int m, n, first_opt = FirstOpt(_pvCtx), kopt;
679 double* pdblData = NULL;
680 getVarAddressFromPosition(_pvCtx, pos, &piAddr);
681 getVarType(_pvCtx, piAddr, &iType);
685 getMatrixOfDouble(_pvCtx, piAddr, &m, &n, &pdblData);
693 else if ((kopt = FindOpt(_pvCtx, name, opts)) >= 0)
695 double* pdblData = NULL;
696 getMatrixOfDouble(_pvCtx, opts[kopt].piAddr, &m, &n, &pdblData);
699 Scierror(999, "%s: Wrong size for input argument #%d: %d expected\n", fname, kopt, 4);
707 /*--------------------------------------------------------------------------*/
708 int get_optional_int_arg(void* _pvCtx, char* fname, int pos, char* name, int** value, int sz, rhs_opts opts[])
710 int m, n, first_opt = FirstOpt(_pvCtx), kopt;
717 getVarAddressFromPosition(_pvCtx, pos, &piAddr);
718 getVarType(_pvCtx, piAddr, &iType);
722 getMatrixOfDoubleAsInteger(_pvCtx, piAddr, &m, &n, &piData);
730 else if ((kopt = FindOpt(_pvCtx, name, opts)) >= 0)
734 getMatrixOfDoubleAsInteger(_pvCtx, opts[kopt].piAddr, &m, &n, &piData);
743 /*--------------------------------------------------------------------------*/