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