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