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