XML reader for data1 (EXPAT)
[yaz-moved-to-github.git] / retrieval / d1_handle.c
1 /*
2  * Copyright (c) 1995-2002, Index Data.
3  * See the file LICENSE for details.
4  *
5  * $Id: d1_handle.c,v 1.8 2002-04-05 12:46:07 adam Exp $
6  */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10
11 #include <yaz/log.h>
12 #include <yaz/data1.h>
13
14 struct data1_handle_info {
15     WRBUF wrbuf;
16     char *tab_path;
17     char *tab_root;
18
19     char *read_buf;
20     int read_len;
21
22     data1_absyn_cache absyn_cache;
23     data1_attset_cache attset_cache;
24
25     char *map_buf;
26     int map_len;
27
28     NMEM mem;
29 };
30
31 data1_handle data1_create (void)
32 {
33     data1_handle p = (data1_handle)xmalloc (sizeof(*p));
34     if (!p)
35         return NULL;
36     p->tab_path = NULL;
37     p->tab_root = NULL;
38     p->wrbuf = wrbuf_alloc();
39     p->read_buf = NULL;
40     p->read_len = 0;
41     p->map_buf = NULL;
42     p->map_len = 0;
43     p->absyn_cache = NULL;
44     p->attset_cache = NULL;
45     p->mem = nmem_create ();
46     return p;
47 }
48
49 NMEM data1_nmem_get (data1_handle dh)
50 {
51     return dh->mem;
52 }
53
54 data1_absyn_cache *data1_absyn_cache_get (data1_handle dh)
55 {
56     return &dh->absyn_cache;
57 }
58
59 data1_attset_cache *data1_attset_cache_get (data1_handle dh)
60 {
61     return &dh->attset_cache;
62 }
63
64 void data1_destroy (data1_handle dh)
65 {
66     if (!dh)
67         return;
68     wrbuf_free (dh->wrbuf, 1);
69     if (dh->tab_path)
70         xfree (dh->tab_path);
71     if (dh->tab_root)
72         xfree (dh->tab_root);
73     if (dh->read_buf)
74         xfree (dh->read_buf);
75     if (dh->map_buf)
76         xfree (dh->map_buf);
77     nmem_destroy (dh->mem);
78     
79     xfree (dh);
80 }
81
82 WRBUF data1_get_wrbuf (data1_handle dp)
83 {
84     return dp->wrbuf;
85 }
86
87 char **data1_get_read_buf (data1_handle dp, int **lenp)
88 {
89     *lenp = &dp->read_len;
90     yaz_log (LOG_DEBUG, "data1_get_read_buf lenp=%u", **lenp);
91     return &dp->read_buf;
92 }
93
94 char **data1_get_map_buf (data1_handle dp, int **lenp)
95 {
96     *lenp = &dp->map_len;
97     yaz_log (LOG_DEBUG, "data1_get_map_buf lenp=%u", **lenp);
98     return &dp->map_buf;
99 }
100
101 void data1_set_tabpath (data1_handle dp, const char *p)
102 {
103     xfree (dp->tab_path);
104     dp->tab_path = NULL;
105     if (p)
106         dp->tab_path = xstrdup (p);
107 }
108
109 void data1_set_tabroot (data1_handle dp, const char *p)
110 {
111     xfree (dp->tab_root);
112     dp->tab_root = NULL;
113     if (p)
114         dp->tab_root = xstrdup (p);
115 }
116
117 const char *data1_get_tabpath (data1_handle dp)
118 {
119     return dp->tab_path;
120 }
121
122 const char *data1_get_tabroot (data1_handle dp)
123 {
124     return dp->tab_root;
125 }
126
127 FILE *data1_path_fopen (data1_handle dh, const char *file, const char *mode)
128 {
129     const char *path = data1_get_tabpath(dh);
130     const char *root = data1_get_tabroot(dh);
131     return yaz_fopen (path, file, "r", root);
132 }