2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2012 - Scilab Enterprises - Calixte DENIZET
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
15 #include "XMLObject.hxx"
16 #include "XMLDocument.hxx"
17 #include "XMLElement.hxx"
19 #include "XMLAttr.hxx"
20 #include "XMLNodeList.hxx"
28 #include "api_scilab.h"
29 #include "xml_mlist.h"
30 #include "xml_constants.h"
31 #include "localization.h"
33 #include "strdup_windows.h"
37 using namespace org_modules_xml;
39 /*--------------------------------------------------------------------------*/
42 * Creates a string on stack
43 * @param fname the function name
44 * @param str the string to put
45 * @param pos the stack position
46 * @return 1 if all is ok, else 0
48 int createStringOnStack(char * fname, const char * str, int pos, void* pvApiCtx)
57 if (strchr(str, '\n'))
59 char * tok = strdup(str);
61 std::vector<char *> vector = std::vector<char *>();
63 tok = strtok(tok, "\n");
66 vector.push_back(tok);
67 tok = strtok(0, "\n");
72 err = createMatrixOfString(pvApiCtx, pos, (int)vector.size(), 1, const_cast<const char * const *>(&(vector[0])));
76 err = createMatrixOfDouble(pvApiCtx, pos, 0, 0, 0);
83 err = createMatrixOfString(pvApiCtx, pos, 1, 1, const_cast<const char * const *>(&str));
94 /*--------------------------------------------------------------------------*/
96 * Creates a new variable on stack according to the requested field
97 * @param fname the function name
98 * @param doc the document
99 * @param field the field name
100 * @param pos the stack position
101 * @return 1 if all is ok, else 0
103 int createVariableOnStack(char * fname, org_modules_xml::XMLDocument & doc, const char * field, int pos, void* pvApiCtx)
105 if (!strcmp("root", field))
107 const XMLElement * e = doc.getRoot();
110 Scierror(999, gettext("%s: No root element.\n"), fname, field);
113 return e->createOnStack(pos, pvApiCtx);
115 else if (!strcmp("url", field))
117 return createStringOnStack(fname, doc.getDocumentURL(), pos, pvApiCtx);
121 Scierror(999, gettext("%s: Unknown field: %s\n"), fname, field);
125 /*--------------------------------------------------------------------------*/
128 * Creates a new variable on stack according to the requested field
129 * @param fname the function name
130 * @param elem the element
131 * @param field the field name
132 * @param pos the stack position
133 * @return 1 if all is ok, else 0
135 int createVariableOnStack(char * fname, XMLElement & elem, const char * field, int pos, void* pvApiCtx)
137 if (!strcmp("name", field))
139 return createStringOnStack(fname, elem.getNodeName(), pos, pvApiCtx);
141 else if (!strcmp("namespace", field))
143 const XMLNs * ns = elem.getNodeNameSpace();
146 return ns->createOnStack(pos, pvApiCtx);
150 createMatrixOfDouble(pvApiCtx, pos, 0, 0, 0);
154 else if (!strcmp("content", field))
156 const char * content = elem.getNodeContent();
157 int ret = createStringOnStack(fname, content, pos, pvApiCtx);
158 xmlFree(const_cast<char *>(content));
161 else if (!strcmp("type", field))
163 return createStringOnStack(fname, nodes_type[elem.getNodeType() - 1], pos, pvApiCtx);
165 else if (!strcmp("parent", field))
167 const XMLElement * parent = elem.getParentElement();
170 return parent->createOnStack(pos, pvApiCtx);
174 createMatrixOfDouble(pvApiCtx, pos, 0, 0, 0);
178 else if (!strcmp("attributes", field))
180 return elem.getAttributes()->createOnStack(pos, pvApiCtx);
182 else if (!strcmp("children", field))
184 return elem.getChildren()->createOnStack(pos, pvApiCtx);
186 else if (!strcmp("line", field))
188 double line = (double)elem.getDefinitionLine();
189 SciErr err = createMatrixOfDouble(pvApiCtx, pos, 1, 1, &line);
193 Scierror(999,_("%s: Memory allocation error.\n"), fname);
201 Scierror(999, gettext("%s: Unknown field: %s\n"), fname, field);
205 /*--------------------------------------------------------------------------*/
208 * Creates a new variable on stack according to the requested field
209 * @param fname the function name
210 * @param ns the namespace
211 * @param field the field name
212 * @param pos the stack position
213 * @return 1 if all is ok, else 0
215 int createVariableOnStack(char * fname, XMLNs & ns, const char * field, int pos, void* pvApiCtx)
217 if (!strcmp("href", field))
219 return createStringOnStack(fname, ns.getHref(), pos, pvApiCtx);
221 else if (!strcmp("prefix", field))
223 return createStringOnStack(fname, ns.getPrefix(), pos, pvApiCtx);
227 Scierror(999, gettext("%s: Unknown field: %s\n"), fname, field);
231 /*--------------------------------------------------------------------------*/
234 * Function to handle extraction in different XMLObjects
235 * @param fname the function name
236 * @param fname_len the function name length
239 int sci_extraction(char * fname, void *pvApiCtx)
252 err = getVarAddressFromPosition(pvApiCtx, 1, &fieldaddr);
259 if (!isStringType(pvApiCtx, fieldaddr))
261 Scierror(999, gettext("%s: Wrong type for input argument #%i: A string expected.\n"), fname, 1);
265 err = getVarAddressFromPosition(pvApiCtx, 2, &mlistaddr);
269 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
273 if (getAllocatedSingleString(pvApiCtx, fieldaddr, &field) != 0)
275 Scierror(999, _("%s: No more memory.\n"), fname);
278 id = getXMLObjectId(mlistaddr, pvApiCtx);
280 t = XMLObject::getFromId<T>(id);
283 freeAllocatedSingleString(field);
284 Scierror(999, gettext("%s: XML object does not exist.\n"), fname);
288 ret = createVariableOnStack(fname, *t, const_cast<char *>(field), Rhs + 1, pvApiCtx);
289 freeAllocatedSingleString(field);