Add yaz_parse_facet_list with helper functions
[yaz-moved-to-github.git] / src / odr_util.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2010 Index Data
3  * See the file LICENSE for details.
4  */
5 /**
6  * \file odr_util.c
7  * \brief Implements various ODR utilities
8  */
9 #if HAVE_CONFIG_H
10 #include <config.h>
11 #endif
12
13 #include <stdlib.h>
14 #include <string.h>
15 #include <ctype.h>
16 #include "odr-priv.h"
17 #include <yaz/oid_util.h>
18
19 void odr_prname(ODR o, const char *name)
20 {
21     if (name)
22         odr_printf(o, "%*s%s ", o->op->indent*4, "", name);
23     else
24         odr_printf(o, "%*s", o->op->indent*4, "");
25 }
26
27 int odp_more_chunks(ODR o, const unsigned char *base, int len)
28 {
29     if (!len)
30         return 0;
31     if (len < 0) /* indefinite length */
32     {
33         if (*o->bp == 0 && *(o->bp + 1) == 0)
34         {
35             o->bp += 2;
36             return 0;
37         }
38         else
39             return 1;
40     }
41     else
42         return o->bp - base < len;
43 }
44
45 Odr_oid *odr_oiddup_nmem(NMEM nmem, const Odr_oid *o)
46 {
47     Odr_oid *r;
48
49     if (!o)
50         return 0;
51     if (!(r = (Odr_oid *)
52           nmem_malloc(nmem, (oid_oidlen(o) + 1) * sizeof(Odr_oid))))
53         return 0;
54     oid_oidcpy(r, o);
55     return r;
56 }
57
58 Odr_oid *odr_oiddup(ODR odr, const Odr_oid *o)
59 {
60     return odr_oiddup_nmem(odr_getmem(odr), o);
61 }
62
63 Odr_oid *odr_getoidbystr_nmem(NMEM nmem, const char *str)
64 {
65     Odr_oid oid[OID_SIZE];
66     Odr_oid *ret;
67
68     if (oid_dotstring_to_oid(str, oid))
69         return 0;
70     ret = (Odr_oid *)nmem_malloc(nmem, sizeof(*ret)*(oid_oidlen(oid) + 1));
71     oid_oidcpy(ret, oid);
72     return ret;
73 }
74
75 Odr_oid *odr_getoidbystr(ODR o, const char *str)
76 {
77     return odr_getoidbystr_nmem(odr_getmem(o), str);
78 }
79
80 int odr_missing(ODR o, int opt, const char *name)
81 {
82     if (o->error)
83         return 0;
84     if (!opt)
85     {
86         odr_seterror(o, OREQUIRED, 53);
87         odr_setelement(o, name);
88     }
89     return opt;
90 }
91
92 /*
93  * Reallocate the buffer `old', using the ODR memory pool `o' to be
94  * big enough to hold its existing value (if any) plus `prefix' (if
95  * any) and a separator character.  Copy `prefix', a forward slash and
96  * the old value into the new area and return its address.  Can be
97  * used as follows:
98  *      initRequest->implementationName = odr_prepend(o,
99  *              initRequest->implementationName, "ZOOM-C");
100  */
101 char *odr_prepend(ODR o, const char *prefix, const char *old)
102 {
103     int plen = (prefix == 0) ? 0 : strlen(prefix);
104     int olen = (old == 0) ? 0 : strlen(old);
105     char *res = (char*) odr_malloc (o, olen + plen + 2);
106
107     *res = '\0';
108     if (prefix != 0)
109         strcpy (res, prefix);
110     if (prefix != 0 && old != 0)
111         strcat (res, "/");
112     if (old !=0)
113         strcat (res, old);
114
115     return res;
116 }
117 /*
118  * Local variables:
119  * c-basic-offset: 4
120  * c-file-style: "Stroustrup"
121  * indent-tabs-mode: nil
122  * End:
123  * vim: shiftwidth=4 tabstop=8 expandtab
124  */
125