use freeArrayOfString(...); (factorized code)
[scilab.git] / scilab / modules / tclsci / sci_gateway / c / sci_TCL_EvalStr.c
1 /*--------------------------------------------------------------------------*/
2 /* INRIA 2005 */
3 /* Allan CORNET */
4 /*--------------------------------------------------------------------------*/
5 #include "TCL_Global.h"
6 #include "gw_tclsci.h"
7 #include "Scierror.h"
8 #include "localization.h"
9 #include "freeArrayOfString.h"
10 /*--------------------------------------------------------------------------*/
11 int C2F(sci_TCL_EvalStr) _PARAMS((char *fname,unsigned long l))
12
13         CheckRhs(1,2);
14         CheckLhs(1,1);
15
16         if (GetType(1) == sci_strings)
17         {
18                 char **Str=NULL;
19                 int m1,n1,i,j,RET;
20                 int m2,n2,l2;
21         Tcl_Interp *TCLinterpreter=NULL;
22         char **ReturnArrayString=NULL;
23         int k=0;
24
25                 GetRhsVar(1,MATRIX_OF_STRING_DATATYPE,&m1,&n1,&Str);
26
27                 if (TCLinterp == NULL)
28                 {
29                         freeArrayOfString(Str,m1*n1);
30                         Scierror(999,_("%s : Error main TCL interpreter not initialized.\n"),fname);
31                         return 0;
32                 }
33
34                 if (Rhs==2)
35                 {
36                         /* two arguments given - get a pointer on the slave interpreter */
37                         if (GetType(2) == sci_strings)
38                         {
39                                 GetRhsVar(2,STRING_DATATYPE,&m2,&n2,&l2);
40                                 TCLinterpreter=Tcl_GetSlave(TCLinterp,cstk(l2));
41                                 if (TCLinterpreter==NULL)
42                                 {
43                                         freeArrayOfString(Str,m1*n1);
44                                         Scierror(999,_("%s: No such slave interpreter.\n"),fname);
45                                         return 0;
46                                 }
47                         }
48                         else
49                         {
50                                 freeArrayOfString(Str,m1*n1);
51                                 Scierror(999,_("%s : Argument type must be character string.\n"),fname);
52                                  return 0;
53                         }
54                 }
55                 else
56                 {
57                         /* only one argument given - use the main interpreter */
58                         TCLinterpreter=TCLinterp;
59                 }
60
61         ReturnArrayString = (char **) MALLOC(m1*n1*sizeof(char **));
62
63                 for (i = 0; i<m1*n1 ;i++)
64                 {
65                         char *RetStr=NULL;
66                         char *AsciiFromUTF8=NULL;
67
68                         RET=Tcl_Eval(TCLinterpreter,Str[i]);
69
70                         if (RET==TCL_ERROR)
71                         {
72                 const char *trace = Tcl_GetVar(TCLinterpreter, "errorInfo", TCL_GLOBAL_ONLY);
73                                 freeArrayOfString(Str,m1*n1);
74                                 if(Err>0)
75                                 {
76                                         Scierror(999,"%s, ScilabEval error at line %i\n %s.\n",fname,i+1,(char *)trace);
77                                 }
78                                 else
79                                 {
80                                         Scierror(999,"%s, %s at line %i\n       %s\n",fname,TCLinterpreter->result,i+1,(char *)trace);
81                                 }
82                                 return 0;
83             } 
84                         else
85                         {
86                 /* return result of the successful evaluation of the script */
87                 /* return a matrix of string results */
88                 RetStr = (char*)Tcl_GetStringResult(TCLinterpreter);
89                 AsciiFromUTF8=UTF8toANSI(TCLinterpreter,RetStr);
90                 ReturnArrayString[k++]=AsciiFromUTF8;
91             }
92                 }
93                 CreateVarFromPtr(Rhs+1,MATRIX_OF_STRING_DATATYPE, &m1, &n1, ReturnArrayString);
94                 LhsVar(1)=Rhs+1;
95                 C2F(putlhsvar)();
96
97                 for (i=0;i<n1;i++) for (j=0;j<m1;j++) { FREE(ReturnArrayString[i+n1*j]);ReturnArrayString[i+n1*j]=NULL; }
98                 FREE(ReturnArrayString);
99                 freeArrayOfString(Str,m1*n1);
100         }
101         else
102         {
103                 Scierror(999,_("Argument type incorrect: Must be a string or vector of strings.\n"));
104                 return 0;
105         }
106         
107         return 0;
108 }
109 /*--------------------------------------------------------------------------*/