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