Added function yaz_oid_add which adds custom OID entry to database.
[yaz-moved-to-github.git] / src / oid_util.c
1 /*
2  * Copyright (C) 1995-2007, Index Data ApS
3  * See the file LICENSE for details.
4  *
5  * $Id: oid_util.c,v 1.2 2007-04-12 20:47:28 adam Exp $
6  */
7
8 /**
9  * \file oid_util.c
10  * \brief Implements OID base utilities
11  *
12  */
13 #if HAVE_CONFIG_H
14 #include <config.h>
15 #endif
16
17 #include <stdlib.h>
18 #include <string.h>
19 #include <ctype.h>
20
21 #include <yaz/snprintf.h>
22 #include <yaz/oid_util.h>
23
24 void oid_oidcpy(int *t, const int *s)
25 {
26     while ((*(t++) = *(s++)) > -1);
27 }
28
29 void oid_oidcat(int *t, const int *s)
30 {
31     while (*t > -1)
32         t++;
33     while ((*(t++) = *(s++)) > -1);
34 }
35
36 int oid_oidcmp(const int *o1, const int *o2)
37 {
38     while (*o1 == *o2 && *o1 > -1)
39     {
40         o1++;
41         o2++;
42     }
43     if (*o1 == *o2)
44         return 0;
45     else if (*o1 > *o2)
46         return 1;
47     else
48         return -1;
49 }
50
51 int oid_oidlen(const int *o)
52 {
53     int len = 0;
54
55     while (*(o++) >= 0)
56         len++;
57     return len;
58 }
59
60
61 char *oid_oid_to_dotstring(const int *oid, char *oidbuf)
62 {
63     char tmpbuf[20];
64     int i;
65
66     oidbuf[0] = '\0';
67     for (i = 0; oid[i] != -1 && i < OID_SIZE; i++) 
68     {
69         yaz_snprintf(tmpbuf, sizeof(tmpbuf)-1, "%d", oid[i]);
70         if (i > 0)
71             strcat(oidbuf, ".");
72         strcat(oidbuf, tmpbuf);
73     }
74     return oidbuf;
75 }
76
77 int oid_dotstring_to_oid(const char *name, int *oid)
78 {
79     int i = 0;
80     int val = 0;
81     while (isdigit (*(unsigned char *) name))
82     {
83         val = val*10 + (*name - '0');
84         name++;
85         if (*name == '.')
86         {
87             if (i < OID_SIZE-1)
88                 oid[i++] = val;
89             val = 0;
90             name++;
91         }
92     }
93     oid[i] = val;
94     oid[i+1] = -1;
95     return 0;
96 }
97
98 /*
99  * Local variables:
100  * c-basic-offset: 4
101  * indent-tabs-mode: nil
102  * End:
103  * vim: shiftwidth=4 tabstop=8 expandtab
104  */
105