|
| 1 | +/* |
| 2 | + * This program is free software; you can redistribute it and/or modify |
| 3 | + * it under the terms of the GNU General Public License as published by |
| 4 | + * the Free Software Foundation; either version 2 of the License, or |
| 5 | + * (at your option) any later version. |
| 6 | + * |
| 7 | + * This program is distributed in the hope that it will be useful, |
| 8 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | + * GNU General Public License for more details. |
| 11 | + * |
| 12 | + * You should have received a copy of the GNU General Public License |
| 13 | + * along with this program; if not, write to the Free Software |
| 14 | + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 15 | + * |
| 16 | + * For the avoidance of doubt the "preferred form" of this code is one which |
| 17 | + * is in an open unpatent encumbered format. Where cryptographic key signing |
| 18 | + * forms part of the process of creating an executable the information |
| 19 | + * including keys needed to generate an equivalently functional executable |
| 20 | + * are deemed to be part of the source code. |
| 21 | + */ |
| 22 | + |
| 23 | +#include "demo.h" |
| 24 | + |
| 25 | +xmlNode *gen_nodes(const char *entry ) { |
| 26 | + xmlNode *c_xmlNode_root = NULL; |
| 27 | + xmlNode *c_xmlNode_tag = NULL; |
| 28 | + |
| 29 | + // Prepare a root node |
| 30 | + c_xmlNode_root = xmlNewNode(NULL, (xmlChar *) "dmixml_demo"); |
| 31 | + assert( c_xmlNode_root != NULL ); |
| 32 | + |
| 33 | + dmixml_AddAttribute(c_xmlNode_root, "entrypoint", "%s", entry); |
| 34 | + |
| 35 | + // Populate XML |
| 36 | + dmixml_AddTextChild(c_xmlNode_root, "Test", "Yes, just testing"); |
| 37 | + |
| 38 | + c_xmlNode_tag = dmixml_AddTextChild(c_xmlNode_root, "tag1", "Another test"); |
| 39 | + dmixml_AddAttribute(c_xmlNode_tag, "TestTagID", "%i", 1); |
| 40 | + |
| 41 | + c_xmlNode_tag = c_xmlNode_root; |
| 42 | + int i; |
| 43 | + for(i = 0; i <= 3; ++i) { |
| 44 | + c_xmlNode_tag = xmlNewChild(c_xmlNode_tag, NULL, (xmlChar *) "subtag", NULL); |
| 45 | + dmixml_AddAttribute(c_xmlNode_tag, "SubLevel", "%i", i); |
| 46 | + } |
| 47 | + dmixml_AddTextContent(c_xmlNode_tag, "%s - Adding data to the tag at sublevel %i", "TEST", i-1); |
| 48 | + |
| 49 | + return c_xmlNode_root; |
| 50 | +} |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | +PyObject* demo_dump_doc() { |
| 56 | + PyObject *py_xmlDoc = NULL; |
| 57 | + xmlDoc *c_xmlDoc = NULL; |
| 58 | + |
| 59 | + // Create an XML document |
| 60 | + c_xmlDoc = xmlNewDoc((xmlChar *) "1.0"); |
| 61 | + assert( c_xmlDoc != NULL ); |
| 62 | + |
| 63 | + // Generate XML nodes and assign the root node to the document |
| 64 | + xmlDocSetRootElement( c_xmlDoc, gen_nodes("demo_dump_doc") ); |
| 65 | + |
| 66 | + py_xmlDoc = libxml_xmlDocPtrWrap((xmlDocPtr) c_xmlDoc); |
| 67 | + Py_INCREF(py_xmlDoc); |
| 68 | + |
| 69 | + return py_xmlDoc; |
| 70 | +} |
| 71 | + |
| 72 | +PyObject* demo_dump_node() { |
| 73 | + PyObject *py_xmlNode = NULL; |
| 74 | + xmlNode *nodes = NULL; |
| 75 | + |
| 76 | + nodes = gen_nodes("demo_dump_node"); |
| 77 | + py_xmlNode = libxml_xmlNodePtrWrap((xmlNodePtr) nodes); |
| 78 | + Py_INCREF(py_xmlNode); |
| 79 | + |
| 80 | + return py_xmlNode; |
| 81 | +} |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | +static PyMethodDef DemoMethods[] = { |
| 86 | + { "dump_doc", demo_dump_doc, METH_NOARGS, (char *)"Return an XML document" }, |
| 87 | + { "dump_node", demo_dump_node, METH_NOARGS, (char *)"Retuen an XML node" }, |
| 88 | + { NULL, NULL, 0, NULL } |
| 89 | +}; |
| 90 | + |
| 91 | +PyMODINIT_FUNC initdemomodule(void) { |
| 92 | + PyObject *module = |
| 93 | + Py_InitModule3((char *)"demomodule", DemoMethods, |
| 94 | + "LibXML2 DMIDecode Proof-of-Concept Python Module"); |
| 95 | + |
| 96 | + PyObject *version = PyString_FromString("0.10"); |
| 97 | + Py_INCREF(version); |
| 98 | + PyModule_AddObject(module, "version", version); |
| 99 | +} |
0 commit comments