1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2012 - Scilab Enterprises - Calixte DENIZET
4  *
5  * Copyright (C) 2012 - 2016 - Scilab Enterprises
6  *
7  * This file is hereby licensed under the terms of the GNU GPL v2.0,
8  * pursuant to article 5.3.4 of the CeCILL v.2.1.
9  * This file was originally licensed under the terms of the CeCILL v2.1,
10  * and continues to be available under such terms.
11  * For more information, see the COPYING file which you should have received
12  * along with this program.
13  *
14  */
15 
16 #ifndef __XMLLIST_INSERT_HPP__
17 #define __XMLLIST_INSERT_HPP__
18 
19 #include "XMLRhsValue.hxx"
20 
21 extern "C"
22 {
23 #include <stdio.h>
24 #include "gw_xml.h"
25 #include "Scierror.h"
26 #include "api_scilab.h"
27 #include "xml_mlist.h"
28 #include "localization.h"
29 }
30 
31 using namespace org_modules_xml;
32 
33 /*--------------------------------------------------------------------------*/
34 
35 /**
36  * Function to handle insertion in XMLNodeList
37  * @param fname the function name
38  * @param fname_len the function name length
39  */
40 template <class T>
sci_XMLList_insertion(char * fname,void * pvApiCtx)41 int sci_XMLList_insertion(char * fname, void* pvApiCtx)
42 {
43     XMLNodeList * a;
44     T * b;
45     int lhsid;
46     bool success;
47     double index;
48     SciErr err;
49     int * indexaddr = 0;
50     int * rhsaddr = 0;
51     int * lhsaddr = 0;
52 
53     CheckLhs(0, 1);
54     CheckRhs(3, 3);
55 
56     err = getVarAddressFromPosition(pvApiCtx, 1, &indexaddr);
57     if (err.iErr)
58     {
59         printError(&err, 0);
60         Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
61         return 0;
62     }
63 
64     if (!isDoubleType(pvApiCtx, indexaddr))
65     {
66         Scierror(999, gettext("%s: Wrong type for input argument #%i: A double expected.\n"), fname, 1);
67         return 0;
68     }
69 
70     getScalarDouble(pvApiCtx, indexaddr, &index);
71 
72     err = getVarAddressFromPosition(pvApiCtx, 2, &rhsaddr);
73     if (err.iErr)
74     {
75         printError(&err, 0);
76         Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
77         return 0;
78     }
79 
80     err = getVarAddressFromPosition(pvApiCtx, 3, &lhsaddr);
81     if (err.iErr)
82     {
83         printError(&err, 0);
84         Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3);
85         return 0;
86     }
87 
88     lhsid = getXMLObjectId(lhsaddr, pvApiCtx);
89     a = XMLObject::getFromId<XMLNodeList>(lhsid);
90     if (!a)
91     {
92         Scierror(999, gettext("%s: XML object does not exist.\n"), fname);
93         return 0;
94     }
95 
96     success = XMLRhsValue::get(fname, rhsaddr, &b, pvApiCtx);
97     if (!success)
98     {
99         Scierror(999, gettext("%s: Error in getting rhs argument.\n"), fname);
100         return 0;
101     }
102 
103     a->setElementAtPosition(index, *b);
104 
105     if (typeid(T) == typeid(std::string))
106     {
107         delete b;
108     }
109 
110     if (a->createOnStack(Rhs + 1, pvApiCtx))
111     {
112         LhsVar(1) = Rhs + 1;
113     }
114     else
115     {
116         LhsVar(1) = 0;
117     }
118 
119     PutLhsVar();
120 
121     return 0;
122 }
123 
124 #endif
125