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