change license for data1 source
[idzebra-moved-to-github.git] / data1 / d1_handle.c
1 /* $Id: d1_handle.c,v 1.2 2002-10-22 13:19:50 adam 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/log.h>
27 #include <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     wrbuf_free (dh->wrbuf, 1);
91     if (dh->tab_path)
92         xfree (dh->tab_path);
93     if (dh->tab_root)
94         xfree (dh->tab_root);
95     if (dh->read_buf)
96         xfree (dh->read_buf);
97     if (dh->map_buf)
98         xfree (dh->map_buf);
99     nmem_destroy (dh->mem);
100     
101     xfree (dh);
102 }
103
104 WRBUF data1_get_wrbuf (data1_handle dp)
105 {
106     return dp->wrbuf;
107 }
108
109 char **data1_get_read_buf (data1_handle dp, int **lenp)
110 {
111     *lenp = &dp->read_len;
112     yaz_log (LOG_DEBUG, "data1_get_read_buf lenp=%u", **lenp);
113     return &dp->read_buf;
114 }
115
116 char **data1_get_map_buf (data1_handle dp, int **lenp)
117 {
118     *lenp = &dp->map_len;
119     yaz_log (LOG_DEBUG, "data1_get_map_buf lenp=%u", **lenp);
120     return &dp->map_buf;
121 }
122
123 void data1_set_tabpath (data1_handle dp, const char *p)
124 {
125     xfree (dp->tab_path);
126     dp->tab_path = NULL;
127     if (p)
128         dp->tab_path = xstrdup (p);
129 }
130
131 void data1_set_tabroot (data1_handle dp, const char *p)
132 {
133     xfree (dp->tab_root);
134     dp->tab_root = NULL;
135     if (p)
136         dp->tab_root = xstrdup (p);
137 }
138
139 const char *data1_get_tabpath (data1_handle dp)
140 {
141     return dp->tab_path;
142 }
143
144 const char *data1_get_tabroot (data1_handle dp)
145 {
146     return dp->tab_root;
147 }
148
149 FILE *data1_path_fopen (data1_handle dh, const char *file, const char *mode)
150 {
151     const char *path = data1_get_tabpath(dh);
152     const char *root = data1_get_tabroot(dh);
153     return yaz_fopen (path, file, "r", root);
154 }
155
156 int data1_is_xmlmode(data1_handle dh)
157 {
158     return dh->flags & DATA1_FLAG_XML;
159 }