d95a693841daeeb30b2218784b4299b61606c8c4
[yaz-moved-to-github.git] / src / odr_util.c
1 /*
2  * Copyright (c) 1995-2003, Index Data
3  * See the file LICENSE for details.
4  *
5  * $Id: odr_util.c,v 1.1 2003-10-27 12:21:34 adam Exp $
6  */
7 #if HAVE_CONFIG_H
8 #include <config.h>
9 #endif
10
11 #include <stdlib.h>
12 #include <string.h>
13 #include <ctype.h>
14 #include "odr-priv.h"
15 #include <yaz/oid.h>
16
17 void odr_prname(ODR o, const char *name)
18 {
19     if (name)
20         fprintf (o->print, "%*s%s ", o->indent*4, "", name);
21     else
22         fprintf (o->print, "%*s", o->indent*4, "");
23 }
24
25 int odp_more_chunks(ODR o, const unsigned char *base, int len)
26 {
27     if (!len)
28         return 0;
29     if (len < 0) /* indefinite length */
30     {
31         if (*o->bp == 0 && *(o->bp + 1) == 0)
32         {
33             o->bp += 2;
34             return 0;
35         }
36         else
37             return 1;
38     }
39     else
40         return o->bp - base < len;
41 }
42
43 Odr_oid *odr_oiddup_nmem(NMEM nmem, Odr_oid *o)
44 {
45     Odr_oid *r;
46
47     if (!o)
48         return 0;
49     if (!(r = (int *)nmem_malloc(nmem, (oid_oidlen(o) + 1) * sizeof(int))))
50         return 0;
51     oid_oidcpy(r, o);
52     return r;
53 }
54
55 Odr_oid *odr_oiddup(ODR odr, Odr_oid *o)
56 {
57     return odr_oiddup_nmem (odr->mem, o);
58 }
59
60 Odr_oid *odr_getoidbystr_nmem(NMEM nmem, const char *str)
61 {
62     int num = 1, i = 0;
63     const char *p = str;
64     Odr_oid *ret;
65
66     if (!isdigit(*str))
67         return 0;
68     while ((p = strchr(p, '.')))
69         num++, p++;
70     ret = (int *)nmem_malloc(nmem, sizeof(*ret)*(num + 1));
71     p = str;
72     do
73         ret[i++] = atoi(p);
74     while ((p = strchr(p, '.')) && *++p);
75     ret[i] = -1;
76     return ret;
77 }
78
79 Odr_oid *odr_getoidbystr(ODR o, const char *str)
80 {
81     return odr_getoidbystr_nmem (o->mem, str);
82 }
83
84 int odr_missing(ODR o, int opt, const char *name)
85 {
86     if (o->error)
87         return 0;
88     if (!opt)
89     {
90         odr_seterror(o, OREQUIRED, 53);
91         odr_setelement(o, name);
92     }
93     return opt;
94 }