0cee5397764a537ffe8555032c2f959245d7977b
[yaz-moved-to-github.git] / odr / odr_util.c
1 /*
2  * Copyright (c) 1995-1997, Index Data
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: odr_util.c,v $
7  * Revision 1.12  1997-10-31 12:20:08  adam
8  * Improved memory debugging for xmalloc/nmem.c. References to NMEM
9  * instead of ODR in n ESPEC-1 handling in source d1_espec.c.
10  * Bug fix: missing fclose in data1_read_espec1.
11  *
12  */
13
14 #include <stdlib.h>
15 #include <string.h>
16 #include <ctype.h>
17 #include <odr.h>
18 #include <oid.h>
19
20 char *odr_indent(ODR o)
21 {
22     static char buf[512];
23     int i = o->indent;
24
25     memset(buf, ' ', 512);
26     if (i >= 128)
27         i = 127;
28     buf[o->indent * 4] = 0;
29     return buf;
30 }
31
32 int odp_more_chunks(ODR o, unsigned char *base, int len)
33 {
34     if (!len)
35         return 0;
36     if (len < 0) /* indefinite length */
37     {
38         if (*o->bp == 0 && *(o->bp + 1) == 0)
39         {
40             o->bp += 2;
41             o->left -= 2;
42             return 0;
43         }
44         else
45             return 1;
46     }
47     else
48         return o->bp - base < len;
49 }
50
51 Odr_oid *odr_oiddup_nmem(NMEM nmem, Odr_oid *o)
52 {
53     Odr_oid *r;
54
55     if (!o)
56         return 0;
57     if (!(r = nmem_malloc(nmem, (oid_oidlen(o) + 1) * sizeof(int))))
58         return 0;
59     oid_oidcpy(r, o);
60     return r;
61 }
62
63 Odr_oid *odr_oiddup(ODR odr, Odr_oid *o)
64 {
65     return odr_oiddup_nmem (odr->mem, o);
66 }
67
68 Odr_oid *odr_getoidbystr_nmem(NMEM nmem, char *str)
69 {
70     int num = 1, i = 0;
71     char *p = str;
72     Odr_oid *ret;
73
74     if (!isdigit(*str))
75         return 0;
76     while ((p = strchr(p, '.')))
77         num++, p++;
78     ret = nmem_malloc(nmem, sizeof(*ret)*(num + 1));
79     p = str;
80     do
81         ret[i++] = atoi(p);
82     while ((p = strchr(p, '.')) && ++p);
83     ret[i] = -1;
84     return ret;
85 }
86
87 Odr_oid *odr_getoidbystr(ODR o, char *str)
88 {
89     return odr_getoidbystr_nmem (o->mem, str);
90 }
91