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) {
return (xmlNodePtr)xmlHashLookup(hashTable, (const xmlChar *)path);
}
/* -------------------------------------------------------------------------- */
static inline long model_get_node_long_attrib (xmlNodePtr node, char *id)
{
xmlAttr *attribute;
xmlChar* value;
long ret_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);
ret_value = strtol((char *)value, NULL, 0);
xmlFree(value);
return ret_value;
}
attribute = attribute->next;
}
/**
* Modifies the content of an XML node identified by its path in the hash table.
*
* @param hashTable The hash table containing the XML nodes.
* @param path The path to the node to modify.
* @param newContent The new content to set for the node.
* @return 1 if the node was found and modified, 0 otherwise.
*/
int model_modify_node(xmlHashTablePtr hashTable,
const char *path,
const char *newContent) {
xmlNodePtr node = model_find_node_by_path(hashTable, path);
if (node) {
// Free the current content of the node, if any
xmlNodeSetContent(node, (const xmlChar *)""); // Clear existing content
// Set new content
xmlNodeSetContent(node, (const xmlChar *)newContent);
return 1; // Success
}
return 0;
}
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;
return 0; // Node not found or unable to modify
}
/* -------------------------------------------------------------------------- */