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