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