5ba4aa306c4d700081ca32abd602551a2833459b
[yaz-moved-to-github.git] / ccl / cclqual.c
1 /* CCL qualifiers
2  * Europagate, 1995
3  *
4  * $Log: cclqual.c,v $
5  * Revision 1.1  1995-04-10 10:28:20  quinn
6  * Added copy of CCL.
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 struct ccl_qualifiers {
35     struct ccl_qualifier *list;
36 };
37
38 void ccl_qual_add (CCL_bibset b, const char *name, int no, int *pairs)
39 {
40     struct ccl_qualifier *q;
41     struct ccl_rpn_attr **attrp;
42
43     assert (b);
44     for (q = b->list; q; q = q->next)
45         if (!strcmp (name, q->name))
46             break;
47     if (!q)
48     {
49         struct ccl_qualifier *new_qual = malloc (sizeof(*new_qual));
50         assert (new_qual);
51         
52         new_qual->next = b->list;
53         b->list = new_qual;
54         
55         new_qual->name = malloc (strlen(name)+1);
56         assert (new_qual->name);
57         strcpy (new_qual->name, name);
58         attrp = &new_qual->attr_list;
59     }
60     else
61     {
62         attrp = &q->attr_list;
63         while (*attrp)
64             attrp = &(*attrp)->next;
65     }
66     while (--no >= 0)
67     {
68         struct ccl_rpn_attr *attr;
69
70         attr = malloc (sizeof(*attr));
71         assert (attr);
72         attr->type = *pairs++;
73         attr->value = *pairs++;
74         *attrp = attr;
75         attrp = &attr->next;
76     }
77     *attrp = NULL;
78 }
79
80 CCL_bibset ccl_qual_mk (void)
81 {
82     CCL_bibset b = malloc (sizeof(*b));
83     assert (b);
84     b->list = NULL;     
85     return b;
86 }
87
88 void ccl_qual_rm (CCL_bibset *b)
89 {
90     assert (*b);
91     *b = NULL;
92 }
93
94 struct ccl_rpn_attr *ccl_qual_search (CCL_bibset b, const char *name, int len)
95 {
96     struct ccl_qualifier *q;
97
98     assert (b);
99     for (q = b->list; q; q = q->next)
100         if (strlen(q->name) == len && !memcmp (name, q->name, len))
101             return q->attr_list;
102     return NULL;
103 }
104
105 void ccl_qual_file (CCL_bibset bibset, FILE *inf)
106 {
107     char line[256];
108     char *cp;
109     char qual_name[128];
110     char qual_des[128];
111     int  no_scan;
112
113     while (fgets (line, 255, inf))
114     {
115         cp = line;
116         if (*cp == '#')
117             continue;
118         if (sscanf (cp, "%s%n", qual_name, &no_scan) != 1)
119             continue;
120         cp += no_scan;
121         while (1)
122         {
123             int pair[2];
124             char *qual_type;
125             char *qual_value;
126             char *split;
127
128             if (sscanf (cp, "%s%n", qual_des, &no_scan) != 1)
129                 break;
130
131             if (!(split = strchr (qual_des, '=')))
132                 break;
133             cp += no_scan;
134
135             *split++ = '\0';
136             qual_type = qual_des;
137             qual_value = split;
138             while (1)
139             {
140                 if ((split = strchr (qual_value, ',')))
141                     *split++ = '\0';
142                 pair[1] = atoi (qual_value);
143                 switch (qual_type[0])
144                 {
145                 case 'u':
146                     pair[0] = CCL_BIB1_USE;
147                     break;
148                 case 'r':
149                     pair[0] = CCL_BIB1_REL;
150                     if (!strcmp (qual_value, "o"))
151                         pair[1] = CCL_BIB1_REL_ORDER;
152                     break;                
153                 case 'p':
154                     pair[0] = CCL_BIB1_POS;
155                     break;
156                 case 's':
157                     pair[0] = CCL_BIB1_STR;
158                     if (!strcmp (qual_value, "pw"))
159                         pair[1] = CCL_BIB1_STR_WP;
160                     break;                
161                 case 't':
162                     pair[0] = CCL_BIB1_TRU;
163                     if (!strcmp (qual_value, "l"))
164                         pair[1] = CCL_BIB1_TRU_CAN_LEFT;
165                     else if (!strcmp (qual_value, "r"))
166                         pair[1] = CCL_BIB1_TRU_CAN_RIGHT;
167                     else if (!strcmp (qual_value, "b"))
168                         pair[1] = CCL_BIB1_TRU_CAN_BOTH;
169                     else if (!strcmp (qual_value, "n"))
170                         pair[1] = CCL_BIB1_TRU_CAN_NONE;
171                     break;                
172                 case 'c':
173                     pair[0] = CCL_BIB1_COM;
174                     break;                
175                 default:
176                     pair[0] = atoi (qual_type);
177                 }
178                 ccl_qual_add (bibset, qual_name, 1, pair);
179                 if (!split)
180                     break;
181                 qual_value = split;
182             }
183         }
184     }
185 }