Bump copyright year
[idzebra-moved-to-github.git] / data1 / d1_handle.c
1 /* This file is part of the Zebra server.
2    Copyright (C) 1994-2010 Index Data
3
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22
23 #include <yaz/log.h>
24 #include <idzebra/data1.h>
25
26 #define DATA1_FLAG_XML  1
27
28 struct data1_handle_info {
29     WRBUF wrbuf;
30     char *tab_path;
31     char *tab_root;
32
33     char *read_buf;
34     int read_len;
35
36     data1_absyn_cache absyn_cache;
37     data1_attset_cache attset_cache;
38
39     char *map_buf;
40     int map_len;
41
42     NMEM mem;
43 };
44
45 data1_handle data1_create(void)
46 {
47     data1_handle p = (data1_handle)xmalloc (sizeof(*p));
48     if (!p)
49         return 0;
50     p->tab_path = NULL;
51     p->tab_root = NULL;
52     p->wrbuf = wrbuf_alloc();
53     p->read_buf = NULL;
54     p->read_len = 0;
55     p->map_buf = NULL;
56     p->map_len = 0;
57     p->absyn_cache = NULL;
58     p->attset_cache = NULL;
59     p->mem = nmem_create ();
60     return p;
61 }
62
63 NMEM data1_nmem_get (data1_handle dh)
64 {
65     return dh->mem;
66 }
67
68 data1_absyn_cache *data1_absyn_cache_get (data1_handle dh)
69 {
70     return &dh->absyn_cache;
71 }
72
73 data1_attset_cache *data1_attset_cache_get (data1_handle dh)
74 {
75     return &dh->attset_cache;
76 }
77
78 void data1_destroy (data1_handle dh)
79 {
80     if (!dh)
81         return;
82     
83     /* *ostrich*
84        We need to destroy DFAs, in xp_element (xelm) definitions 
85        pop, 2002-12-13
86     */
87     data1_absyn_destroy(dh);
88
89     wrbuf_destroy(dh->wrbuf);
90     if (dh->tab_path)
91         xfree (dh->tab_path);
92     if (dh->tab_root)
93         xfree (dh->tab_root);
94     if (dh->read_buf)
95         xfree (dh->read_buf);
96     if (dh->map_buf)
97         xfree (dh->map_buf);
98     nmem_destroy (dh->mem);
99     
100     xfree (dh);
101 }
102
103 WRBUF data1_get_wrbuf (data1_handle dp)
104 {
105     return dp->wrbuf;
106 }
107
108 char **data1_get_read_buf (data1_handle dp, int **lenp)
109 {
110     *lenp = &dp->read_len;
111     yaz_log (YLOG_DEBUG, "data1_get_read_buf lenp=%u", **lenp);
112     return &dp->read_buf;
113 }
114
115 char **data1_get_map_buf (data1_handle dp, int **lenp)
116 {
117     *lenp = &dp->map_len;
118     yaz_log (YLOG_DEBUG, "data1_get_map_buf lenp=%u", **lenp);
119     return &dp->map_buf;
120 }
121
122 void data1_set_tabpath (data1_handle dp, const char *p)
123 {
124     xfree (dp->tab_path);
125     dp->tab_path = NULL;
126     if (p)
127         dp->tab_path = xstrdup (p);
128 }
129
130 void data1_set_tabroot (data1_handle dp, const char *p)
131 {
132     xfree (dp->tab_root);
133     dp->tab_root = NULL;
134     if (p)
135         dp->tab_root = xstrdup (p);
136 }
137
138 const char *data1_get_tabpath (data1_handle dp)
139 {
140     return dp->tab_path;
141 }
142
143 const char *data1_get_tabroot (data1_handle dp)
144 {
145     return dp->tab_root;
146 }
147
148 FILE *data1_path_fopen (data1_handle dh, const char *file, const char *mode)
149 {
150     FILE *f;
151     const char *path = data1_get_tabpath(dh);
152     const char *root = data1_get_tabroot(dh);
153     if (!path || !*path)
154     {
155         yaz_log(YLOG_DEBUG, "data1_fath_fopen file=%s mode=%s no open",
156                 file, mode);
157         return 0;
158     }
159     yaz_log(YLOG_DEBUG, "data1_fath_fopen path=%s root=%s "
160             "file=%s mode=%s", path ? path : "NULL",
161             root ? root : "NULL", file, mode);
162     f = yaz_fopen(path, file, "r", root);
163     if (!f)
164         yaz_log(YLOG_WARN|YLOG_ERRNO, "Couldn't open %s", file);
165     return f;
166 }
167
168 int data1_is_xmlmode(data1_handle dh)
169 {
170     return 1;
171 }
172 /*
173  * Local variables:
174  * c-basic-offset: 4
175  * c-file-style: "Stroustrup"
176  * indent-tabs-mode: nil
177  * End:
178  * vim: shiftwidth=4 tabstop=8 expandtab
179  */
180