1 /* HomeBank -- Free, easy, personal accounting for everyone.
2 * Copyright (C) 1995-2018 Maxime DOYEN
4 * This file is part of HomeBank.
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.
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.
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/>.
21 #include "hb-assign.h"
31 /* our global datas */
32 extern struct HomeBank
*GLOBALS
;
35 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
38 da_asg_free(Assign
*item
)
40 DB( g_print("da_asg_free\n") );
43 DB( g_print(" => %d, %s\n", item
->key
, item
->text
) );
54 DB( g_print("da_asg_malloc\n") );
55 return g_malloc0(sizeof(Assign
));
62 DB( g_print("da_asg_destroy\n") );
63 g_hash_table_destroy(GLOBALS
->h_rul
);
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
);
75 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
76 static void da_asg_max_key_ghfunc(gpointer key
, Assign
*item
, guint32
*max_key
)
78 *max_key
= MAX(*max_key
, item
->key
);
81 static gboolean
da_asg_name_grfunc(gpointer key
, Assign
*item
, gchar
*name
)
83 if( name
&& item
->text
)
85 if(!strcasecmp(name
, item
->text
))
94 * Return value: the number of elements
99 return g_hash_table_size(GLOBALS
->h_rul
);
105 * delete an rul from the GHashTable
107 * Return value: TRUE if the key was found and deleted
111 da_asg_remove(guint32 key
)
113 DB( g_print("da_asg_remove %d\n", key
) );
115 return g_hash_table_remove(GLOBALS
->h_rul
, &key
);
121 * insert an rul into the GHashTable
123 * Return value: TRUE if inserted
127 da_asg_insert(Assign
*item
)
131 DB( g_print("da_asg_insert\n") );
133 new_key
= g_new0(guint32
, 1);
134 *new_key
= item
->key
;
135 g_hash_table_insert(GLOBALS
->h_rul
, new_key
, item
);
144 * append a new rul into the GHashTable
146 * Return value: TRUE if inserted
150 da_asg_append(Assign
*item
)
155 DB( g_print("da_asg_append\n") );
157 DB( g_print(" -> try append: %s\n", item
->text
) );
159 if( item
->text
!= NULL
)
161 /* ensure no duplicate */
162 existitem
= da_asg_get_by_name( item
->text
);
163 if( existitem
== NULL
)
165 new_key
= g_new0(guint32
, 1);
166 *new_key
= da_asg_get_max_key() + 1;
167 item
->key
= *new_key
;
169 DB( g_print(" -> append id: %d\n", *new_key
) );
171 g_hash_table_insert(GLOBALS
->h_rul
, new_key
, item
);
176 DB( g_print(" -> %s already exist: %d\n", item
->text
, item
->key
) );
182 * da_asg_get_max_key:
184 * Get the biggest key from the GHashTable
186 * Return value: the biggest key value
190 da_asg_get_max_key(void)
194 g_hash_table_foreach(GLOBALS
->h_rul
, (GHFunc
)da_asg_max_key_ghfunc
, &max_key
);
202 * da_asg_get_by_name:
204 * Get an rul structure by its name
206 * Return value: rul * or NULL if not found
210 da_asg_get_by_name(gchar
*name
)
212 DB( g_print("da_asg_get_by_name\n") );
214 return g_hash_table_find(GLOBALS
->h_rul
, (GHRFunc
)da_asg_name_grfunc
, name
);
222 * Get an rul structure by key
224 * Return value: rul * or NULL if not found
228 da_asg_get(guint32 key
)
230 DB( g_print("da_asg_get_rul\n") );
232 return g_hash_table_lookup(GLOBALS
->h_rul
, &key
);
237 assign_glist_key_compare_func(Assign
*a
, Assign
*b
)
239 return a
->key
- b
->key
;
243 GList
*assign_glist_sorted(gint column
)
245 GList
*list
= g_hash_table_get_values(GLOBALS
->h_rul
);
248 return g_list_sort(list
, (GCompareFunc
)assign_glist_key_compare_func
);
250 // return g_list_sort(list, (GCompareFunc)assign_glist_name_compare_func);
256 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
261 da_asg_debug_list_ghfunc(gpointer key
, gpointer value
, gpointer user_data
)
264 Assign
*item
= value
;
266 DB( g_print(" %d :: %s\n", *id
, item
->text
) );
271 da_asg_debug_list(void)
274 DB( g_print("\n** debug **\n") );
276 g_hash_table_foreach(GLOBALS
->h_rul
, da_asg_debug_list_ghfunc
, NULL
);
278 DB( g_print("\n** end debug **\n") );