root/liboggz/trunk/src/liboggz/oggz_table.c

Revision 714, 3.9 kB (checked in by conrad, 4 years ago)

cleaning up -- make sure config.h is included in all .c files

Line 
1 /*
2    Copyright (C) 2003 Commonwealth Scientific and Industrial Research
3    Organisation (CSIRO) Australia
4
5    Redistribution and use in source and binary forms, with or without
6    modification, are permitted provided that the following conditions
7    are met:
8
9    - Redistributions of source code must retain the above copyright
10    notice, this list of conditions and the following disclaimer.
11
12    - Redistributions in binary form must reproduce the above copyright
13    notice, this list of conditions and the following disclaimer in the
14    documentation and/or other materials provided with the distribution.
15
16    - Neither the name of CSIRO Australia nor the names of its
17    contributors may be used to endorse or promote products derived from
18    this software without specific prior written permission.
19
20    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21    ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23    PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE ORGANISATION OR
24    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include "config.h"
34
35 #include <stdlib.h>
36 #include "oggz_macros.h"
37 #include "oggz_vector.h"
38
39 typedef struct _OggzTable OggzTable;
40
41 struct _OggzTable {
42   OggzVector * keys;
43   OggzVector * data;
44 };
45
46 OggzTable *
47 oggz_table_new (void)
48 {
49   OggzTable * table;
50
51   table = oggz_malloc (sizeof (OggzTable));
52   table->keys = oggz_vector_new ();
53   table->data = oggz_vector_new ();
54
55   return table;
56 }
57
58 void
59 oggz_table_delete (OggzTable * table)
60 {
61   if (table == NULL) return;
62
63   oggz_vector_delete (table->keys);
64   oggz_vector_delete (table->data);
65   oggz_free (table);
66 }
67
68 void *
69 oggz_table_lookup (OggzTable * table, long key)
70 {
71   int i, size;
72
73   size = oggz_vector_size (table->keys);
74   for (i = 0; i < size; i++) {
75     if (oggz_vector_nth_l (table->keys, i) == key) {
76       return oggz_vector_nth_p (table->data, i);
77     }
78   }
79
80   return NULL;
81 }
82
83 void *
84 oggz_table_insert (OggzTable * table, long key, void * data)
85 {
86   void * old_data;
87
88   if ((old_data = oggz_table_lookup (table, key)) != NULL) {
89     if (oggz_vector_remove_l (table->keys, key) == NULL)
90       return NULL;
91
92     if (oggz_vector_remove_p (table->data, old_data) == NULL) {
93       /* XXX: This error condition can only happen if the previous
94        * removal succeeded, and this removal failed, ie. there was
95        * an error reallocing table->data->data downwards. */
96       return NULL;
97     }
98   }
99
100   if (oggz_vector_insert_l (table->keys, key) == -1)
101     return NULL;
102  
103   if (oggz_vector_insert_p (table->data, data) == NULL) {
104     oggz_vector_remove_l (table->keys, key);
105     return NULL;
106   }
107
108   return data;
109 }
110
111 int
112 oggz_table_remove (OggzTable * table, long key)
113 {
114   void * old_data;
115
116   if ((old_data = oggz_table_lookup (table, key)) != NULL) {
117     if (oggz_vector_remove_l (table->keys, key) == NULL)
118       return -1;
119
120     if (oggz_vector_remove_p (table->data, old_data) == NULL) {
121       /* XXX: This error condition can only happen if the previous
122        * removal succeeded, and this removal failed, ie. there was
123        * an error reallocing table->data->data downwards. */
124       return -1;
125     }
126   }
127  
128   return 0;
129 }
130
131 int
132 oggz_table_size (OggzTable * table)
133 {
134   if (table == NULL) return 0;
135   return oggz_vector_size (table->data);
136 }
137
138 void *
139 oggz_table_nth (OggzTable * table, int n, long * key)
140 {
141   if (table == NULL) return NULL;
142   if (key) *key = oggz_vector_nth_l (table->keys, n);
143   return oggz_vector_nth_p (table->data, n);
144 }
Note: See TracBrowser for help on using the browser.