1bcce37dd3e2a64a76737a55e6c71899f0b7365f
[idzebra-moved-to-github.git] / recctrl / sgmlread.c
1 /*
2  * Copyright (C) 1994-1999, Index Data
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: sgmlread.c,v $
7  * Revision 1.10  1999-11-30 13:48:04  adam
8  * Improved installation. Updated for inclusion of YAZ header files.
9  *
10  * Revision 1.9  1999/07/14 10:56:16  adam
11  * Filter handles multiple records in one file.
12  *
13  * Revision 1.8  1999/06/25 13:47:25  adam
14  * Minor change that prevents MSVC warning.
15  *
16  * Revision 1.7  1999/05/21 12:00:17  adam
17  * Better diagnostics for extraction process.
18  *
19  * Revision 1.6  1999/05/20 12:57:18  adam
20  * Implemented TCL filter. Updated recctrl system.
21  *
22  * Revision 1.5  1999/02/02 14:51:31  adam
23  * Updated WIN32 code specific sections. Changed header.
24  *
25  * Revision 1.4  1997/09/17 12:19:22  adam
26  * Zebra version corresponds to YAZ version 1.4.
27  * Changed Zebra server so that it doesn't depend on global common_resource.
28  *
29  * Revision 1.3  1997/09/04 13:54:41  adam
30  * Added MARC filter - type grs.marc.<syntax> where syntax refers
31  * to abstract syntax. New method tellf in retrieve/extract method.
32  *
33  * Revision 1.2  1997/04/30 08:56:08  quinn
34  * null
35  *
36  * Revision 1.1  1996/10/11  10:57:32  adam
37  * New module recctrl. Used to manage records (extract/retrieval).
38  *
39  */
40 #include <assert.h>
41 #include <yaz/log.h>
42
43 #include "grsread.h"
44
45 struct sgml_getc_info {
46     char *buf;
47     int buf_size;
48     int size;
49     int off;
50     int moffset;
51     void *fh;
52     int (*readf)(void *, char *, size_t);
53     WRBUF wrbuf;
54 };
55
56 int sgml_getc (void *clientData)
57 {
58     struct sgml_getc_info *p = (struct sgml_getc_info *) clientData;
59     int res;
60     
61     if (p->off < p->size)
62         return p->buf[(p->off)++];
63     if (p->size < p->buf_size)
64         return 0;
65     p->moffset += p->off;
66     p->off = 0;
67     p->size = 0;
68     res = (*p->readf)(p->fh, p->buf, p->buf_size);
69     if (res > 0)
70     {
71         p->size += res;
72         return p->buf[(p->off)++];
73     }
74     return 0;
75 }
76
77 static data1_node *grs_read_sgml (struct grs_read_info *p)
78 {
79     struct sgml_getc_info *sgi = (struct sgml_getc_info *) p->clientData;
80     data1_node *node;
81     int res;
82     
83     sgi->moffset = p->offset;
84     sgi->fh = p->fh;
85     sgi->readf = p->readf;
86     sgi->off = 0;
87     sgi->size = 0;
88     res = (*sgi->readf)(sgi->fh, sgi->buf, sgi->buf_size);
89     if (res > 0)
90         sgi->size += res;
91     else
92         return 0;
93     node = data1_read_nodex (p->dh, p->mem, sgml_getc, sgi, sgi->wrbuf);
94     if (node && p->endf)
95         (*p->endf)(sgi->fh, sgi->moffset + sgi->off);
96     return node;
97 }
98
99 static void *grs_init_sgml(void)
100 {
101     struct sgml_getc_info *p = (struct sgml_getc_info *) xmalloc (sizeof(*p));
102     p->buf_size = 512;
103     p->buf = xmalloc (p->buf_size);
104     p->wrbuf = wrbuf_alloc();
105     return p;
106 }
107
108 static void grs_destroy_sgml(void *clientData)
109 {
110     struct sgml_getc_info *p = (struct sgml_getc_info *) clientData;
111
112     wrbuf_free(p->wrbuf, 1);
113     xfree (p->buf);
114     xfree (p);
115 }
116
117 static struct recTypeGrs sgml_type = {
118     "sgml",
119     grs_init_sgml,
120     grs_destroy_sgml,
121     grs_read_sgml
122 };
123
124 RecTypeGrs recTypeGrs_sgml = &sgml_type;
125