Fix sample PQF
[yaz-moved-to-github.git] / ccl / cclqfile.c
index 2263291..61cd463 100644 (file)
 /* CCL qualifiers
  * Europagate, 1995
  *
- * $Log: cclqfile.c,v $
- * Revision 1.3  1999-11-30 13:47:11  adam
- * Improved installation. Moved header files to include/yaz.
+ * $Id: cclqfile.c,v 1.14 2003-06-23 10:22:21 adam Exp $
  *
- * Revision 1.2  1997/04/30 08:52:06  quinn
- * Null
- *
- * Revision 1.1  1996/10/11  15:00:25  adam
- * CCL parser from Europagate Email gateway 1.0.
+ * Old Europagate Log:
  *
  * Revision 1.3  1995/05/16  09:39:26  adam
  * LICENSE.
 
 #include <stdio.h>
 #include <stdlib.h>
-#include <assert.h>
 #include <string.h>
 
 #include <yaz/ccl.h>
 
-void ccl_qual_fitem (CCL_bibset bibset, const char *cp, const char *qual_name)
+void ccl_qual_field (CCL_bibset bibset, const char *cp, const char *qual_name)
 {
-    char qual_type[128];
-    int no_scan;
-    int pair[128];
+    char qual_spec[128];
+    int type_ar[128];
+    int value_ar[128];
+    char *svalue_ar[128];
+    char *attsets[128];
     int pair_no = 0;
 
-    while (1)
+    while (pair_no < 128)
     {
-        char *qual_value;
-        char *split;
-        
-        if (sscanf (cp, "%s%n", qual_type, &no_scan) != 1)
-            break;
+        char *qual_value, *qual_type;
+        char *split, *setp;
+        int no_scan = 0;
         
-        if (!(split = strchr (qual_type, '=')))
+        if (sscanf (cp, "%100s%n", qual_spec, &no_scan) < 1)
+           break;
+
+        if (!(split = strchr (qual_spec, '=')))
+        {
+           /* alias specification .. */
+            if (pair_no == 0)
+            {
+                ccl_qual_add_combi (bibset, qual_name, cp);
+                return;
+            }
             break;
+        }
+       /* [set,]type=value ... */
         cp += no_scan;
         
         *split++ = '\0';
-        while (1)
+
+       setp = strchr (qual_spec, ',');
+       if (setp)
+       {
+           /* set,type=value ... */
+           *setp++ = '\0';
+            qual_type = setp;
+       }
+       else
+       {
+           /* type=value ... */
+            qual_type = qual_spec;
+       }
+        while (pair_no < 128)
         {
             int type, value;
 
             qual_value = split;
             if ((split = strchr (qual_value, ',')))
                 *split++ = '\0';
-            value = atoi (qual_value);
+
+           value = 0;
             switch (qual_type[0])
             {
             case 'u':
@@ -123,6 +142,10 @@ void ccl_qual_fitem (CCL_bibset bibset, const char *cp, const char *qual_name)
                 type = CCL_BIB1_STR;
                 if (!ccl_stricmp (qual_value, "pw"))
                     value = CCL_BIB1_STR_WP;
+                if (!ccl_stricmp (qual_value, "al"))
+                    value = CCL_BIB1_STR_AND_LIST;
+                if (!ccl_stricmp (qual_value, "ol"))
+                    value = CCL_BIB1_STR_OR_LIST;
                 break;                
             case 't':
             case 'T':
@@ -139,18 +162,56 @@ void ccl_qual_fitem (CCL_bibset bibset, const char *cp, const char *qual_name)
             case 'c':
             case 'C':
                 type = CCL_BIB1_COM;
-                break;                
+                break;
             default:
                 type = atoi (qual_type);
             }
-            pair[pair_no*2] = type;
-            pair[pair_no*2+1] = value;
+
+            type_ar[pair_no] = type;
+
+           if (value)
+           {
+               value_ar[pair_no] = value;
+               svalue_ar[pair_no] = 0;
+           }
+           else if (*qual_value >= '0' && *qual_value <= '9')
+           {
+               value_ar[pair_no] = atoi (qual_value);
+               svalue_ar[pair_no] = 0;
+           }
+           else
+           {
+               size_t len;
+               if (split)
+                   len = split - qual_value;
+               else
+                   len = strlen(qual_value);
+               svalue_ar[pair_no] = xmalloc(len+1);
+               memcpy(svalue_ar[pair_no], qual_value, len);
+               svalue_ar[pair_no][len] = '\0';
+           }
+           if (setp)
+           {
+               attsets[pair_no] = (char*) xmalloc (strlen(qual_spec)+1);
+               strcpy (attsets[pair_no], qual_spec);
+           }
+           else
+               attsets[pair_no] = 0;
             pair_no++;
             if (!split)
                 break;
         }
     }
-    ccl_qual_add (bibset, qual_name, pair_no, pair);
+    ccl_qual_add_set (bibset, qual_name, pair_no, type_ar, value_ar, svalue_ar,
+                     attsets);
+}
+
+void ccl_qual_fitem (CCL_bibset bibset, const char *cp, const char *qual_name)
+{
+    if (*qual_name == '@')
+       ccl_qual_add_special(bibset, qual_name+1, cp);
+    else
+       ccl_qual_field(bibset, cp, qual_name);
 }
 
 /*
@@ -169,18 +230,33 @@ void ccl_qual_fitem (CCL_bibset bibset, const char *cp, const char *qual_name)
 void ccl_qual_file (CCL_bibset bibset, FILE *inf)
 {
     char line[256];
-    char *cp;
+    char *cp, *cp1;
     char qual_name[128];
-    int  no_scan;
 
     while (fgets (line, 255, inf))
     {
+        int  no_scan = 0;
+
         cp = line;
         if (*cp == '#')
             continue;        /* ignore lines starting with # */
-        if (sscanf (cp, "%s%n", qual_name, &no_scan) != 1)
+        if (sscanf (cp, "%100s%n", qual_name, &no_scan) < 1)
             continue;        /* also ignore empty lines */
         cp += no_scan;
+       cp1 = strchr(cp, '#');
+       if (cp1)
+           *cp1 = '\0';
         ccl_qual_fitem (bibset, cp, qual_name);
     }
 }
+
+int ccl_qual_fname (CCL_bibset bibset, const char *fname)
+{
+    FILE *inf;
+    inf = fopen (fname, "r");
+    if (!inf)
+        return -1;
+    ccl_qual_file (bibset, inf);
+    fclose (inf);
+    return 0;
+}