YAZ 5.35.1
xml_get.c
Go to the documentation of this file.
1/* This file is part of the YAZ toolkit.
2 * Copyright (C) Index Data
3 * See the file LICENSE for details.
4 */
9#if HAVE_CONFIG_H
10#include <config.h>
11#endif
12
13#include <string.h>
14#include <stdarg.h>
15#include <yaz/xmalloc.h>
16#include <yaz/xml_get.h>
17#if YAZ_HAVE_XML2
18
19const char *yaz_xml_get_prop(const xmlNode *n, const char *fmt, ...)
20{
21 int no = 0;
22 va_list ap;
23 const char *cp;
24 struct _xmlAttr *attr;
25
26 va_start(ap, fmt);
27 for (cp = fmt; *cp; cp++)
28 if (*cp == '%')
29 no++;
30 if (no > 0)
31 {
32 const char ***ar = xmalloc(sizeof(*ar) * no);
33 int i;
34 for (i = 0; i < no; i++)
35 {
36 const char **s = va_arg(ap, const char **);
37 ar[i] = s;
38 }
39 va_end(ap);
40 for (attr = n->properties; attr; attr = attr->next)
41 {
42 const char *cp1 = fmt;
43 for (i = 0; *cp1; i++)
44 {
45 const char *cp2 = cp1;
46 size_t l;
47 while (*cp2 != '\0' && *cp2 != '%')
48 cp2++;
49 if (*cp2 != '\0')
50 { /* no % following, break out (bad fmt really) */
51 cp1 = cp2;
52 break;
53 }
54 l = cp2 - cp1;
55 if (l > 0 && strlen((const char *) attr->name) == l &&
56 !memcmp((const char *) attr->name, cp1, l))
57 break;
58 cp1 = 1 + cp2;
59 if (*cp1)
60 cp1++; /* skip char following % */
61 }
62 if (!*cp1)
63 {
64 /* attribute not listed in fmt: return first unknown one */
65 xfree(ar);
66 return (const char *) attr->name;
67 }
68 *ar[i] = (const char *) attr->children->content;
69 }
70 xfree(ar);
71 }
72 else
73 {
74 va_end(ap);
75 for (attr = n->properties; attr; attr = attr->next)
76 {
77 if (!strcmp((const char *) attr->name, fmt))
78 return (const char *) attr->children->content;
79 }
80 }
81 return 0; /* failure for simple mode; successful for %mode */
82}
83
84#endif
85
86/*
87 * Local variables:
88 * c-basic-offset: 4
89 * c-file-style: "Stroustrup"
90 * indent-tabs-mode: nil
91 * End:
92 * vim: shiftwidth=4 tabstop=8 expandtab
93 */
94
Header for memory handling functions.
#define xfree(x)
utility macro which calls xfree_f
Definition xmalloc.h:53
#define xmalloc(x)
utility macro which calls malloc_f
Definition xmalloc.h:49
const char * yaz_xml_get_prop(const xmlNode *n, const char *fmt,...)
Definition xml_get.c:19
XML node getter/creation utilities.