629d3dde4f001dc1400174a84831272fbd4f4b5f
[scilab.git] / scilab / modules / windows_tools / sci_gateway / c / sci_winqueryreg.c
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) INRIA - Allan CORNET
4  * Copyright (C) 2011 - DIGITEO - Allan CORNET
5  *
6  * This file must be used under the terms of the CeCILL.
7  * This source file is licensed as described in the file COPYING, which
8  * you should have received as part of this distribution.  The terms
9  * are also available at
10  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
11  *
12  */
13 /*--------------------------------------------------------------------------*/
14 #include "gw_windows_tools.h"
15 #include "api_scilab.h"
16 #include "stack-c.h"
17 #include "MALLOC.h"
18 #include "registry.h"
19 #include "Scierror.h"
20 #include "localization.h"
21 #include "PATH_MAX.h"
22 #include "freeArrayOfString.h"
23 /*--------------------------------------------------------------------------*/
24 int sci_winqueryreg(char *fname,int *_piKey)
25 {
26     SciErr sciErr;
27     int *piAddressVarOne = NULL;
28     int *piAddressVarTwo = NULL;
29     int *piAddressVarThree = NULL;
30
31     char *pStrParamOne = NULL;
32     char *pStrParamTwo = NULL;
33     char *pStrParamThree = NULL;
34
35     char *pStrOutput = NULL;
36     int iOutput = 0;
37
38     Rhs = Max(0, Rhs);
39     CheckRhs(2, 3);
40     CheckLhs(0, 1);
41
42     if (Rhs == 3)
43     {
44         sciErr = getVarAddressFromPosition(_piKey, 3, &piAddressVarThree);
45         if(sciErr.iErr)
46         {
47             printError(&sciErr, 0);
48             return 0;
49         }
50
51         if (!isStringType(_piKey, piAddressVarThree))
52         {
53             Scierror(999,_("%s: Wrong type for input argument #%d: String expected.\n"), fname, 3);
54             return 0;
55         }
56
57         if (!isScalar(_piKey, piAddressVarThree))
58         {
59             Scierror(999,_("%s: Wrong size for input argument #%d: String expected.\n"), fname, 3);
60             return 0;
61         }
62
63         if (getAllocatedSingleString(_piKey, piAddressVarThree, &pStrParamThree) != 0)
64         {
65             Scierror(999,_("%s: Memory allocation error.\n"), fname);
66             return 0;
67         }
68     }
69
70     sciErr = getVarAddressFromPosition(_piKey, 1, &piAddressVarOne);
71     if(sciErr.iErr)
72     {
73         printError(&sciErr, 0);
74         return 0;
75     }
76
77     if (!isStringType(_piKey, piAddressVarOne))
78     {
79         Scierror(999,_("%s: Wrong type for input argument #%d: String expected.\n"), fname, 1);
80         return 0;
81     }
82
83     if (!isScalar(_piKey, piAddressVarOne))
84     {
85         Scierror(999,_("%s: Wrong size for input argument #%d: String expected.\n"), fname, 1);
86         return 0;
87     }
88
89     sciErr = getVarAddressFromPosition(_piKey, 2, &piAddressVarTwo);
90     if(sciErr.iErr)
91     {
92         printError(&sciErr, 0);
93         return 0;
94     }
95
96     if (!isStringType(_piKey, piAddressVarTwo))
97     {
98         Scierror(999,_("%s: Wrong type for input argument #%d: String expected.\n"), fname, 2);
99         return 0;
100     }
101
102     if (!isScalar(_piKey, piAddressVarTwo))
103     {
104         Scierror(999,_("%s: Wrong size for input argument #%d: String expected.\n"), fname, 2);
105         return 0;
106     }
107
108     if (getAllocatedSingleString(_piKey, piAddressVarTwo, &pStrParamTwo) != 0)
109     {
110         if (pStrParamThree)
111         {
112             freeAllocatedSingleString(pStrParamThree);
113             pStrParamThree = NULL;
114         }
115         Scierror(999,_("%s: Memory allocation error.\n"), fname);
116         return 0;
117     }
118
119     if (getAllocatedSingleString(_piKey, piAddressVarOne, &pStrParamOne) != 0)
120     {
121         if (pStrParamThree)
122         {
123             freeAllocatedSingleString(pStrParamThree);
124             pStrParamThree = NULL;
125         }
126
127         if (pStrParamTwo)
128         {
129             freeAllocatedSingleString(pStrParamTwo);
130             pStrParamTwo = NULL;
131         }
132
133         Scierror(999,_("%s: Memory allocation error.\n"), fname);
134         return 0;
135     }
136
137     if (Rhs == 3)
138     {
139         if (strcmp(pStrParamOne, "name") == 0)
140         {
141             int NumbersElm = 0;
142             WindowsQueryRegistryNumberOfElementsInList(pStrParamTwo, pStrParamThree, &NumbersElm);
143             if (NumbersElm)
144             {
145                 #define MAX_ELMT_REGLIST 255
146                 char **ListKeysName = NULL;
147                 int i = 0;
148
149                 if (NumbersElm > MAX_ELMT_REGLIST) NumbersElm = MAX_ELMT_REGLIST;
150                 ListKeysName = (char **)MALLOC(sizeof(char*) * NumbersElm);
151                 for (i = 0; i < NumbersElm; i++)
152                 {
153                     ListKeysName[i] = NULL;
154                 }
155
156                 if (WindowsQueryRegistryList(pStrParamTwo, pStrParamThree, NumbersElm, ListKeysName))
157                 {
158                     int nOne = 1;
159                     sciErr = createMatrixOfString(_piKey, Rhs + 1, NumbersElm, nOne, ListKeysName);
160                     if (sciErr.iErr)
161                     {
162                         printError(&sciErr, 0);
163                     }
164                     else
165                     {
166                         LhsVar(1) = Rhs + 1;
167                         PutLhsVar();
168                     }
169                 }
170                 else
171                 {
172                     Scierror(999,_("%s: Cannot open Windows registry.\n"), fname);
173                 }
174                 freeArrayOfString(ListKeysName, NumbersElm);
175             }
176             else
177             {
178                 createEmptyMatrix(_piKey, Rhs + 1);
179                 LhsVar(1) = Rhs + 1;
180                 PutLhsVar();
181             }
182
183             if (pStrParamThree)
184             {
185                 freeAllocatedSingleString(pStrParamThree);
186                 pStrParamThree = NULL;
187             }
188
189             if (pStrParamTwo)
190             {
191                 freeAllocatedSingleString(pStrParamTwo);
192                 pStrParamTwo = NULL;
193             }
194
195             if (pStrParamOne)
196             {
197                 freeAllocatedSingleString(pStrParamOne);
198                 pStrParamOne = NULL;
199             }
200             return 0;
201         }
202     }
203
204     pStrOutput = (char*)MALLOC(PATH_MAX * sizeof(char));
205     if (pStrOutput)
206     {
207         BOOL OuputIsREG_SZ = FALSE;
208         BOOL TestWinQuery = WindowsQueryRegistry(pStrParamOne, pStrParamTwo, pStrParamThree, pStrOutput, &iOutput, &OuputIsREG_SZ);
209         if ( TestWinQuery )
210         {
211             if (OuputIsREG_SZ)
212             {
213                 createSingleString(_piKey, Rhs + 1, pStrOutput);
214             }
215             else
216             {
217                 createScalarDouble(_piKey, Rhs + 1, (double)iOutput);
218             }
219
220             LhsVar(1) = Rhs+1;
221             PutLhsVar();
222         }
223         else
224         {
225             Scierror(999,_("%s: Cannot query value of this type.\n"),fname);
226         }
227
228         FREE( pStrOutput);
229         pStrOutput = NULL;
230     }
231     else
232     {
233         Scierror(999,_("%s: Memory allocation error.\n"), fname);
234     }
235
236     if (pStrParamThree)
237     {
238         freeAllocatedSingleString(pStrParamThree);
239         pStrParamThree = NULL;
240     }
241
242     if (pStrParamTwo)
243     {
244         freeAllocatedSingleString(pStrParamTwo);
245         pStrParamTwo = NULL;
246     }
247
248     if (pStrParamOne)
249     {
250         freeAllocatedSingleString(pStrParamOne);
251         pStrParamOne = NULL;
252     }
253
254     return 0;
255
256 }
257 /*--------------------------------------------------------------------------*/