void delete_distributed_derivation(DistributedDerivation *distributed_derivation) { if(distributed_derivation != NULL) { delete_derivation_array(distributed_derivation->derivation_array); delete_interface_array(distributed_derivation->interface_array); g_free(distributed_derivation); } }
DistributedDerivation *create_distributed_derivation(const gchar *distributed_derivation_file) { GPtrArray *derivation_array, *interface_array; DistributedDerivation *distributed_derivation; derivation_array = create_derivation_array(distributed_derivation_file); if(derivation_array == NULL) return NULL; interface_array = create_interface_array(distributed_derivation_file); if(interface_array == NULL) { delete_derivation_array(derivation_array); return NULL; } distributed_derivation = (DistributedDerivation*)g_malloc(sizeof(DistributedDerivation)); distributed_derivation->derivation_array = derivation_array; distributed_derivation->interface_array = interface_array; return distributed_derivation; }
GPtrArray *create_derivation_array(const gchar *distributed_derivation_file) { /* Declarations */ xmlDocPtr doc; xmlNodePtr node_root; xmlXPathObjectPtr result; GPtrArray *derivation_array = NULL; /* Parse the XML document */ if((doc = xmlParseFile(distributed_derivation_file)) == NULL) { g_printerr("Error with parsing the distributed derivation XML file!\n"); xmlCleanupParser(); return NULL; } /* Retrieve root element */ node_root = xmlDocGetRootElement(doc); if(node_root == NULL) { g_printerr("The distributed derivation XML file is empty!\n"); xmlFreeDoc(doc); xmlCleanupParser(); return NULL; } /* Query the mapping elements */ result = executeXPathQuery(doc, "/distributedderivation/build/mapping"); /* Iterate over all the mapping elements and add them to the array */ if(result) { xmlNodeSetPtr nodeset = result->nodesetval; unsigned int i; /* Create a derivation array */ derivation_array = g_ptr_array_new(); /* Iterate over all the mapping elements */ for(i = 0; i < nodeset->nodeNr; i++) { xmlNodePtr mapping_children = nodeset->nodeTab[i]->children; DerivationItem *item = (DerivationItem*)g_malloc(sizeof(DerivationItem)); gchar *derivation = NULL, *target = NULL; /* Iterate over all the mapping item children (derivation and target elements) */ while(mapping_children != NULL) { if(xmlStrcmp(mapping_children->name, (xmlChar*) "derivation") == 0) derivation = duplicate_node_text(mapping_children); else if(xmlStrcmp(mapping_children->name, (xmlChar*) "target") == 0) target = duplicate_node_text(mapping_children); mapping_children = mapping_children->next; } /* Added the mapping to the array */ item->derivation = derivation; item->target = target; item->result = NULL; if(item->derivation == NULL || item->target == NULL) { /* Check if all mandatory properties have been provided */ g_printerr("A mandatory property seems to be missing. Have you provided a correct\n"); g_printerr("distributed derivation file?\n"); delete_derivation_array(derivation_array); derivation_array = NULL; break; } else g_ptr_array_add(derivation_array, item); /* Add item to the array */ } xmlXPathFreeObject(result); } /* Cleanup */ xmlFreeDoc(doc); xmlCleanupParser(); /* Return the derivation array */ return derivation_array; }