Smallish
[yaz-moved-to-github.git] / ccl / cclqual.c
1 /* CCL qualifiers
2  * Europagate, 1995
3  *
4  * $Log: cclqual.c,v $
5  * Revision 1.3  1995-09-29 17:12:00  quinn
6  * Smallish
7  *
8  * Revision 1.2  1995/09/27  15:02:44  quinn
9  * Modified function heads & prototypes.
10  *
11  * Revision 1.1  1995/04/10  10:28:20  quinn
12  * Added copy of CCL.
13  *
14  * Revision 1.6  1995/02/23  08:32:00  adam
15  * Changed header.
16  *
17  * Revision 1.4  1995/02/14  19:55:12  adam
18  * Header files ccl.h/cclp.h are gone! They have been merged an
19  * moved to ../include/ccl.h.
20  * Node kind(s) in ccl_rpn_node have changed names.
21  *
22  * Revision 1.3  1995/02/14  16:20:56  adam
23  * Qualifiers are read from a file now.
24  *
25  * Revision 1.2  1995/02/14  10:25:56  adam
26  * The constructions 'qualifier rel term ...' implemented.
27  *
28  * Revision 1.1  1995/02/13  15:15:07  adam
29  * Added handling of qualifiers. Not finished yet.
30  *
31  */
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <assert.h>
36 #include <string.h>
37
38 #include <ccl.h>
39
40 struct ccl_qualifiers {
41     struct ccl_qualifier *list;
42 };
43
44 void ccl_qual_add (CCL_bibset b, const char *name, int no, int *pairs)
45 {
46     struct ccl_qualifier *q;
47     struct ccl_rpn_attr **attrp;
48
49     assert (b);
50     for (q = b->list; q; q = q->next)
51         if (!strcmp (name, q->name))
52             break;
53     if (!q)
54     {
55         struct ccl_qualifier *new_qual = malloc (sizeof(*new_qual));
56         assert (new_qual);
57         
58         new_qual->next = b->list;
59         b->list = new_qual;
60         
61         new_qual->name = malloc (strlen(name)+1);
62         assert (new_qual->name);
63         strcpy (new_qual->name, name);
64         attrp = &new_qual->attr_list;
65     }
66     else
67     {
68         attrp = &q->attr_list;
69         while (*attrp)
70             attrp = &(*attrp)->next;
71     }
72     while (--no >= 0)
73     {
74         struct ccl_rpn_attr *attr;
75
76         attr = malloc (sizeof(*attr));
77         assert (attr);
78         attr->type = *pairs++;
79         attr->value = *pairs++;
80         *attrp = attr;
81         attrp = &attr->next;
82     }
83     *attrp = NULL;
84 }
85
86 CCL_bibset ccl_qual_mk (void)
87 {
88     CCL_bibset b = malloc (sizeof(*b));
89     assert (b);
90     b->list = NULL;     
91     return b;
92 }
93
94 void ccl_qual_rm (CCL_bibset *b)
95 {
96     assert (*b);
97     *b = NULL;
98 }
99
100 struct ccl_rpn_attr *ccl_qual_search (CCL_bibset b, const char *name, int len)
101 {
102     struct ccl_qualifier *q;
103
104     assert (b);
105     for (q = b->list; q; q = q->next)
106         if (strlen(q->name) == len && !memcmp (name, q->name, len))
107             return q->attr_list;
108     return NULL;
109 }
110
111 void ccl_qual_file (CCL_bibset bibset, FILE *inf)
112 {
113     char line[256];
114     char *cp;
115     char qual_name[128];
116     char qual_des[128];
117     int  no_scan;
118
119     while (fgets (line, 255, inf))
120     {
121         cp = line;
122         if (*cp == '#')
123             continue;
124         if (sscanf (cp, "%s%n", qual_name, &no_scan) != 1)
125             continue;
126         cp += no_scan;
127         while (1)
128         {
129             int pair[2];
130             char *qual_type;
131             char *qual_value;
132             char *split;
133
134             if (sscanf (cp, "%s%n", qual_des, &no_scan) != 1)
135                 break;
136
137             if (!(split = strchr (qual_des, '=')))
138                 break;
139             cp += no_scan;
140
141             *split++ = '\0';
142             qual_type = qual_des;
143             qual_value = split;
144             while (1)
145             {
146                 if ((split = strchr (qual_value, ',')))
147                     *split++ = '\0';
148                 pair[1] = atoi (qual_value);
149                 switch (qual_type[0])
150                 {
151                 case 'u':
152                     pair[0] = CCL_BIB1_USE;
153                     break;
154                 case 'r':
155                     pair[0] = CCL_BIB1_REL;
156                     if (!strcmp (qual_value, "o"))
157                         pair[1] = CCL_BIB1_REL_ORDER;
158                     break;                
159                 case 'p':
160                     pair[0] = CCL_BIB1_POS;
161                     break;
162                 case 's':
163                     pair[0] = CCL_BIB1_STR;
164                     if (!strcmp (qual_value, "pw"))
165                         pair[1] = CCL_BIB1_STR_WP;
166                     break;                
167                 case 't':
168                     pair[0] = CCL_BIB1_TRU;
169                     if (!strcmp (qual_value, "l"))
170                         pair[1] = CCL_BIB1_TRU_CAN_LEFT;
171                     else if (!strcmp (qual_value, "r"))
172                         pair[1] = CCL_BIB1_TRU_CAN_RIGHT;
173                     else if (!strcmp (qual_value, "b"))
174                         pair[1] = CCL_BIB1_TRU_CAN_BOTH;
175                     else if (!strcmp (qual_value, "n"))
176                         pair[1] = CCL_BIB1_TRU_CAN_NONE;
177                     break;                
178                 case 'c':
179                     pair[0] = CCL_BIB1_COM;
180                     break;                
181                 default:
182                     pair[0] = atoi (qual_type);
183                 }
184                 ccl_qual_add (bibset, qual_name, 1, pair);
185                 if (!split)
186                     break;
187                 qual_value = split;
188             }
189         }
190     }
191 }