src/model.c: now able to modify node's content

This commit is contained in:
Adrien Bourmault 2024-03-27 23:00:00 +02:00
parent 1ea9a356a9
commit 394d9644a0
Signed by: neox
GPG Key ID: 95F65F55F682A17A
1 changed files with 19 additions and 41 deletions

View File

@ -86,49 +86,27 @@ void model_populate_hashtable(xmlNode *node,
char* model_find_node_by_path(xmlNode *node, const char *path) { char* model_find_node_by_path(xmlNode *node, const char *path) {
return (xmlNodePtr)xmlHashLookup(hashTable, (const xmlChar *)path); return (xmlNodePtr)xmlHashLookup(hashTable, (const xmlChar *)path);
} }
/* -------------------------------------------------------------------------- */
static inline long model_get_node_long_attrib (xmlNodePtr node, char *id) /**
{ * Modifies the content of an XML node identified by its path in the hash table.
xmlAttr *attribute; *
xmlChar* value; * @param hashTable The hash table containing the XML nodes.
long ret_value; * @param path The path to the node to modify.
* @param newContent The new content to set for the node.
if (node && node->properties) { * @return 1 if the node was found and modified, 0 otherwise.
attribute = node->properties; */
while(attribute && attribute->name && attribute->children) { int model_modify_node(xmlHashTablePtr hashTable,
if (!xmlStrcmp(attribute->name, (const xmlChar *)id)) { const char *path,
value = xmlNodeListGetString(node->doc, attribute->children, 1); const char *newContent) {
ret_value = strtol((char *)value, NULL, 0); xmlNodePtr node = model_find_node_by_path(hashTable, path);
xmlFree(value); if (node) {
return ret_value; // Free the current content of the node, if any
} xmlNodeSetContent(node, (const xmlChar *)""); // Clear existing content
attribute = attribute->next; // Set new content
} xmlNodeSetContent(node, (const xmlChar *)newContent);
return 1; // Success
} }
return 0; return 0; // Node not found or unable to modify
}
static inline bool model_get_node_str_attrib (xmlNodePtr node,
char *id,
char *dest)
{
xmlAttr *attribute;
xmlChar* value;
if (node && node->properties) {
attribute = node->properties;
while(attribute && attribute->name && attribute->children) {
if (!xmlStrcmp(attribute->name, (const xmlChar *)id)) {
value = xmlNodeListGetString(node->doc, attribute->children, 1);
strcpy(dest, value);
xmlFree(value);
return true;
}
attribute = attribute->next;
}
}
return false;
} }
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */