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