D
deadpickle
Im trying to read an xml file. What happens is that I have a FORTRAN
program that calls a C library that uses libxml2 to read an xml file.
The passed array (from FORTRAN) needs to be passed to several
functions within the C program before being returned to the FORTRAN
program. My question is how do I pass the array within C and how do I
return the array to FORTRAN.
The code:
#include <stdio.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xmlstring.h>
#include <libxml/xpath.h>
#ifdef LIBXML_TREE_ENABLED
static void get_item_values(xmlNode * b_node, int *max, double
ellipseinfo[5][*max], int mark) {
xmlNode *sub_node = NULL;
xmlChar *attrname;
int cluster = 0;
for (sub_node = b_node; sub_node; sub_node = sub_node->next) {
if (sub_node->type == XML_ELEMENT_NODE) {
if ( !xmlStrcmp(sub_node->name, "item") ) {
attrname = xmlGetNoNsProp(sub_node,"value");
double value = xmlXPathCastStringToNumber(attrname);
ellipseinfo[mark][cluster] = value;
cluster++;
}
}
}
}
static void print_element_names(xmlNode * a_node, double *ellipseinfo)
{
xmlNode *cur_node = NULL;
xmlChar *attrname;
for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
if (cur_node->type == XML_ELEMENT_NODE) {
if ( !xmlStrcmp(cur_node->name, "datacolumn") ) {
attrname = xmlGetNoNsProp(cur_node, (const xmlChar *) "name");
if (xmlStrEqual(attrname, (const xmlChar *) "Latitude")) {
int mark = 0;
get_item_values(cur_node->children, max, ellipseinfo,mark);
}
else if (xmlStrEqual(attrname, (const xmlChar *) "Longitude")) {
int mark = 1;
get_item_values(cur_node->children, max, ellipseinfo[5]
[*max],mark);
}
if (xmlStrEqual(attrname, (const xmlChar *) "LatRadius")) {
int mark = 2;
get_item_values(cur_node->children, max, ellipseinfo[5]
[*max],mark);
}
if (xmlStrEqual(attrname, (const xmlChar *) "LonRadius")) {
int mark = 3;
get_item_values(cur_node->children, max, ellipseinfo[5]
[*max],mark);
}
if (xmlStrEqual(attrname, (const xmlChar *) "Orientation")) {
int mark = 4;
get_item_values(cur_node->children, max, ellipseinfo[5]
[*max],mark);
}
}
}
print_element_names(cur_node->children,max,ellipseinfo[5][*max]);
}
}
int
read_xml(int *max, char *filename, double ellipseinfo[5][*max])
{
xmlDoc *doc = NULL;
xmlNode *root_element = NULL;
LIBXML_TEST_VERSION
doc = xmlReadFile(filename, NULL, 0);
if (doc == NULL) {
printf("error: could not parse file %s\n", filename);
}
root_element = xmlDocGetRootElement(doc);
print_element_names(root_element, &ellipseinfo[5][max]);
xmlFreeDoc(doc);
xmlCleanupParser();
return;
}
#else
int main(void) {
fprintf(stderr, "Tree support not compiled in\n");
exit(1);
}
#endif
program that calls a C library that uses libxml2 to read an xml file.
The passed array (from FORTRAN) needs to be passed to several
functions within the C program before being returned to the FORTRAN
program. My question is how do I pass the array within C and how do I
return the array to FORTRAN.
The code:
#include <stdio.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xmlstring.h>
#include <libxml/xpath.h>
#ifdef LIBXML_TREE_ENABLED
static void get_item_values(xmlNode * b_node, int *max, double
ellipseinfo[5][*max], int mark) {
xmlNode *sub_node = NULL;
xmlChar *attrname;
int cluster = 0;
for (sub_node = b_node; sub_node; sub_node = sub_node->next) {
if (sub_node->type == XML_ELEMENT_NODE) {
if ( !xmlStrcmp(sub_node->name, "item") ) {
attrname = xmlGetNoNsProp(sub_node,"value");
double value = xmlXPathCastStringToNumber(attrname);
ellipseinfo[mark][cluster] = value;
cluster++;
}
}
}
}
static void print_element_names(xmlNode * a_node, double *ellipseinfo)
{
xmlNode *cur_node = NULL;
xmlChar *attrname;
for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
if (cur_node->type == XML_ELEMENT_NODE) {
if ( !xmlStrcmp(cur_node->name, "datacolumn") ) {
attrname = xmlGetNoNsProp(cur_node, (const xmlChar *) "name");
if (xmlStrEqual(attrname, (const xmlChar *) "Latitude")) {
int mark = 0;
get_item_values(cur_node->children, max, ellipseinfo,mark);
}
else if (xmlStrEqual(attrname, (const xmlChar *) "Longitude")) {
int mark = 1;
get_item_values(cur_node->children, max, ellipseinfo[5]
[*max],mark);
}
if (xmlStrEqual(attrname, (const xmlChar *) "LatRadius")) {
int mark = 2;
get_item_values(cur_node->children, max, ellipseinfo[5]
[*max],mark);
}
if (xmlStrEqual(attrname, (const xmlChar *) "LonRadius")) {
int mark = 3;
get_item_values(cur_node->children, max, ellipseinfo[5]
[*max],mark);
}
if (xmlStrEqual(attrname, (const xmlChar *) "Orientation")) {
int mark = 4;
get_item_values(cur_node->children, max, ellipseinfo[5]
[*max],mark);
}
}
}
print_element_names(cur_node->children,max,ellipseinfo[5][*max]);
}
}
int
read_xml(int *max, char *filename, double ellipseinfo[5][*max])
{
xmlDoc *doc = NULL;
xmlNode *root_element = NULL;
LIBXML_TEST_VERSION
doc = xmlReadFile(filename, NULL, 0);
if (doc == NULL) {
printf("error: could not parse file %s\n", filename);
}
root_element = xmlDocGetRootElement(doc);
print_element_names(root_element, &ellipseinfo[5][max]);
xmlFreeDoc(doc);
xmlCleanupParser();
return;
}
#else
int main(void) {
fprintf(stderr, "Tree support not compiled in\n");
exit(1);
}
#endif