Debian Codegraph @

src/main/refcache.c

raw symbols SCIP
1 /*
2 * Object cache for Gutenprint
3 *
4 * Copyright 2017 Robert Krawitz ([email protected])
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20 #include <gutenprint/gutenprint.h>
21 #include "gutenprint-internal.h"
22 #include <gutenprint/gutenprint-intl-internal.h>
23 #include <ctype.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <math.h>
28 #include <errno.h>
29
30 /*
31 * Lists aren't exactly the right data structure for this...if we start
32 * getting into enough items for it to matter, we'll reimplement it them.
33 */
34
35 static stp_list_t *global_cache_list = NULL;
36 static stp_string_list_t *global_cache_names = NULL;
37
38 typedef struct stp_cache
39 {
40 char *name;
41 stp_list_t *cache;
42 stp_string_list_t *cache_items;
43 } stp_refcache_t;
44
45 typedef struct
46 {
47 char *name;
48 void *content;
49 } stp_refcache_item_t;
50
51 static const char *
52 stp_refcache_namefunc(const void *cache)
53 {
54 return ((const stp_refcache_t *) cache)->name;
55 }
56
57 static void
58 stp_refcache_freefunc(void *item)
59 {
60 stp_refcache_t *cache = (stp_refcache_t *) item;
61 STP_SAFE_FREE(cache->name);
62 stp_list_destroy(cache->cache);
63 stp_string_list_destroy(cache->cache_items);
64 STP_SAFE_FREE(item);
65 }
66
67 static const char *
68 stp_refcache_item_namefunc(const void *item)
69 {
70 return ((const stp_refcache_item_t *) item)->name;
71 }
72
73 static void
74 stp_refcache_item_freefunc(void *item)
75 {
76 stp_refcache_item_t *cache_item = (stp_refcache_item_t *) item;
77 STP_SAFE_FREE(cache_item->name);
78 STP_SAFE_FREE(item);
79 }
80
81 static void
82 check_stp_cache(void)
83 {
84 if (! global_cache_list)
85 {
86 global_cache_list = stp_list_create();
87 stp_list_set_namefunc(global_cache_list, stp_refcache_namefunc);
88 stp_list_set_freefunc(global_cache_list, stp_refcache_freefunc);
89 global_cache_names = stp_string_list_create();
90 }
91 }
92
93 int
94 stp_refcache_create(const char *name)
95 {
96 check_stp_cache();
97 if (stp_list_get_item_by_name(global_cache_list, name))
98 return 0;
99 else
100 {
101 stp_refcache_t *cache = stp_zalloc(sizeof(stp_refcache_t));
102 cache->name = stp_strdup(name);
103 cache->cache = stp_list_create();
104 cache->cache_items = stp_string_list_create();
105 stp_list_set_namefunc(cache->cache, stp_refcache_item_namefunc);
106 stp_list_set_freefunc(cache->cache, stp_refcache_item_freefunc);
107 stp_list_item_create(global_cache_list, NULL, cache);
108 stp_string_list_add_string_unsafe(global_cache_names, name, name);
109 return 1;
110 }
111 }
112
113 static stp_refcache_t *
114 find_cache_named(const char *cache)
115 {
116 check_stp_cache();
117 stp_list_item_t *item = stp_list_get_item_by_name(global_cache_list, cache);
118 if (item)
119 return (stp_refcache_t *) stp_list_item_get_data(item);
120 else
121 return NULL;
122 }
123
124 static stp_refcache_t *
125 find_or_create_cache_named(const char *cache)
126 {
127 check_stp_cache();
128 stp_list_item_t *item = stp_list_get_item_by_name(global_cache_list, cache);
129 if (!item)
130 {
131 stp_refcache_create(cache);
132 item = stp_list_get_item_by_name(global_cache_list, cache);
133 }
134 return (stp_refcache_t *) stp_list_item_get_data(item);
135 }
136
137 void *
138 stp_refcache_find_item(const char *cache, const char *item)
139 {
140 stp_refcache_t *cache_impl = find_cache_named(cache);
141 if (cache_impl)
142 {
143 stp_list_item_t *item_impl =
144 stp_list_get_item_by_name(cache_impl->cache, item);
145 if (item_impl)
146 return ((stp_refcache_item_t *)stp_list_item_get_data(item_impl))->content;
147 }
148 return NULL;
149 }
150
151 static void
152 add_item_to_cache(stp_refcache_t *cache, const char *item, void *data)
153 {
154 stp_refcache_item_t *item_impl = stp_zalloc(sizeof(stp_refcache_item_t));
155 item_impl->name = stp_strdup(item);
156 item_impl->content = data;
157 stp_list_item_create(cache->cache, NULL, item_impl);
158 stp_string_list_add_string_unsafe(cache->cache_items, item, item);
159 }
160
161 int
162 stp_refcache_add_item(const char *cache, const char *item, void *data)
163 {
164 stp_refcache_t *cache_impl = find_or_create_cache_named(cache);
165 if (!stp_list_get_item_by_name(cache_impl->cache, item))
166 {
167 add_item_to_cache(cache_impl, item, data);
168 return 1;
169 }
170 return 0;
171 }
172
173 void
174 stp_refcache_remove_item(const char *cache, const char *item)
175 {
176 stp_refcache_t *cache_impl = find_cache_named(cache);
177 if (cache_impl)
178 {
179 stp_list_item_t *item_impl =
180 stp_list_get_item_by_name(cache_impl->cache, item);
181 if (item_impl)
182 {
183 stp_list_item_destroy(cache_impl->cache, item_impl);
184 stp_string_list_remove_string(cache_impl->cache_items, item);
185 }
186 }
187 }
188
189 void
190 stp_refcache_replace_item(const char *cache, const char *item, void *data)
191 {
192 stp_refcache_t *cache_impl = find_or_create_cache_named(cache);
193 stp_list_item_t *item_item =
194 stp_list_get_item_by_name(cache_impl->cache, item);
195 if (item_item)
196 {
197 stp_refcache_item_t *item_impl =
198 (stp_refcache_item_t *) stp_list_item_get_data(item_item);
199 item_impl->content = data;
200 }
201 else
202 {
203 add_item_to_cache(cache_impl, item, data);
204 }
205 }
206
207 void
208 stp_refcache_destroy(const char *cache)
209 {
210 check_stp_cache();
211 stp_list_item_t *item = stp_list_get_item_by_name(global_cache_list, cache);
212 if (item)
213 {
214 stp_list_item_destroy(global_cache_list, item);
215 stp_string_list_remove_string(global_cache_names, cache);
216 }
217 }
218
219 const stp_string_list_t *
220 stp_refcache_list_caches(void)
221 {
222 check_stp_cache();
223 return global_cache_names;
224 }
225
226 const stp_string_list_t *
227 stp_refcache_list_cache_items(const char *cache)
228 {
229 stp_refcache_t *cache_impl = find_cache_named(cache);
230 return cache_impl ? cache_impl->cache_items : NULL;
231 }