diff options
author | David Sommerseth <davids@redhat.com> | 2011-01-06 13:44:25 +0100 |
---|---|---|
committer | David Sommerseth <davids@redhat.com> | 2011-01-06 13:44:25 +0100 |
commit | 10a2d8bd43934966dd842fd8f401f0d679d0d66a (patch) | |
tree | fbb0543f9685d3a51b5a209344186dae0b8b3cee | |
parent | 0d48bd736d5c9602574907500b5068138b155bf7 (diff) | |
download | python-dmidecode-10a2d8bd43934966dd842fd8f401f0d679d0d66a.tar.gz python-dmidecode-10a2d8bd43934966dd842fd8f401f0d679d0d66a.tar.xz python-dmidecode-10a2d8bd43934966dd842fd8f401f0d679d0d66a.zip |
Implemented dmixml_AddDMIstring()
This function can be used instead of dmi_string() and
dmixml_AddTextChild(). In those cases where dmi_string() returns
NULL, this situation is handled more gracefully. In addition of
also handling "not specified" situations better as well.
Signed-off-by: David Sommerseth <davids@redhat.com>
-rw-r--r-- | src/dmixml.c | 42 | ||||
-rw-r--r-- | src/dmixml.h | 1 |
2 files changed, 43 insertions, 0 deletions
diff --git a/src/dmixml.c b/src/dmixml.c index 3b05ad4..5fe0dd5 100644 --- a/src/dmixml.c +++ b/src/dmixml.c @@ -41,6 +41,7 @@ #include <libxml/xpath.h> #include <libxml/xmlstring.h> +#include "dmidecode.h" #include "dmilog.h" #include "dmixml.h" @@ -164,6 +165,47 @@ xmlNode *dmixml_AddTextChild(xmlNode *node, const char *tagname, const char *fmt } /** + * A variant of dmixml_AddTextChild() which will do dmi_string() decoding instead of a plain string. + * If the dmi_string() function returns NULL, it will instead add a XML node attribute 'badindex' + * @author David Sommerseth <davids@redhat.com> + * @param xmlNode* Pointer to the current node which will get the text child + * @param const char* Name of the new tag + * @param const struct dmi_header* Pointer to the DMI table header + * @param u8 DMI table index of the information to be extracted and used in the XML node + * @return xmlNode* Pointer to the new tag. On errors the return value will be NULL. On fatal + * errors and assert() call will be done. + */ +xmlNode *dmixml_AddDMIstring(xmlNode *node, const char *tagname, const struct dmi_header *dm, u8 s) { + xmlChar *tagname_s = NULL; + xmlNode *res = NULL; + const char *dmistr; + + if( (node == NULL) || (tagname == NULL) ) { + return NULL; + } + + tagname_s = xmlCharStrdup(tagname); + assert( tagname_s != NULL ); + + if(s == 0) { + res = xmlNewChild(node, NULL, tagname_s, NULL); + dmixml_AddAttribute(res, "not_specified", "1"); + return res; + } + + dmistr = dmi_string(dm, s); + if( dmistr == NULL ) { + res = xmlNewChild(node, NULL, tagname_s, NULL); + dmixml_AddAttribute(res, "badindex", "1"); + } else { + xmlChar *val_s = xmlCharStrdup(dmistr); + res = xmlNewTextChild(node, NULL, tagname_s, val_s); + free(val_s); + } + return res; +} + +/** * Adds a text node child to the given XML node. If input is NULL, the tag contents will be empty. * @author David Sommerseth <davids@redhat.com> * @param xmlNode* Pointer to the current node which will get the text child diff --git a/src/dmixml.h b/src/dmixml.h index 7bbbb76..cc2c303 100644 --- a/src/dmixml.h +++ b/src/dmixml.h @@ -34,6 +34,7 @@ xmlAttr *dmixml_AddAttribute(xmlNode *node, const char *atrname, const char *fmt, ...); xmlNode *dmixml_AddTextChild(xmlNode *node, const char *tagname, const char *fmt, ...); +xmlNode *dmixml_AddDMIstring(xmlNode *node, const char *tagname, const struct dmi_header *dm, u8 s); xmlNode *dmixml_AddTextContent(xmlNode *node, const char *fmt, ...); char *dmixml_GetAttrValue(xmlNode *node, const char *key); |