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