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