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