]> www.fi.muni.cz Git - evince.git/blob - dvi/mdvi-lib/assoc.c
Recent files support.
[evince.git] / dvi / mdvi-lib / assoc.c
1 /*
2  * Copyright (C) 2000, Matias Atria
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #include "mdvi.h"
20 #include "assoc.h"
21 #include "hash.h"
22
23 typedef struct {
24         void    *data;
25         DviFree2Func free_func;
26 } DviAssoc;
27
28 #define MDVI_ASSOC_SIZE 31
29
30 static void assoc_free(DviHashKey key, void *ptr)
31 {
32         DviAssoc *assoc = (DviAssoc *)ptr;
33
34         DEBUG((4, "Destroying association `%s'\n", (char *)key));       
35         if(assoc->free_func)
36                 assoc->free_func(key, assoc->data);
37         xfree(assoc);
38 }
39
40 int     mdvi_assoc_put(DviContext *dvi, char *key, void *data, DviFree2Func f)
41 {
42         DviAssoc *assoc;
43         int     ok;
44                 
45         if(dvi->assoc.buckets == NULL) {
46                 mdvi_hash_create(&dvi->assoc, MDVI_ASSOC_SIZE);
47                 dvi->assoc.hash_free = assoc_free;
48         }
49         assoc = xalloc(DviAssoc);
50         assoc->data = data;
51         assoc->free_func = f;
52
53         ok = mdvi_hash_add(&dvi->assoc, MDVI_KEY(key), 
54                 assoc, MDVI_HASH_UNIQUE);
55         if(ok < 0) {
56                 xfree(assoc);
57                 return -1;
58         }
59         return 0;
60 }
61
62 void    *mdvi_assoc_get(DviContext *dvi, char *key)
63 {
64         DviAssoc *assoc;
65         
66         if(dvi->assoc.buckets == NULL)
67                 return NULL;
68         assoc = (DviAssoc *)mdvi_hash_lookup(&dvi->assoc, MDVI_KEY(key));
69         return assoc ? assoc->data : NULL;
70 }
71
72 void    *mdvi_assoc_del(DviContext *dvi, char *key)
73 {
74         DviAssoc *assoc;
75         void    *ptr;
76         
77         if(dvi->assoc.buckets == NULL)
78                 return NULL;
79         assoc = mdvi_hash_remove(&dvi->assoc, MDVI_KEY(key));
80         if(assoc == NULL)
81                 return NULL;
82         ptr = assoc->data;
83         xfree(assoc);
84         return ptr;
85 }
86
87 void    mdvi_assoc_free(DviContext *dvi, char *key)
88 {
89         if(dvi->assoc.buckets) {
90                 /* this will call `assoc_free' */
91                 mdvi_hash_destroy_key(&dvi->assoc, MDVI_KEY(key));
92         }
93 }
94
95 void    mdvi_assoc_flush(DviContext *dvi)
96 {
97         if(dvi->assoc.buckets)
98                 mdvi_hash_reset(&dvi->assoc, 0);
99 }