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