set windows environment for ilib_for_link, add getenv, setenv, dos, winqueryreg gateways
[scilab.git] / scilab / modules / io / sci_gateway / c / sci_setenv.c
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2009 - DIGITEO - Allan CORNET
4  * 
5  * This file must be used under the terms of the CeCILL.
6  * This source file is licensed as described in the file COPYING, which
7  * you should have received as part of this distribution.  The terms
8  * are also available at    
9  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
10  *
11  */
12
13 /*--------------------------------------------------------------------------*/
14 #include <stdio.h>
15 #include <string.h>
16 #include "gw_io.h"
17 #include "stack-c.h"
18 #include "api_scilab.h"
19 #include "api_oldstack.h"
20 #include "setenvc.h"
21 #include "MALLOC.h" /* MALLOC */
22 #include "Scierror.h"
23 #include "localization.h"
24 /*--------------------------------------------------------------------------*/
25 int sci_setenv(char *fname, int* _piKey)
26 {
27         SciErr sciErr;
28         int m1 = 0, n1 = 0;
29         int *piAddressVarOne = NULL;
30         int iType1 = 0;
31         char *pStVarOne = NULL;
32         int lenStVarOne = 0;
33
34         int m2 = 0, n2 = 0;
35         int *piAddressVarTwo = NULL;
36         int iType2 = 0;
37         char *pStVarTwo = NULL;
38         int lenStVarTwo = 0;
39
40         int m_out1 = 0, n_out1 = 0;
41         int *pbAddressOut1 = NULL;
42
43         int result = 0;
44
45         CheckRhs(2,2);
46         CheckLhs(0,1);
47
48         sciErr = getVarAddressFromPosition(_piKey, 1, &piAddressVarOne);
49         if(sciErr.iErr)
50         {
51                 printError(&sciErr, 0);
52                 return 0;
53         }
54
55         sciErr = getVarAddressFromPosition(_piKey, 2, &piAddressVarTwo);
56         if(sciErr.iErr)
57         {
58                 printError(&sciErr, 0);
59                 return 0;
60         }
61
62         sciErr = getVarType(_piKey, piAddressVarOne, &iType1);
63         if(sciErr.iErr)
64         {
65                 printError(&sciErr, 0);
66                 return 0;
67         }
68
69         if (iType1  != sci_strings )
70         {
71                 Scierror(999,_("%s: Wrong type for input argument #%d: A string expected.\n"),fname,1);
72                 return 0;
73         }
74
75         sciErr = getVarType(_piKey, piAddressVarTwo, &iType2);
76         if(sciErr.iErr)
77         {
78                 printError(&sciErr, 0);
79                 return 0;
80         }
81
82         if (iType2  != sci_strings )
83         {
84                 Scierror(999,_("%s: Wrong type for input argument #%d: A string expected.\n"),fname,2);
85                 return 0;
86         }
87
88         sciErr = getMatrixOfString(_piKey, piAddressVarOne,&m1,&n1,&lenStVarOne,&pStVarOne);
89         if(sciErr.iErr)
90         {
91                 printError(&sciErr, 0);
92                 return 0;
93         }
94
95         if ( (m1 != n1) && (n1 != 1) ) 
96         {
97                 Scierror(999,_("%s: Wrong size for input argument #%d: A string expected.\n"),fname,1);
98                 return 0;
99         }
100
101         sciErr = getMatrixOfString(_piKey, piAddressVarTwo,&m2,&n2,&lenStVarTwo,&pStVarTwo);
102         if(sciErr.iErr)
103         {
104                 printError(&sciErr, 0);
105                 return 0;
106         }
107
108         if ( (m2 != n2) && (n2 != 1) ) 
109         {
110                 Scierror(999,_("%s: Wrong size for input argument #%d: A string expected.\n"),fname,2);
111                 return 0;
112         }
113
114         pStVarOne = (char*)MALLOC(sizeof(char)*(lenStVarOne + 1));
115         if (pStVarOne)
116         {
117                 sciErr = getMatrixOfString(_piKey, piAddressVarOne,&m1,&n1,&lenStVarOne,&pStVarOne);
118                 if(sciErr.iErr)
119                 {
120                         printError(&sciErr, 0);
121                         return 0;
122                 }
123
124         }
125         else
126         {
127                 Scierror(999,_("%s: Memory allocation error.\n"),fname);
128                 return 0;
129         }
130
131         pStVarTwo = (char*)MALLOC(sizeof(char)*(lenStVarTwo + 1));
132         if (pStVarTwo)
133         {
134                 sciErr = getMatrixOfString(_piKey, piAddressVarTwo,&m2,&n2,&lenStVarTwo,&pStVarTwo);
135                 if(sciErr.iErr)
136                 {
137                         printError(&sciErr, 0);
138                         return 0;
139                 }
140
141         }
142         else
143         {
144                 FREE(pStVarOne);
145                 Scierror(999,_("%s: Memory allocation error.\n"),fname);
146                 return 0;
147         }
148
149         result = setenvc(pStVarOne, pStVarTwo);
150
151         FREE(pStVarOne); pStVarOne = NULL;
152         FREE(pStVarTwo); pStVarTwo = NULL;
153
154         m_out1 = 1; n_out1 = 1;
155         sciErr = createMatrixOfBoolean(_piKey, Rhs + 1, m_out1, n_out1, &result);
156         if(sciErr.iErr)
157         {
158                 printError(&sciErr, 0);
159                 return 0;
160         }
161
162         LhsVar(1) = Rhs + 1; 
163
164     PutLhsVar();
165         return 0;
166 }
167 /*--------------------------------------------------------------------------*/