1 /* HomeBank -- Free, easy, personal accounting for everyone.
2 * Copyright (C) 1995-2017 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"
34 /* our global datas */
35 extern struct HomeBank
*GLOBALS
;
38 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
41 da_asg_free(Assign
*item
)
43 DB( g_print("da_asg_free\n") );
46 DB( g_print(" => %d, %s\n", item
->key
, item
->text
) );
57 DB( g_print("da_asg_malloc\n") );
58 return rc_alloc(sizeof(Assign
));
65 DB( g_print("da_asg_destroy\n") );
66 g_hash_table_destroy(GLOBALS
->h_rul
);
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
);
78 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
79 static void da_asg_max_key_ghfunc(gpointer key
, Assign
*item
, guint32
*max_key
)
81 *max_key
= MAX(*max_key
, item
->key
);
84 static gboolean
da_asg_name_grfunc(gpointer key
, Assign
*item
, gchar
*name
)
86 if( name
&& item
->text
)
88 if(!strcasecmp(name
, item
->text
))
97 * Return value: the number of elements
102 return g_hash_table_size(GLOBALS
->h_rul
);
108 * delete an rul from the GHashTable
110 * Return value: TRUE if the key was found and deleted
114 da_asg_remove(guint32 key
)
116 DB( g_print("da_asg_remove %d\n", key
) );
118 return g_hash_table_remove(GLOBALS
->h_rul
, &key
);
124 * insert an rul into the GHashTable
126 * Return value: TRUE if inserted
130 da_asg_insert(Assign
*item
)
134 DB( g_print("da_asg_insert\n") );
136 new_key
= g_new0(guint32
, 1);
137 *new_key
= item
->key
;
138 g_hash_table_insert(GLOBALS
->h_rul
, new_key
, item
);
147 * append a new rul into the GHashTable
149 * Return value: TRUE if inserted
153 da_asg_append(Assign
*item
)
158 DB( g_print("da_asg_append\n") );
160 DB( g_print(" -> try append: %s\n", item
->text
) );
162 if( item
->text
!= NULL
)
164 /* ensure no duplicate */
165 existitem
= da_asg_get_by_name( item
->text
);
166 if( existitem
== NULL
)
168 new_key
= g_new0(guint32
, 1);
169 *new_key
= da_asg_get_max_key() + 1;
170 item
->key
= *new_key
;
172 DB( g_print(" -> append id: %d\n", *new_key
) );
174 g_hash_table_insert(GLOBALS
->h_rul
, new_key
, item
);
179 DB( g_print(" -> %s already exist: %d\n", item
->text
, item
->key
) );
185 * da_asg_get_max_key:
187 * Get the biggest key from the GHashTable
189 * Return value: the biggest key value
193 da_asg_get_max_key(void)
197 g_hash_table_foreach(GLOBALS
->h_rul
, (GHFunc
)da_asg_max_key_ghfunc
, &max_key
);
205 * da_asg_get_by_name:
207 * Get an rul structure by its name
209 * Return value: rul * or NULL if not found
213 da_asg_get_by_name(gchar
*name
)
215 DB( g_print("da_asg_get_by_name\n") );
217 return g_hash_table_find(GLOBALS
->h_rul
, (GHRFunc
)da_asg_name_grfunc
, name
);
225 * Get an rul structure by key
227 * Return value: rul * or NULL if not found
231 da_asg_get(guint32 key
)
233 DB( g_print("da_asg_get_rul\n") );
235 return g_hash_table_lookup(GLOBALS
->h_rul
, &key
);
240 assign_glist_key_compare_func(Assign
*a
, Assign
*b
)
242 return a
->key
- b
->key
;
246 GList
*assign_glist_sorted(gint column
)
248 GList
*list
= g_hash_table_get_values(GLOBALS
->h_rul
);
251 return g_list_sort(list
, (GCompareFunc
)assign_glist_key_compare_func
);
253 // return g_list_sort(list, (GCompareFunc)assign_glist_name_compare_func);
259 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
264 da_asg_debug_list_ghfunc(gpointer key
, gpointer value
, gpointer user_data
)
267 Assign
*item
= value
;
269 DB( g_print(" %d :: %s\n", *id
, item
->text
) );
274 da_asg_debug_list(void)
277 DB( g_print("\n** debug **\n") );
279 g_hash_table_foreach(GLOBALS
->h_rul
, da_asg_debug_list_ghfunc
, NULL
);
281 DB( g_print("\n** end debug **\n") );