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