117702d48fdf02630184e78dd2f9a0c1c0d44544
[egate.git] / ccl / cclqual.c
1 /* CCL qualifiers
2  * Europagate, 1995
3  *
4  * $Log: cclqual.c,v $
5  * Revision 1.7  1995/04/17 09:31:46  adam
6  * Improved handling of qualifiers. Aliases or reserved words.
7  *
8  * Revision 1.6  1995/02/23  08:32:00  adam
9  * Changed header.
10  *
11  * Revision 1.4  1995/02/14  19:55:12  adam
12  * Header files ccl.h/cclp.h are gone! They have been merged an
13  * moved to ../include/ccl.h.
14  * Node kind(s) in ccl_rpn_node have changed names.
15  *
16  * Revision 1.3  1995/02/14  16:20:56  adam
17  * Qualifiers are read from a file now.
18  *
19  * Revision 1.2  1995/02/14  10:25:56  adam
20  * The constructions 'qualifier rel term ...' implemented.
21  *
22  * Revision 1.1  1995/02/13  15:15:07  adam
23  * Added handling of qualifiers. Not finished yet.
24  *
25  */
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <assert.h>
30 #include <string.h>
31
32 #include <ccl.h>
33
34 /* Definition of CCL_bibset pointer */
35 struct ccl_qualifiers {
36     struct ccl_qualifier *list;
37 };
38
39 /*
40  * ccl_qual_add: Add qualifier to Bibset. If qualifier already
41  *               exists, then attributes are appendend to old
42  *               definition.
43  * name:    name of qualifier
44  * no:      No of attribute type/value pairs.
45  * pairs:   Attributes. pairs[0] first type, pair[1] first value,
46  *          ... pair[2*no-2] last type, pair[2*no-1] last value.
47  */
48 void ccl_qual_add (CCL_bibset b, const char *name, int no, int *pairs)
49 {
50     struct ccl_qualifier *q;
51     struct ccl_rpn_attr **attrp;
52
53     assert (b);
54     for (q = b->list; q; q = q->next)
55         if (!strcmp (name, q->name))
56             break;
57     if (!q)
58     {
59         struct ccl_qualifier *new_qual = malloc (sizeof(*new_qual));
60         assert (new_qual);
61         
62         new_qual->next = b->list;
63         b->list = new_qual;
64         
65         new_qual->name = malloc (strlen(name)+1);
66         assert (new_qual->name);
67         strcpy (new_qual->name, name);
68         attrp = &new_qual->attr_list;
69     }
70     else
71     {
72         attrp = &q->attr_list;
73         while (*attrp)
74             attrp = &(*attrp)->next;
75     }
76     while (--no >= 0)
77     {
78         struct ccl_rpn_attr *attr;
79
80         attr = malloc (sizeof(*attr));
81         assert (attr);
82         attr->type = *pairs++;
83         attr->value = *pairs++;
84         *attrp = attr;
85         attrp = &attr->next;
86     }
87     *attrp = NULL;
88 }
89
90 /*
91  * ccl_qual_mk: Make new (empty) bibset.
92  * return:   empty bibset.
93  */
94 CCL_bibset ccl_qual_mk (void)
95 {
96     CCL_bibset b = malloc (sizeof(*b));
97     assert (b);
98     b->list = NULL;     
99     return b;
100 }
101
102 /*
103  * ccl_qual_rm: Delete bibset.
104  * b:        pointer to bibset
105  */
106 void ccl_qual_rm (CCL_bibset *b)
107 {
108     struct ccl_qualifier *q, *q1;
109
110     if (!*b)
111         return;
112     for (q = (*b)->list; q; q = q1)
113     {
114         struct ccl_rpn_attr *attr, *attr1;
115
116         for (attr = q->attr_list; attr; attr = attr1)
117         {
118             attr1 = attr->next;
119             free (attr);
120         }
121         q1 = q->next;
122         free (q);
123     }
124     free (*b);
125     *b = NULL;
126 }
127
128 /*
129  * ccl_qual_search: Search for qualifier in bibset.
130  * b:      Bibset
131  * name:   Name of qualifier to search for (need no null-termination)
132  * len:    Length of name.
133  * return: Attribute info. NULL if not found.
134  */
135 struct ccl_rpn_attr *ccl_qual_search (CCL_bibset b, const char *name, int len)
136 {
137     struct ccl_qualifier *q;
138
139     assert (b);
140     for (q = b->list; q; q = q->next)
141         if (strlen(q->name) == len && !memcmp (name, q->name, len))
142             return q->attr_list;
143     return NULL;
144 }
145