9108c2af2d31ac9565d8a7ac2a943c97edd60f65
[egate.git] / fml / fmlmarc.c
1 /*
2  * FML interpreter. Europagate, 1995
3  *
4  * $Log: fmlmarc.c,v $
5  * Revision 1.5  1995/03/30 14:22:02  adam
6  * Uses MARC anchor functions now.
7  *
8  * Revision 1.4  1995/02/23  08:32:05  adam
9  * Changed header.
10  *
11  * Revision 1.2  1995/02/10  16:52:08  adam
12  * Indicator field moved in MARC structure. The FML list representation
13  * of a MARC record has changed.
14  *
15  * Revision 1.1  1995/02/10  15:50:56  adam
16  * MARC interface implemented. Minor bugs fixed. fmltest can
17  * be used to format single MARC records. New function '\list'
18  * implemented.
19  *
20  */
21
22 #include <assert.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25
26 #include <fmlmarc.h>
27 #include <iso2709.h>
28
29 #include "fmlp.h"
30
31 static void add_string (const char *str, char **buf, int *max, int *size)
32 {
33     if (*size + strlen(str) >= *max)
34     {
35         char *nbuf;
36         int nsize = *size + strlen(str) + 2048;
37
38         nbuf = malloc (nsize);
39         assert (nbuf);
40         if (*buf)
41             strcpy (nbuf, *buf);
42         else
43             *nbuf = '\0';
44         free (*buf);
45         *buf = nbuf;
46         *max = nsize;
47     }
48     strcpy (*buf + *size, str);
49     *size += strlen(str);
50 }
51
52 char *marc_to_str (Fml fml, Iso2709Rec rec)
53 {
54     static char *buf = NULL;
55     static int max = 0;
56     int size = 0;
57     Iso2709Anchor a;
58     char *tag, *indicator, *identifier, *data;
59
60     add_string ("{", &buf, &max, &size);
61     a = iso2709_a_mk (rec);
62     do
63     {
64         if (!iso2709_a_info_line (a, &tag, &indicator))
65             break;
66         add_string ("{\'", &buf, &max, &size);
67         add_string (tag, &buf, &max, &size);
68         add_string ("\'",&buf, &max, &size);
69         if (indicator)
70         {
71             add_string ("\'", &buf, &max, &size);
72             add_string (indicator, &buf, &max, &size);
73             add_string ("\'", &buf, &max, &size);
74         }
75         else
76             add_string ("{}", &buf, &max, &size);
77         add_string ("{", &buf, &max, &size);
78         do
79         {
80             iso2709_a_info_field (a, NULL, NULL, &identifier, &data);
81             add_string ("{", &buf, &max, &size);
82             if (identifier)
83             {
84                 add_string ("\'", &buf, &max, &size);
85                 add_string (identifier, &buf, &max, &size);
86                 add_string ("\'", &buf, &max, &size);
87             }
88             else
89                 add_string ("{}", &buf, &max, &size);
90             add_string (" \'", &buf, &max, &size);
91             add_string (data, &buf, &max, &size);
92             add_string ("\'}", &buf, &max, &size);
93         } while (iso2709_a_next_field (a));
94         add_string ("}}\n", &buf, &max, &size);
95     } while (iso2709_a_next_line (a));
96     add_string ("}", &buf, &max, &size);
97     iso2709_a_rm (a);
98     return buf;
99 }