File: ncxml_xml2.c

package info (click to toggle)
netcdf-parallel 1%3A4.9.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 116,192 kB
  • sloc: ansic: 279,265; sh: 14,143; cpp: 5,971; yacc: 2,612; makefile: 2,075; lex: 1,218; javascript: 280; xml: 173; awk: 2
file content (163 lines) | stat: -rw-r--r-- 3,454 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* Copyright 2018-2018 University Corporation for Atmospheric  Research/Unidata. */

#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <libxml2/libxml/parser.h>
#include <libxml/tree.h>
#include "ncxml.h"

#ifndef nulldup
#define nulldup(s) ((s)?strdup(s):NULL)
#endif

static int ncxml_initialized = 0;

void
ncxml_initialize(void)
{
    ncxml_initialized = 1;
}

void
ncxml_finalize(void)
{
    ncxml_initialized = 0;
    xmlCleanupParser();
}

ncxml_doc_t
ncxml_parse(char* contents, size_t len)
{
    xmlDocPtr doc; /* the resulting document tree */
    doc = xmlReadMemory(contents, (int)len, "dap4.xml", NULL, 0);
    return (ncxml_doc_t)doc;
}

void
ncxml_free(ncxml_doc_t doc0)
{
    xmlDoc *doc = (xmlDoc*)doc0;
    xmlFreeDoc(doc);
 }

ncxml_t
ncxml_root(ncxml_doc_t doc0)
{
    xmlDoc *doc = (xmlDoc*)doc0;
    return (ncxml_t)xmlDocGetRootElement(doc);
}

const char*
ncxml_name(ncxml_t xml0)
{
    xmlNode* xml = (xmlNode*)xml0;    
    return (const char*)(xml?xml->name:NULL);
}

char*
ncxml_attr(ncxml_t xml0, const char* key)
{
    xmlNode* xml = (xmlNode*)xml0;
    xmlChar* value = NULL;
    char* s = NULL;

    value = xmlGetProp(xml,(const xmlChar*)key);
    s = nulldup((char*)value);
    xmlFree(value);
    return s;
}

/* First child by name */
ncxml_t
ncxml_child(ncxml_t xml0, const char* name)
{
    xmlNode* xml = (xmlNode*)xml0;
    xmlNode* child = NULL;

    for(child=xml->children;child; child = child->next) {
        if(child->type == XML_ELEMENT_NODE && strcmp((const char*)child->name,name)==0) 
	    return (ncxml_t)child;
    }
    return NULL;
}

ncxml_t
ncxml_next(ncxml_t xml0, const char* name)
{
    xmlNode* xml = (xmlNode*)xml0;
    xmlNode* next = NULL;

    for(next=xml->next;next; next = next->next) {
        if(next->type == XML_ELEMENT_NODE && strcmp((const char*)next->name,name)==0) 
	    return (ncxml_t)next;
    }
    return NULL;
}

char*
ncxml_text(ncxml_t xml0)
{
    xmlNode* xml = (xmlNode*)xml0;
    xmlChar* txt = NULL;
    char* s = NULL;
    if(xml == NULL) return NULL;
    txt = xmlNodeGetContent(xml);
    s = nulldup((char*)txt);
    xmlFree(txt);
    return s;
}

/* Nameless versions of child and next */
ncxml_t
ncxml_child_first(ncxml_t xml0)
{
    xmlNode* xml = (xmlNode*)xml0;
    xmlNode* child = NULL;

    if(xml == NULL) return NULL;
    for(child=xml->children;child; child = child->next) {
        if(child->type == XML_ELEMENT_NODE) return child;
    }
    return NULL;
}

ncxml_t
ncxml_child_next(ncxml_t xml0)
{
    xmlNode* xml = (xmlNode*)xml0;

    if(xml == NULL) return NULL;
    for(xml=xml->next;xml; xml = xml->next) {
        if(xml->type == XML_ELEMENT_NODE) return xml;
    }
    return NULL;
}

int
ncxml_attr_pairs(ncxml_t xml0, char*** pairsp)
{
    char** pairs = NULL;
    xmlNode* xml = (xmlNode*)xml0;
    xmlAttr* attr = NULL;
    int i;
    size_t count = 0;

    if(xml == NULL) return 0;
    /* First count */
    for(attr=xml->properties;attr;attr=attr->next) count++;
    /* Allocate */
    pairs = (char**)malloc(sizeof(char*)*((2*count)+1));
    if(pairs == NULL) return 0;
    /* Collect */
    for(i=0,attr=xml->properties;attr;i+=2,attr=attr->next) {
	xmlChar* value;
        pairs[i] = nulldup((char*)attr->name);
        value = xmlNodeListGetString(xml->doc, attr->children, 1);
	pairs[i+1] = nulldup((char*)value);
	xmlFree(value);
    }
    pairs[2*count] = NULL;
    if(pairsp) *pairsp = pairs;
    return 1;
}