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);
215 Scierror(999, _("%s: Wrong type for input argument #%d: String expected.\n"), fname, pos);
219 getAllocatedSingleString(_pvCtx, piAddr, &pstData);
220 if ((int)strlen(pstData) != 3)
222 freeAllocatedSingleString(pstData);
223 Scierror(999, _("%s: Wrong size for input argument #%d: String of %d characters expected.\n"), fname, pos, 3);
228 else if ((kopt = FindOpt(_pvCtx, "strf", opts)) >= 0)
230 char* pstData = NULL;
232 getVarType(_pvCtx, opts[kopt].piAddr, &iType);
235 Scierror(999, _("%s: Wrong type for input argument #%d: String expected.\n"), fname, pos);
239 getAllocatedSingleString(_pvCtx, opts[kopt].piAddr, &pstData);
240 if ((int)strlen(pstData) != 3)
242 freeAllocatedSingleString(pstData);
243 Scierror(999, _("%s: Wrong size for input argument #%d: String of %d characters expected.\n"), fname, kopt, 3);
250 /* def value can be changed */
252 *strf = getDefStrf();
257 /*--------------------------------------------------------------------------*/
258 int get_legend_arg(void* _pvCtx, char *fname, int pos, rhs_opts opts[], char ** legend)
260 int first_opt = FirstOpt(_pvCtx), kopt;
266 char* pstData = NULL;
267 getVarAddressFromPosition(_pvCtx, pos, &piAddr);
268 getVarType(_pvCtx, piAddr, &iType);
272 getAllocatedSingleString(_pvCtx, piAddr, &pstData);
277 *legend = getDefLegend();
280 else if ((kopt = FindOpt(_pvCtx, "leg", opts)) >= 0)
282 char* pstData = NULL;
283 getAllocatedSingleString(_pvCtx, opts[kopt].piAddr, &pstData);
288 *legend = getDefLegend();
292 /*--------------------------------------------------------------------------*/
294 * retrieve the labels from the command line and store them into labels
296 int get_labels_arg(void* _pvCtx, char *fname, int pos, rhs_opts opts[], char ** labels)
298 int first_opt = FirstOpt(_pvCtx), kopt;
304 char* pstData = NULL;
305 getVarAddressFromPosition(_pvCtx, pos, &piAddr);
306 getVarType(_pvCtx, piAddr, &iType);
310 getAllocatedSingleString(_pvCtx, piAddr, &pstData);
315 /* jb silvy 03/2006 */
316 /* do not change the legend if one already exists */
317 if (sciGetLegendDefined(getOrCreateDefaultSubwin()))
323 *labels = getDefLegend();
327 else if ((kopt = FindOpt(_pvCtx, "leg", opts)) >= 0)
329 char* pstData = NULL;
330 getAllocatedSingleString(_pvCtx, opts[kopt].piAddr, &pstData);
335 /* jb silvy 03/2006 */
336 /* do not change the legend if one already exists */
337 if (sciGetLegendDefined(getOrCreateDefaultSubwin()))
343 *labels = getDefLegend();
349 /*--------------------------------------------------------------------------*/
350 int get_nax_arg(void* _pvCtx, int pos, rhs_opts opts[], int ** nax, BOOL * flagNax)
352 int i, m, n, first_opt = FirstOpt(_pvCtx), kopt;
359 getVarAddressFromPosition(_pvCtx, pos, &piAddr);
360 getVarType(_pvCtx, piAddr, &iType);
364 getMatrixOfDoubleAsInteger(_pvCtx, piAddr, &m, &n, &piData);
370 for (i = 0 ; i < 4; ++i)
372 // When i = 1 or 3 we talk about the number of ticks, this value can be -1 to say 'AutoTicks'
373 piData[i] = Max(piData[i], -(i % 2));
384 else if ((kopt = FindOpt(_pvCtx, "nax", opts)) >= 0)
388 getMatrixOfDoubleAsInteger(_pvCtx, opts[kopt].piAddr, &m, &n, &piData);
394 for (i = 0 ; i < 4; ++i)
396 // When i = 1 or 3 we talk about the number of ticks, this value can be -1 to say 'AutoTicks'
397 piData[i] = Max(piData[i], -(i % 2));
411 /*--------------------------------------------------------------------------*/
412 int get_zminmax_arg(void* _pvCtx, char *fname, int pos, rhs_opts opts[], double ** zminmax)
414 int m, n, first_opt = FirstOpt(_pvCtx), kopt;
420 double* pdblData = NULL;
421 getVarAddressFromPosition(_pvCtx, pos, &piAddr);
422 getVarType(_pvCtx, piAddr, &iType);
426 getMatrixOfDouble(_pvCtx, piAddr, &m, &n, &pdblData);
429 Scierror(999, "%s: Wrong size for input argument #%d: %d expected\n", fname, pos, 2);
436 /** global value can be modified **/
437 double zeros[2] = { 0.0, 0.0 };
438 setDefZminMax(zeros);
439 *zminmax = getDefZminMax();
442 else if ((kopt = FindOpt(_pvCtx, "zminmax", opts)) >= 0) /* named argument: rect=value */
444 double* pdblData = NULL;
445 getMatrixOfDouble(_pvCtx, opts[kopt].piAddr, &m, &n, &pdblData);
448 Scierror(999, "%s: Wrong size for input argument #%d: %d expected\n", fname, kopt, 2);
455 /** global value can be modified **/
456 double zeros[2] = { 0.0, 0.0 };
457 setDefZminMax(zeros);
458 *zminmax = getDefZminMax();
464 /*--------------------------------------------------------------------------*/
465 int get_colminmax_arg(void* _pvCtx, char *fname, int pos, rhs_opts opts[], int ** colminmax)
467 int m, n, first_opt = FirstOpt(_pvCtx), kopt;
474 getVarAddressFromPosition(_pvCtx, pos, &piAddr);
475 getVarType(_pvCtx, piAddr, &iType);
479 getMatrixOfDoubleAsInteger(_pvCtx, piAddr, &m, &n, &piData);
488 /** global value can be modified **/
489 int zeros[2] = { 0, 0 };
490 setDefColMinMax(zeros);
491 *colminmax = getDefColMinMax();
494 else if ((kopt = FindOpt(_pvCtx, "colminmax", opts)) >= 0)
498 getMatrixOfDoubleAsInteger(_pvCtx, opts[kopt].piAddr, &m, &n, &piData);
507 /** global value can be modified **/
508 int zeros[2] = { 0, 0 };
509 setDefColMinMax(zeros);
510 *colminmax = getDefColMinMax();
515 /*--------------------------------------------------------------------------*/
516 int get_colout_arg(void* _pvCtx, char *fname, int pos, rhs_opts opts[], int ** colout)
518 int m, n, first_opt = FirstOpt(_pvCtx), kopt;
525 getVarAddressFromPosition(_pvCtx, pos, &piAddr);
526 getVarType(_pvCtx, piAddr, &iType);
530 getMatrixOfDoubleAsInteger(_pvCtx, piAddr, &m, &n, &piData);
539 /** global value can be modified **/
540 int newDefCO[2] = { -1, -1 };
541 setDefColOut(newDefCO);
542 *colout = getDefColOut();
545 else if ((kopt = FindOpt(_pvCtx, "colout", opts)) >= 0)
549 getMatrixOfDoubleAsInteger(_pvCtx, opts[kopt].piAddr, &m, &n, &piData);
558 /** global value can be modified **/
559 int newDefCO[2] = { -1, -1 };
560 setDefColOut(newDefCO);
561 *colout = getDefColOut();
565 /*--------------------------------------------------------------------------*/
566 int get_with_mesh_arg(void* _pvCtx, char *fname, int pos, rhs_opts opts[], BOOL * withMesh)
568 int first_opt = FirstOpt(_pvCtx), kopt;
575 getVarAddressFromPosition(_pvCtx, pos, &piAddr);
576 getVarType(_pvCtx, piAddr, &iType);
580 getScalarBoolean(_pvCtx, piAddr, &iData);
585 /** global value can be modified **/
586 setDefWithMesh(FALSE);
587 *withMesh = getDefWithMesh();
590 else if ((kopt = FindOpt(_pvCtx, "mesh", opts)) >= 0)
594 getScalarBoolean(_pvCtx, opts[kopt].piAddr, &iData);
599 /** global value can be modified **/
600 setDefWithMesh(FALSE);
601 *withMesh = getDefWithMesh();
606 /*--------------------------------------------------------------------------*/
607 int get_logflags_arg(void* _pvCtx, char *fname, int pos, rhs_opts opts[], char ** logFlags)
614 if (pos < FirstOpt(_pvCtx)) //input argument */
616 //no idea of the real goal of this, how input var can have type == 0 Oo
617 if (getInputArgumentType(_pvCtx, pos) == 0)
619 *logFlags = getDefLogFlags();
623 getVarAddressFromPosition(_pvCtx, pos, &piAddr);
625 else if ((kopt = FindOpt(_pvCtx, "logflag", opts)) >= 0)//optional argument
627 piAddr = opts[kopt].piAddr;
632 *logFlags = getDefLogFlags();
636 getAllocatedSingleString(_pvCtx, piAddr, &pstLog);
637 iLog = (int)strlen(pstLog);
638 if (iLog != 2 && iLog != 3)
640 Scierror(999, "%s: Wrong size for input argument #%d: %d or %d expected\n", fname, pos, 2, 3);
646 if ((pstLog[0] != 'l' && pstLog[0] != 'n') || (pstLog[1] != 'l' && pstLog[1] != 'n'))
653 logFlagsCpy[0] = 'g';
654 logFlagsCpy[1] = pstLog[0];
655 logFlagsCpy[2] = pstLog[1];
656 *logFlags = logFlagsCpy;
660 if (((pstLog[0] != 'g') && (pstLog[0] != 'e') && (pstLog[0] != 'o')) ||
661 (pstLog[1] != 'l' && pstLog[1] != 'n') ||
662 (pstLog[2] != 'l' && pstLog[2] != 'n'))
674 /*--------------------------------------------------------------------------*/
675 int get_optional_double_arg(void* _pvCtx, char* fname, int pos, char* name, double** value, int sz, rhs_opts opts[])
677 int m, n, first_opt = FirstOpt(_pvCtx), kopt;
683 double* pdblData = NULL;
684 getVarAddressFromPosition(_pvCtx, pos, &piAddr);
685 getVarType(_pvCtx, piAddr, &iType);
689 getMatrixOfDouble(_pvCtx, piAddr, &m, &n, &pdblData);
697 else if ((kopt = FindOpt(_pvCtx, name, opts)) >= 0)
699 double* pdblData = NULL;
700 getMatrixOfDouble(_pvCtx, opts[kopt].piAddr, &m, &n, &pdblData);
703 Scierror(999, "%s: Wrong size for input argument #%d: %d expected\n", fname, kopt, 4);
711 /*--------------------------------------------------------------------------*/
712 int get_optional_int_arg(void* _pvCtx, char* fname, int pos, char* name, int** value, int sz, rhs_opts opts[])
714 int m, n, first_opt = FirstOpt(_pvCtx), kopt;
721 getVarAddressFromPosition(_pvCtx, pos, &piAddr);
722 getVarType(_pvCtx, piAddr, &iType);
726 getMatrixOfDoubleAsInteger(_pvCtx, piAddr, &m, &n, &piData);
734 else if ((kopt = FindOpt(_pvCtx, name, opts)) >= 0)
738 getMatrixOfDoubleAsInteger(_pvCtx, opts[kopt].piAddr, &m, &n, &piData);
747 /*--------------------------------------------------------------------------*/