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