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