add gitignore
[chaz/homebank] / src / hb-assign.c
1 /* HomeBank -- Free, easy, personal accounting for everyone.
2 * Copyright (C) 1995-2014 Maxime DOYEN
3 *
4 * This file is part of HomeBank.
5 *
6 * HomeBank is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * HomeBank is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License 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 <http://www.gnu.org/licenses/>.
18 */
19
20 #include "homebank.h"
21 #include "hb-assign.h"
22
23 #define MYDEBUG 0
24
25 #if MYDEBUG
26 #define DB(x) (x);
27 #else
28 #define DB(x);
29 #endif
30
31 /* our global datas */
32 extern struct HomeBank *GLOBALS;
33
34
35 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
36
37 void
38 da_asg_free(Assign *item)
39 {
40 DB( g_print("da_asg_free\n") );
41 if(item != NULL)
42 {
43 DB( g_print(" => %d, %s\n", item->key, item->name) );
44
45 g_free(item->name);
46 g_free(item);
47 }
48 }
49
50
51 Assign *
52 da_asg_malloc(void)
53 {
54 DB( g_print("da_asg_malloc\n") );
55 return g_malloc0(sizeof(Assign));
56 }
57
58
59 void
60 da_asg_destroy(void)
61 {
62 DB( g_print("da_asg_destroy\n") );
63 g_hash_table_destroy(GLOBALS->h_rul);
64 }
65
66
67 void
68 da_asg_new(void)
69 {
70 DB( g_print("da_asg_new\n") );
71 GLOBALS->h_rul = g_hash_table_new_full(g_int_hash, g_int_equal, (GDestroyNotify)g_free, (GDestroyNotify)da_asg_free);
72 }
73
74
75 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
76 static void da_asg_max_key_ghfunc(gpointer key, Assign *item, guint32 *max_key)
77 {
78 *max_key = MAX(*max_key, item->key);
79 }
80
81 static gboolean da_asg_name_grfunc(gpointer key, Assign *item, gchar *name)
82 {
83 if( name && item->name )
84 {
85 if(!strcasecmp(name, item->name))
86 return TRUE;
87 }
88 return FALSE;
89 }
90
91 /**
92 * da_asg_length:
93 *
94 * Return value: the number of elements
95 */
96 guint
97 da_asg_length(void)
98 {
99 return g_hash_table_size(GLOBALS->h_rul);
100 }
101
102 /**
103 * da_asg_remove:
104 *
105 * remove an rul from the GHashTable
106 *
107 * Return value: TRUE if the key was found and removed
108 *
109 */
110 gboolean
111 da_asg_remove(guint32 key)
112 {
113 DB( g_print("da_asg_remove %d\n", key) );
114
115 return g_hash_table_remove(GLOBALS->h_rul, &key);
116 }
117
118 /**
119 * da_asg_insert:
120 *
121 * insert an rul into the GHashTable
122 *
123 * Return value: TRUE if inserted
124 *
125 */
126 gboolean
127 da_asg_insert(Assign *item)
128 {
129 guint32 *new_key;
130
131 DB( g_print("da_asg_insert\n") );
132
133 new_key = g_new0(guint32, 1);
134 *new_key = item->key;
135 g_hash_table_insert(GLOBALS->h_rul, new_key, item);
136
137 return TRUE;
138 }
139
140
141 /**
142 * da_asg_append:
143 *
144 * append a new rul into the GHashTable
145 *
146 * Return value: TRUE if inserted
147 *
148 */
149 gboolean
150 da_asg_append(Assign *item)
151 {
152 Assign *existitem;
153 guint32 *new_key;
154
155 DB( g_print("da_asg_append\n") );
156
157 DB( g_print(" -> try append: %s\n", item->name) );
158
159 if( item->name != NULL )
160 {
161 /* ensure no duplicate */
162 existitem = da_asg_get_by_name( item->name );
163 if( existitem == NULL )
164 {
165 new_key = g_new0(guint32, 1);
166 *new_key = da_asg_get_max_key() + 1;
167 item->key = *new_key;
168
169 DB( g_print(" -> append id: %d\n", *new_key) );
170
171 g_hash_table_insert(GLOBALS->h_rul, new_key, item);
172 return TRUE;
173 }
174 }
175
176 DB( g_print(" -> %s already exist: %d\n", item->name, item->key) );
177
178 return FALSE;
179 }
180
181 /**
182 * da_asg_get_max_key:
183 *
184 * Get the biggest key from the GHashTable
185 *
186 * Return value: the biggest key value
187 *
188 */
189 guint32
190 da_asg_get_max_key(void)
191 {
192 guint32 max_key = 0;
193
194 g_hash_table_foreach(GLOBALS->h_rul, (GHFunc)da_asg_max_key_ghfunc, &max_key);
195 return max_key;
196 }
197
198
199
200
201 /**
202 * da_asg_get_by_name:
203 *
204 * Get an rul structure by its name
205 *
206 * Return value: rul * or NULL if not found
207 *
208 */
209 Assign *
210 da_asg_get_by_name(gchar *name)
211 {
212 DB( g_print("da_asg_get_by_name\n") );
213
214 return g_hash_table_find(GLOBALS->h_rul, (GHRFunc)da_asg_name_grfunc, name);
215 }
216
217
218
219 /**
220 * da_asg_get:
221 *
222 * Get an rul structure by key
223 *
224 * Return value: rul * or NULL if not found
225 *
226 */
227 Assign *
228 da_asg_get(guint32 key)
229 {
230 DB( g_print("da_asg_get_rul\n") );
231
232 return g_hash_table_lookup(GLOBALS->h_rul, &key);
233 }
234
235
236 static gint
237 assign_glist_key_compare_func(Assign *a, Assign *b)
238 {
239 return a->key - b->key;
240 }
241
242
243 GList *assign_glist_sorted(gint column)
244 {
245 GList *list = g_hash_table_get_values(GLOBALS->h_rul);
246
247 //if(column == 0)
248 return g_list_sort(list, (GCompareFunc)assign_glist_key_compare_func);
249 //else
250 // return g_list_sort(list, (GCompareFunc)assign_glist_name_compare_func);
251 }
252
253
254
255
256 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
257
258 #if MYDEBUG
259
260 static void
261 da_asg_debug_list_ghfunc(gpointer key, gpointer value, gpointer user_data)
262 {
263 guint32 *id = key;
264 Assign *item = value;
265
266 DB( g_print(" %d :: %s\n", *id, item->name) );
267
268 }
269
270 static void
271 da_asg_debug_list(void)
272 {
273
274 DB( g_print("\n** debug **\n") );
275
276 g_hash_table_foreach(GLOBALS->h_rul, da_asg_debug_list_ghfunc, NULL);
277
278 DB( g_print("\n** end debug **\n") );
279
280 }
281
282 #endif
283
This page took 0.051161 seconds and 4 git commands to generate.