Using the new ylog.h everywhere, and fixing what that breaks!
[idzebra-moved-to-github.git] / data1 / d1_handle.c
1 /* $Id: d1_handle.c,v 1.5 2004-11-19 10:26:53 heikki Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
3    Index Data Aps
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21 */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25
26 #include <yaz/ylog.h>
27 #include <idzebra/data1.h>
28
29 struct data1_handle_info {
30     WRBUF wrbuf;
31     char *tab_path;
32     char *tab_root;
33
34     char *read_buf;
35     int read_len;
36
37     data1_absyn_cache absyn_cache;
38     data1_attset_cache attset_cache;
39
40     char *map_buf;
41     int map_len;
42
43     NMEM mem;
44     int flags;
45 };
46
47 data1_handle data1_create (void)
48 {
49     return data1_createx(0);
50 }
51
52 data1_handle data1_createx (int flags)
53 {
54     data1_handle p = (data1_handle)xmalloc (sizeof(*p));
55     if (!p)
56         return NULL;
57     p->tab_path = NULL;
58     p->tab_root = NULL;
59     p->wrbuf = wrbuf_alloc();
60     p->read_buf = NULL;
61     p->read_len = 0;
62     p->map_buf = NULL;
63     p->map_len = 0;
64     p->absyn_cache = NULL;
65     p->attset_cache = NULL;
66     p->mem = nmem_create ();
67     p->flags = flags;
68     return p;
69 }
70
71 NMEM data1_nmem_get (data1_handle dh)
72 {
73     return dh->mem;
74 }
75
76 data1_absyn_cache *data1_absyn_cache_get (data1_handle dh)
77 {
78     return &dh->absyn_cache;
79 }
80
81 data1_attset_cache *data1_attset_cache_get (data1_handle dh)
82 {
83     return &dh->attset_cache;
84 }
85
86 void data1_destroy (data1_handle dh)
87 {
88     if (!dh)
89         return;
90     
91     /* *ostrich*
92        We need to destroy DFAs, in xp_element (xelm) definitions 
93        pop, 2002-12-13
94     */
95     data1_absyn_destroy(dh);
96
97     wrbuf_free (dh->wrbuf, 1);
98     if (dh->tab_path)
99         xfree (dh->tab_path);
100     if (dh->tab_root)
101         xfree (dh->tab_root);
102     if (dh->read_buf)
103         xfree (dh->read_buf);
104     if (dh->map_buf)
105         xfree (dh->map_buf);
106     nmem_destroy (dh->mem);
107     
108     xfree (dh);
109 }
110
111 WRBUF data1_get_wrbuf (data1_handle dp)
112 {
113     return dp->wrbuf;
114 }
115
116 char **data1_get_read_buf (data1_handle dp, int **lenp)
117 {
118     *lenp = &dp->read_len;
119     yaz_log (YLOG_DEBUG, "data1_get_read_buf lenp=%u", **lenp);
120     return &dp->read_buf;
121 }
122
123 char **data1_get_map_buf (data1_handle dp, int **lenp)
124 {
125     *lenp = &dp->map_len;
126     yaz_log (YLOG_DEBUG, "data1_get_map_buf lenp=%u", **lenp);
127     return &dp->map_buf;
128 }
129
130 void data1_set_tabpath (data1_handle dp, const char *p)
131 {
132     xfree (dp->tab_path);
133     dp->tab_path = NULL;
134     if (p)
135         dp->tab_path = xstrdup (p);
136 }
137
138 void data1_set_tabroot (data1_handle dp, const char *p)
139 {
140     xfree (dp->tab_root);
141     dp->tab_root = NULL;
142     if (p)
143         dp->tab_root = xstrdup (p);
144 }
145
146 const char *data1_get_tabpath (data1_handle dp)
147 {
148     return dp->tab_path;
149 }
150
151 const char *data1_get_tabroot (data1_handle dp)
152 {
153     return dp->tab_root;
154 }
155
156 FILE *data1_path_fopen (data1_handle dh, const char *file, const char *mode)
157 {
158     const char *path = data1_get_tabpath(dh);
159     const char *root = data1_get_tabroot(dh);
160     return yaz_fopen (path, file, "r", root);
161 }
162
163 int data1_is_xmlmode(data1_handle dh)
164 {
165     return dh->flags & DATA1_FLAG_XML;
166 }