1 /* HomeBank -- Free, easy, personal accounting for everyone.
2 * Copyright (C) 1995-2019 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/>.
31 /* our global datas */
32 extern struct HomeBank
*GLOBALS
;
35 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
37 void da_tag_free(Tag
*item
)
39 DB( g_print("da_tag_free\n") );
42 DB( g_print(" => %d, %s\n", item
->key
, item
->name
) );
50 Tag
*da_tag_malloc(void)
52 DB( g_print("da_tag_malloc\n") );
53 return g_malloc0(sizeof(Tag
));
57 void da_tag_destroy(void)
59 DB( g_print("da_tag_destroy\n") );
60 g_hash_table_destroy(GLOBALS
->h_tag
);
66 DB( g_print("da_tag_new\n") );
67 GLOBALS
->h_tag
= g_hash_table_new_full(g_int_hash
, g_int_equal
, (GDestroyNotify
)g_free
, (GDestroyNotify
)da_tag_free
);
71 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
72 static void da_tag_max_key_ghfunc(gpointer key
, Tag
*item
, guint32
*max_key
)
74 *max_key
= MAX(*max_key
, item
->key
);
77 static gboolean
da_tag_name_grfunc(gpointer key
, Tag
*item
, gchar
*name
)
79 if( name
&& item
->name
)
81 if(!strcasecmp(name
, item
->name
))
90 * Return value: the number of elements
92 guint
da_tag_length(void)
94 return g_hash_table_size(GLOBALS
->h_tag
);
100 * delete an tag from the GHashTable
102 * Return value: TRUE if the key was found and deleted
105 gboolean
da_tag_remove(guint32 key
)
107 DB( g_print("da_tag_remove %d\n", key
) );
109 return g_hash_table_remove(GLOBALS
->h_tag
, &key
);
115 * insert an tag into the GHashTable
117 * Return value: TRUE if inserted
120 gboolean
da_tag_insert(Tag
*item
)
124 DB( g_print("da_tag_insert\n") );
126 new_key
= g_new0(guint32
, 1);
127 *new_key
= item
->key
;
128 g_hash_table_insert(GLOBALS
->h_tag
, new_key
, item
);
137 * append a new tag into the GHashTable
139 * Return value: TRUE if inserted
142 gboolean
da_tag_append(Tag
*item
)
147 DB( g_print("da_tag_append\n") );
149 if( item
->name
!= NULL
)
151 /* ensure no duplicate */
152 //g_strstrip(item->name);
153 existitem
= da_tag_get_by_name( item
->name
);
154 if( existitem
== NULL
)
156 new_key
= g_new0(guint32
, 1);
157 *new_key
= da_tag_get_max_key() + 1;
158 item
->key
= *new_key
;
160 DB( g_print(" -> append id: %d\n", *new_key
) );
162 g_hash_table_insert(GLOBALS
->h_tag
, new_key
, item
);
167 DB( g_print(" -> %s already exist: %d\n", item
->name
, item
->key
) );
173 * da_tag_get_max_key:
175 * Get the biggest key from the GHashTable
177 * Return value: the biggest key value
180 guint32
da_tag_get_max_key(void)
184 g_hash_table_foreach(GLOBALS
->h_tag
, (GHFunc
)da_tag_max_key_ghfunc
, &max_key
);
190 * da_tag_get_by_name:
192 * Get an tag structure by its name
194 * Return value: Tag * or NULL if not found
197 Tag
*da_tag_get_by_name(gchar
*name
)
199 DB( g_print("da_tag_get_by_name\n") );
201 return g_hash_table_find(GLOBALS
->h_tag
, (GHRFunc
)da_tag_name_grfunc
, name
);
209 * Get an tag structure by key
211 * Return value: Tag * or NULL if not found
214 Tag
*da_tag_get(guint32 key
)
216 DB( g_print("da_tag_get_tag\n") );
218 return g_hash_table_lookup(GLOBALS
->h_tag
, &key
);
222 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
225 tags_count(guint32
*tags
)
229 DB( g_print("\n[tags] count\n") );
234 while(*tags
++ != 0 && count
< 32)
241 guint32
*tags_clone(guint32
*tags
)
243 guint32
*newtags
= NULL
;
246 count
= tags_count(tags
);
249 //1501962: we must also copy the final 0
250 newtags
= g_memdup(tags
, (count
+1)*sizeof(guint32
));
258 tags_key_exists(guint32
*tags
, guint32 key
)
261 while(*tags
!= 0 && count
< 32)
273 tags_parse(const gchar
*tagstring
)
276 guint32
*tags
= NULL
;
281 DB( g_print("\n[tags] parse\n") );
285 str_array
= g_strsplit (tagstring
, " ", 0);
286 count
= g_strv_length( str_array
);
287 DB( g_print("- %d tags '%s'\n", count
, tagstring
) );
290 tags
= g_new0(guint32
, count
+ 1);
294 //5.2.3 fixed empty tag
295 if( strlen(str_array
[i
]) == 0 )
298 DB( g_print("- %d search '%s'\n", i
, str_array
[i
]) );
299 tag
= da_tag_get_by_name(str_array
[i
]);
302 Tag
*newtag
= da_tag_malloc();
304 newtag
->name
= g_strdup(str_array
[i
]);
305 da_tag_append(newtag
);
306 tag
= da_tag_get_by_name(str_array
[i
]);
308 DB( g_print("- array add %d '%s'\n", tag
->key
, tag
->name
) );
310 //5.3 fixed duplicate tag in same tags
311 if( tags_key_exists(tags
, tag
->key
) == FALSE
)
317 g_strfreev (str_array
);
325 tags_tostring(guint32
*tags
)
328 gchar
**str_array
, **tptr
;
332 DB( g_print("\n[tags] tostring\n") );
339 count
= tags_count(tags
);
340 str_array
= g_new0(gchar
*, count
+1);
344 tag
= da_tag_get(tags
[i
]);
352 tagstring
= g_strjoinv(" ", str_array
);
359 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
365 da_tag_debug_list_ghfunc(gpointer key
, gpointer value
, gpointer user_data
)
370 DB( g_print(" %d :: %s\n", *id
, item
->name
) );
375 da_tag_debug_list(void)
378 DB( g_print("\n** debug **\n") );
380 g_hash_table_foreach(GLOBALS
->h_tag
, da_tag_debug_list_ghfunc
, NULL
);
382 DB( g_print("\n** end debug **\n") );
388 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
392 tag_rename(Tag
*item
, const gchar
*newname
)
396 gboolean retval
= FALSE
;
398 stripname
= g_strdup(newname
);
399 g_strstrip(stripname
);
401 existitem
= da_tag_get_by_name(stripname
);
403 if( existitem
!= NULL
&& existitem
->key
!= item
->key
)
405 DB( g_print("- error, same name already exist with other key %d <> %d\n",existitem
->key
, item
->key
) );
410 DB( g_print("- renaming\n") );
412 item
->name
= stripname
;
422 tag_glist_name_compare_func(Tag
*a
, Tag
*b
)
424 return hb_string_utf8_compare(a
->name
, b
->name
);
429 tag_glist_key_compare_func(Tag
*a
, Tag
*b
)
431 return a
->key
- b
->key
;
435 GList
*tag_glist_sorted(gint column
)
437 GList
*list
= g_hash_table_get_values(GLOBALS
->h_tag
);
440 return g_list_sort(list
, (GCompareFunc
)tag_glist_key_compare_func
);
442 return g_list_sort(list
, (GCompareFunc
)tag_glist_name_compare_func
);