Share similar XSLTs within session.
[pazpar2-moved-to-github.git] / src / normalize_cache.c
1 /* This file is part of Pazpar2.
2    Copyright (C) 2006-2009 Index Data
3
4 Pazpar2 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 Pazpar2 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 <string.h>
21
22 #include <yaz/yaz-util.h>
23 #include <yaz/nmem.h>
24
25 #if HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28
29 #include "normalize_cache.h"
30
31 #include "pazpar2_config.h"
32
33 struct cached_item {
34     char *spec;
35     struct cached_item *next;
36     normalize_record_t nt;
37 };
38
39 struct normalize_cache_s {
40     struct cached_item *items;
41     NMEM nmem;
42 };
43
44 normalize_cache_t normalize_cache_create(void)
45 {
46     NMEM nmem = nmem_create();
47     normalize_cache_t nc = nmem_malloc(nmem, sizeof(*nc));
48     nc->nmem = nmem;
49     nc->items = 0;
50     return nc;
51 }
52
53 normalize_record_t normalize_cache_get(normalize_cache_t nc,
54                                        struct conf_service *service,
55                                        const char *spec)
56 {
57     normalize_record_t nt;
58     struct cached_item *ci = nc->items;
59     for (; ci; ci = ci->next)
60         if (!strcmp(spec, ci->spec))
61             return ci->nt;
62
63     nt = normalize_record_create(service, spec);
64     if (nt)
65     {
66         ci = nmem_malloc(nc->nmem, sizeof(*ci));
67         ci->next = nc->items;
68         nc->items = ci;
69         ci->nt = nt;
70         ci->spec = nmem_strdup(nc->nmem, spec);
71     }
72     return nt;
73 }
74
75 void normalize_cache_destroy(normalize_cache_t nc)
76 {
77     if (nc)
78     {
79         struct cached_item *ci = nc->items;
80         for (; ci; ci = ci->next)
81             normalize_record_destroy(ci->nt);
82         nmem_destroy(nc->nmem);
83     }
84 }
85
86 /*
87  * Local variables:
88  * c-basic-offset: 4
89  * c-file-style: "Stroustrup"
90  * indent-tabs-mode: nil
91  * End:
92  * vim: shiftwidth=4 tabstop=8 expandtab
93  */
94