MARC interface implemented. Minor bugs fixed. fmltest can
[egate.git] / fml / fmlmarc.c
1 /*
2  * FML interpreter. Europagate, 1995
3  *
4  * $Log: fmlmarc.c,v $
5  * Revision 1.1  1995/02/10 15:50:56  adam
6  * MARC interface implemented. Minor bugs fixed. fmltest can
7  * be used to format single MARC records. New function '\list'
8  * implemented.
9  *
10  */
11
12 #include <assert.h>
13 #include <stdlib.h>
14 #include <stdio.h>
15
16 #include <fmlmarc.h>
17 #include <iso2709p.h>
18
19 #include "fmlp.h"
20
21 struct fml_node *marc_to_fml (Fml fml, Iso2709Rec rec)
22 {
23     struct fml_node *ptr_0 = NULL, *ptr_1;
24     struct iso2709_dir *dir;
25
26     for (dir = rec->directory; dir; dir=dir->next)
27     {
28         struct fml_node *ptr;
29
30         ptr = fml_node_alloc (fml);
31         if (ptr_0)
32             ptr_1->p[1] = ptr;
33         else
34             ptr_0 = ptr_1 = ptr;
35
36         ptr_1 = ptr;
37
38         if (dir->fields)
39         {
40             struct iso2709_field *fields = dir->fields;
41
42             ptr = fml_node_alloc (fml);
43             ptr->p[0] = fml_atom_alloc (fml, fields->data);
44             ptr->is_atom = 1;
45             
46             while ((fields = fields->next))
47             {
48                 ptr = ptr->p[0] = fml_node_alloc (fml);
49                 ptr->p[0] = fml_atom_alloc (fml, fields->data);
50                 ptr->is_atom = 1;
51             }
52         }
53     }
54     return ptr_0;
55 }
56
57
58 static void add_string (const char *str, char **buf, int *max, int *size)
59 {
60     if (*size + strlen(str) >= *max)
61     {
62         char *nbuf;
63         int nsize = *size + strlen(str) + 2048;
64
65         nbuf = malloc (nsize);
66         assert (nbuf);
67         if (*buf)
68             strcpy (nbuf, *buf);
69         else
70             *nbuf = '\0';
71         free (*buf);
72         *buf = nbuf;
73         *max = nsize;
74     }
75     strcpy (*buf + *size, str);
76     *size += strlen(str);
77 }
78
79 char *marc_to_str (Fml fml, Iso2709Rec rec)
80 {
81     struct iso2709_dir *dir;
82     static char *buf = NULL;
83     static int max = 0;
84     int size = 0;
85
86     add_string ("{", &buf, &max, &size);
87     for (dir = rec->directory; dir; dir=dir->next)
88     {
89         struct iso2709_field *fields;
90
91         add_string ("{", &buf, &max, &size);
92         add_string (dir->tag, &buf, &max, &size);
93         add_string ("{", &buf, &max, &size);
94         
95         for (fields = dir->fields; fields; fields=fields->next)
96         {
97             add_string ("{", &buf, &max, &size);
98             
99             if (fields->indicator)
100             {
101                 add_string ("\'", &buf, &max, &size);
102                 add_string (fields->indicator, &buf, &max, &size);
103                 add_string ("\'", &buf, &max, &size);
104             }
105             else
106                 add_string ("{}", &buf, &max, &size);
107             add_string (" ", &buf, &max, &size);
108             if (fields->identifier)
109             {
110                 add_string ("\'", &buf, &max, &size);
111                 add_string (fields->identifier, &buf, &max, &size);
112                 add_string ("\'", &buf, &max, &size);
113             }
114             else
115                 add_string ("{}", &buf, &max, &size);
116             add_string (" ", &buf, &max, &size);
117             add_string ("\'", &buf, &max, &size);
118             add_string (fields->data, &buf, &max, &size);
119             add_string ("\'", &buf, &max, &size);
120             add_string ("}", &buf, &max, &size);
121         }
122         add_string ("}}\n", &buf, &max, &size);
123     }
124     add_string ("}", &buf, &max, &size);
125     return buf;
126 }