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