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-category.h"
24 /****************************************************************************/
26 /****************************************************************************/
35 /* our global datas */
36 extern struct HomeBank
*GLOBALS
;
38 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
41 da_cat_clone(Category
*src_item
)
43 Category
*new_item
= g_memdup(src_item
, sizeof(Category
));
45 DB( g_print("da_cat_clone\n") );
48 //duplicate the string
49 new_item
->name
= g_strdup(src_item
->name
);
56 da_cat_free(Category
*item
)
58 DB( g_print("da_cat_free\n") );
61 DB( g_print(" => %d, %s\n", item
->key
, item
->name
) );
72 DB( g_print("da_cat_malloc\n") );
73 return g_malloc0(sizeof(Category
));
80 DB( g_print("da_cat_destroy\n") );
81 g_hash_table_destroy(GLOBALS
->h_cat
);
90 DB( g_print("da_cat_new\n") );
91 GLOBALS
->h_cat
= g_hash_table_new_full(g_int_hash
, g_int_equal
, (GDestroyNotify
)g_free
, (GDestroyNotify
)da_cat_free
);
93 // insert our 'no category'
94 item
= da_cat_malloc();
95 item
->name
= g_strdup("");
100 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
105 * Return value: the number of elements
110 return g_hash_table_size(GLOBALS
->h_cat
);
116 * da_cat_remove_grfunc:
118 * GRFunc to get the max id
120 * Return value: TRUE if the key/value must be deleted
124 da_cat_remove_grfunc(gpointer key
, Category
*cat
, guint32
*remkey
)
126 if(cat
->key
== *remkey
|| cat
->parent
== *remkey
)
136 * delete a category from the GHashTable
138 * Return value: TRUE if the key was found and deleted
142 da_cat_remove(guint32 key
)
144 DB( g_print("da_cat_remove %d\n", key
) );
146 return g_hash_table_foreach_remove(GLOBALS
->h_cat
, (GHRFunc
)da_cat_remove_grfunc
, &key
);
152 * insert a category into the GHashTable
154 * Return value: TRUE if inserted
158 da_cat_insert(Category
*item
)
162 DB( g_print("da_cat_insert\n") );
164 new_key
= g_new0(guint32
, 1);
165 *new_key
= item
->key
;
166 g_hash_table_insert(GLOBALS
->h_cat
, new_key
, item
);
175 * append a category into the GHashTable
177 * Return value: TRUE if inserted
181 da_cat_append(Category
*cat
)
187 DB( g_print("da_cat_append\n") );
189 if( cat
->name
!= NULL
)
192 fullname
= da_cat_get_fullname(cat
);
193 existitem
= da_cat_get_by_fullname( fullname
);
196 if( existitem
== NULL
)
198 new_key
= g_new0(guint32
, 1);
199 *new_key
= da_cat_get_max_key() + 1;
202 DB( g_print(" -> insert id: %d\n", *new_key
) );
204 g_hash_table_insert(GLOBALS
->h_cat
, new_key
, cat
);
210 DB( g_print(" -> %s already exist\n", cat
->name
) );
217 * da_cat_max_key_ghfunc:
219 * GHFunc for biggest key
223 da_cat_max_key_ghfunc(gpointer key
, Category
*cat
, guint32
*max_key
)
226 *max_key
= MAX(*max_key
, cat
->key
);
230 * da_cat_get_max_key:
232 * Get the biggest key from the GHashTable
234 * Return value: the biggest key value
238 da_cat_get_max_key(void)
242 g_hash_table_foreach(GLOBALS
->h_cat
, (GHFunc
)da_cat_max_key_ghfunc
, &max_key
);
247 * da_cat_get_fullname:
249 * Get category the fullname 'xxxx:yyyyy'
251 * Return value: the category fullname (free it with g_free)
255 da_cat_get_fullname(Category
*cat
)
259 if( cat
->parent
== 0 )
260 return g_strdup(cat
->name
);
263 parent
= da_cat_get(cat
->parent
);
266 return g_strdup_printf("%s:%s", parent
->name
, cat
->name
);
275 * da_cat_name_grfunc:
277 * GRFunc to get the max id
279 * Return value: TRUE if the key/value pair match our name
283 da_cat_name_grfunc(gpointer key
, Category
*cat
, gchar
*name
)
286 // DB( g_print("%s == %s\n", name, cat->name) );
287 if( name
&& cat
->name
)
289 if(!strcasecmp(name
, cat
->name
))
296 * da_cat_get_key_by_name:
298 * Get a category key by its name
300 * Return value: the category key or -1 if not found
304 da_cat_get_key_by_name(gchar
*name
)
308 DB( g_print("da_cat_get_key_by_name\n") );
310 cat
= g_hash_table_find(GLOBALS
->h_cat
, (GHRFunc
)da_cat_name_grfunc
, name
);
318 * da_cat_get_by_name:
320 * Get a category structure by its name
322 * Return value: Category * or NULL if not found
326 da_cat_get_by_name(gchar
*name
)
328 DB( g_print("da_cat_get_by_name\n") );
330 return g_hash_table_find(GLOBALS
->h_cat
, (GHRFunc
)da_cat_name_grfunc
, name
);
334 /* fullname i.e. car:refuel */
335 struct fullcatcontext
343 da_cat_fullname_grfunc(gpointer key
, Category
*item
, struct fullcatcontext
*ctx
)
346 //DB( g_print("'%s' == '%s'\n", ctx->name, item->name) );
347 if( item
->parent
== ctx
->parent
)
349 if(!strcasecmp(ctx
->name
, item
->name
))
356 da_cat_get_by_fullname(gchar
*fullname
)
358 struct fullcatcontext ctx
;
360 Category
*item
= NULL
;
362 DB( g_print("da_cat_get_by_fullname\n") );
364 typestr
= g_strsplit(fullname
, ":", 2);
365 if( g_strv_length(typestr
) == 2 )
368 ctx
.name
= typestr
[0];
369 DB( g_print(" [x:x] try to find the parent : '%s'\n", typestr
[0]) );
371 Category
*parent
= g_hash_table_find(GLOBALS
->h_cat
, (GHRFunc
)da_cat_fullname_grfunc
, &ctx
);
374 ctx
.parent
= parent
->key
;
375 ctx
.name
= typestr
[1];
377 DB( g_print(" [x:x] and searching sub %d '%s'\n", ctx
.parent
, ctx
.name
) );
379 item
= g_hash_table_find(GLOBALS
->h_cat
, (GHRFunc
)da_cat_fullname_grfunc
, &ctx
);
387 DB( g_print(" [x] try to '%s'\n", fullname
) );
389 item
= g_hash_table_find(GLOBALS
->h_cat
, (GHRFunc
)da_cat_fullname_grfunc
, &ctx
);
394 DB( g_print(" return value %p\n", item
) );
401 * da_cat_append_ifnew_by_fullname:
403 * append a category if it is new by fullname
409 da_cat_append_ifnew_by_fullname(gchar
*fullname
, gboolean imported
)
411 struct fullcatcontext ctx
;
413 Category
*newcat
, *item
, *retval
= NULL
;
416 DB( g_print("da_cat_append_ifnew_by_fullname\n") );
418 DB( g_print(" -> fullname: '%s' %d\n", fullname
, strlen(fullname
)) );
420 if( strlen(fullname
) > 0 )
422 typestr
= g_strsplit(fullname
, ":", 2);
424 /* if we have a subcategory : aaaa:bbb */
425 if( g_strv_length(typestr
) == 2 )
428 ctx
.name
= typestr
[0];
429 DB( g_print(" try to find the parent:'%s'\n", typestr
[0]) );
431 Category
*parent
= g_hash_table_find(GLOBALS
->h_cat
, (GHRFunc
)da_cat_fullname_grfunc
, &ctx
);
434 DB( g_print(" -> not found\n") );
436 // append a new category
437 new_key
= g_new0(guint32
, 1);
438 *new_key
= da_cat_get_max_key() + 1;
440 newcat
= da_cat_malloc();
441 newcat
->key
= *new_key
;
442 newcat
->name
= g_strdup(typestr
[0]);
443 newcat
->imported
= imported
;
447 DB( g_print(" -> insert cat '%s' id: %d\n", newcat
->name
, newcat
->key
) );
449 g_hash_table_insert(GLOBALS
->h_cat
, new_key
, newcat
);
452 ctx
.parent
= parent
->key
;
453 ctx
.name
= typestr
[1];
454 DB( g_print(" searching %d '%s'\n", ctx
.parent
, ctx
.name
) );
456 item
= g_hash_table_find(GLOBALS
->h_cat
, (GHRFunc
)da_cat_fullname_grfunc
, &ctx
);
459 // append a new subcategory
460 new_key
= g_new0(guint32
, 1);
461 *new_key
= da_cat_get_max_key() + 1;
463 newcat
= da_cat_malloc();
464 newcat
->key
= *new_key
;
465 newcat
->parent
= parent
->key
;
466 newcat
->name
= g_strdup(typestr
[1]);
467 newcat
->imported
= imported
;
469 newcat
->flags
|= GF_SUB
;
470 //#1713413 take parent type into account
471 if(parent
->flags
& GF_INCOME
)
472 newcat
->flags
|= GF_INCOME
;
474 DB( g_print(" -> insert subcat '%s' id: %d\n", newcat
->name
, newcat
->key
) );
476 g_hash_table_insert(GLOBALS
->h_cat
, new_key
, newcat
);
483 /* this a single category : aaaa */
487 ctx
.name
= typestr
[0];
488 DB( g_print(" searching %d '%s'\n", ctx
.parent
, ctx
.name
) );
490 item
= g_hash_table_find(GLOBALS
->h_cat
, (GHRFunc
)da_cat_fullname_grfunc
, &ctx
);
493 // append a new category
494 new_key
= g_new0(guint32
, 1);
495 *new_key
= da_cat_get_max_key() + 1;
497 newcat
= da_cat_malloc();
498 newcat
->key
= *new_key
;
499 newcat
->name
= g_strdup(typestr
[0]);
500 newcat
->imported
= imported
;
502 DB( g_print(" -> insert cat '%s' id: %d\n", newcat
->name
, newcat
->key
) );
504 g_hash_table_insert(GLOBALS
->h_cat
, new_key
, newcat
);
524 * Get a category structure by key
526 * Return value: Category * or NULL if not found
530 da_cat_get(guint32 key
)
532 //DB( g_print("da_cat_get\n") );
534 return g_hash_table_lookup(GLOBALS
->h_cat
, &key
);
538 void da_cat_consistency(Category
*item
)
542 if((item
->flags
& GF_SUB
) && item
->key
> 0)
544 //check for existing parent
545 if( da_cat_get(item
->parent
) == NULL
)
547 Category
*parent
= da_cat_append_ifnew_by_fullname ("orphaned", FALSE
);
549 item
->parent
= parent
->key
;
551 g_warning("category consistency: fixed missing parent %d", item
->parent
);
555 // ensure type equal for categories and its children
556 if(!(item
->flags
& GF_SUB
) && item
->key
> 0)
558 isIncome
= (item
->flags
& GF_INCOME
) ? TRUE
: FALSE
;
559 if( category_change_type(item
, isIncome
) > 0 )
561 g_warning("category consistency: fixed type for child");
562 GLOBALS
->changes_count
++;
566 g_strstrip(item
->name
);
571 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
576 da_cat_debug_list_ghfunc(gpointer key
, gpointer value
, gpointer user_data
)
579 Category
*cat
= value
;
581 DB( g_print(" %d :: %s (parent=%d\n", *id
, cat
->name
, cat
->parent
) );
586 da_cat_debug_list(void)
589 DB( g_print("\n** debug **\n") );
591 g_hash_table_foreach(GLOBALS
->h_cat
, da_cat_debug_list_ghfunc
, NULL
);
593 DB( g_print("\n** end debug **\n") );
601 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
603 guint32
category_report_id(guint32 key
, gboolean subcat
)
605 Category
*catentry
= da_cat_get(key
);
612 retval
= (catentry
->flags
& GF_SUB
) ? catentry
->parent
: catentry
->key
;
616 retval
= catentry
->key
;
624 category_delete_unused(void)
628 lcat
= list
= g_hash_table_get_values(GLOBALS
->h_cat
);
631 Category
*entry
= list
->data
;
633 if(entry
->usage_count
<= 0 && entry
->key
> 0)
634 da_cat_remove (entry
->key
);
636 list
= g_list_next(list
);
643 category_fill_usage_count(guint32 kcat
)
645 Category
*cat
= da_cat_get (kcat
);
651 if( cat
->parent
> 0 )
653 parent
= da_cat_get(cat
->parent
);
656 parent
->usage_count
++;
664 category_fill_usage(void)
667 GList
*lst_acc
, *lnk_acc
;
669 GList
*lpay
, *lrul
, *list
;
672 lcat
= list
= g_hash_table_get_values(GLOBALS
->h_cat
);
675 Category
*entry
= list
->data
;
676 entry
->usage_count
= 0;
677 list
= g_list_next(list
);
682 lst_acc
= g_hash_table_get_values(GLOBALS
->h_acc
);
683 lnk_acc
= g_list_first(lst_acc
);
684 while (lnk_acc
!= NULL
)
686 Account
*acc
= lnk_acc
->data
;
688 lnk_txn
= g_queue_peek_head_link(acc
->txn_queue
);
689 while (lnk_txn
!= NULL
)
691 Transaction
*txn
= lnk_txn
->data
;
693 //#1689308 count split as well
694 if( txn
->flags
& OF_SPLIT
)
696 nbsplit
= da_splits_count(txn
->splits
);
697 for(i
=0;i
<nbsplit
;i
++)
699 Split
*split
= txn
->splits
[i
];
701 category_fill_usage_count(split
->kcat
);
705 category_fill_usage_count(txn
->kcat
);
707 lnk_txn
= g_list_next(lnk_txn
);
709 lnk_acc
= g_list_next(lnk_acc
);
711 g_list_free(lst_acc
);
713 lpay
= list
= g_hash_table_get_values(GLOBALS
->h_pay
);
716 Payee
*entry
= list
->data
;
718 category_fill_usage_count(entry
->kcat
);
719 list
= g_list_next(list
);
724 list
= g_list_first(GLOBALS
->arc_list
);
727 Archive
*entry
= list
->data
;
729 //#1689308 count split as well
730 if( entry
->flags
& OF_SPLIT
)
732 nbsplit
= da_splits_count(entry
->splits
);
733 for(i
=0;i
<nbsplit
;i
++)
735 Split
*split
= entry
->splits
[i
];
737 category_fill_usage_count(split
->kcat
);
741 category_fill_usage_count(entry
->kcat
);
743 list
= g_list_next(list
);
747 lrul
= list
= g_hash_table_get_values(GLOBALS
->h_rul
);
750 Assign
*entry
= list
->data
;
752 category_fill_usage_count(entry
->kcat
);
753 list
= g_list_next(list
);
761 category_move(guint32 key1
, guint32 key2
)
763 GList
*lst_acc
, *lnk_acc
;
768 lst_acc
= g_hash_table_get_values(GLOBALS
->h_acc
);
769 lnk_acc
= g_list_first(lst_acc
);
770 while (lnk_acc
!= NULL
)
772 Account
*acc
= lnk_acc
->data
;
774 lnk_txn
= g_queue_peek_head_link(acc
->txn_queue
);
775 while (lnk_txn
!= NULL
)
777 Transaction
*txn
= lnk_txn
->data
;
779 if(txn
->kcat
== key1
)
782 txn
->flags
|= OF_CHANGED
;
785 // move split category #1340142
786 nbsplit
= da_splits_count(txn
->splits
);
787 for(i
=0;i
<nbsplit
;i
++)
789 Split
*split
= txn
->splits
[i
];
791 if( split
->kcat
== key1
)
794 txn
->flags
|= OF_CHANGED
;
798 lnk_txn
= g_list_next(lnk_txn
);
801 lnk_acc
= g_list_next(lnk_acc
);
803 g_list_free(lst_acc
);
806 list
= g_list_first(GLOBALS
->arc_list
);
809 Archive
*entry
= list
->data
;
810 if(entry
->kcat
== key1
)
814 list
= g_list_next(list
);
817 lrul
= list
= g_hash_table_get_values(GLOBALS
->h_rul
);
820 Assign
*entry
= list
->data
;
822 if(entry
->kcat
== key1
)
826 list
= g_list_next(list
);
834 category_rename(Category
*item
, const gchar
*newname
)
836 Category
*parent
, *existitem
;
837 gchar
*fullname
= NULL
;
841 DB( g_print("(category) rename\n") );
843 stripname
= g_strdup(newname
);
844 g_strstrip(stripname
);
846 if( item
->parent
== 0)
847 fullname
= g_strdup(stripname
);
850 parent
= da_cat_get(item
->parent
);
853 fullname
= g_strdup_printf("%s:%s", parent
->name
, stripname
);
857 DB( g_print(" - search: %s\n", fullname
) );
859 existitem
= da_cat_get_by_fullname( fullname
);
861 if( existitem
!= NULL
&& existitem
->key
!= item
->key
)
863 DB( g_print("error, same name already exist with other key %d <> %d\n",existitem
->key
, item
->key
) );
868 DB( g_print(" -renaming\n") );
871 item
->name
= g_strdup(stripname
);
882 static gint
category_glist_name_compare_func(Category
*c1
, Category
*c2
)
884 gchar
*name1
, *name2
;
887 if( c1
!= NULL
&& c2
!= NULL
)
889 name1
= da_cat_get_fullname(c1
);
890 name2
= da_cat_get_fullname(c2
);
892 retval
= hb_string_utf8_compare(name1
, name2
);
901 static gint
category_glist_key_compare_func(Category
*a
, Category
*b
)
903 gint ka
, kb
, retval
= 0;
905 if(a
->parent
== 0 && b
->parent
== a
->key
)
908 if(b
->parent
== 0 && a
->parent
== b
->key
)
912 ka
= a
->parent
!= 0 ? a
->parent
: a
->key
;
913 kb
= b
->parent
!= 0 ? b
->parent
: b
->key
;
930 DB( g_print("compare a=%2d:%2d to b=%2d:%2d :: %d [%s]\n", a
->key
, a
->parent
, b
->key
, b
->parent
, retval
, str
) );
937 GList
*category_glist_sorted(gint column
)
939 GList
*list
= g_hash_table_get_values(GLOBALS
->h_cat
);
942 return g_list_sort(list
, (GCompareFunc
)category_glist_key_compare_func
);
944 return g_list_sort(list
, (GCompareFunc
)category_glist_name_compare_func
);
949 category_load_csv(gchar
*filename
, gchar
**error
)
956 gchar
*lastcatname
= NULL
;
961 const gchar
*encoding
;
963 encoding
= homebank_file_getencoding(filename
);
965 DB( g_print(" -> encoding should be %s\n", encoding
) );
970 io
= g_io_channel_new_file(filename
, "r", NULL
);
974 if( encoding
!= NULL
)
976 g_io_channel_set_encoding(io
, encoding
, NULL
);
983 io_stat
= g_io_channel_read_line(io
, &tmpstr
, NULL
, NULL
, &err
);
985 DB( g_print(" + iostat %d\n", io_stat
) );
987 if( io_stat
== G_IO_STATUS_ERROR
)
989 DB (g_print(" + ERROR %s\n",err
->message
));
992 if( io_stat
== G_IO_STATUS_EOF
)
994 if( io_stat
== G_IO_STATUS_NORMAL
)
998 DB( g_print(" + strip %s\n", tmpstr
) );
999 hb_string_strip_crlf(tmpstr
);
1001 DB( g_print(" + split\n") );
1002 str_array
= g_strsplit (tmpstr
, ";", 3);
1005 if( g_strv_length (str_array
) != 3 )
1007 *error
= _("invalid CSV format");
1009 DB( g_print(" + error %s\n", *error
) );
1013 DB( g_print(" + read %s : %s : %s\n", str_array
[0], str_array
[1], str_array
[2]) );
1016 if( g_str_has_prefix(str_array
[0], "1") )
1018 fullcatname
= g_strdup(str_array
[2]);
1019 g_free(lastcatname
);
1020 lastcatname
= g_strdup(str_array
[2]);
1022 type
= g_str_has_prefix(str_array
[1], "+") ? GF_INCOME
: 0;
1024 DB( g_print(" + type = %d\n", type
) );
1028 if( g_str_has_prefix(str_array
[0], "2") )
1030 fullcatname
= g_strdup_printf("%s:%s", lastcatname
, str_array
[2]);
1033 DB( g_print(" + fullcatname %s\n", fullcatname
) );
1035 item
= da_cat_append_ifnew_by_fullname(fullcatname
, FALSE
);
1037 DB( g_print(" + item %p\n", item
) );
1041 DB( g_print(" + assign flags: '%c'\n", type
) );
1043 item
->flags
|= type
;
1047 g_free(fullcatname
);
1048 g_strfreev (str_array
);
1056 g_io_channel_unref (io
);
1061 g_free(lastcatname
);
1069 category_save_csv(gchar
*filename
, gchar
**error
)
1071 gboolean retval
= FALSE
;
1077 io
= g_io_channel_new_file(filename
, "w", NULL
);
1080 lcat
= list
= category_glist_sorted(1);
1082 while (list
!= NULL
)
1084 Category
*item
= list
->data
;
1090 if( item
->parent
== 0)
1093 type
= (item
->flags
& GF_INCOME
) ? '+' : '-';
1101 outstr
= g_strdup_printf("%c;%c;%s\n", lvel
, type
, item
->name
);
1103 DB( g_print(" + export %s\n", outstr
) );
1105 g_io_channel_write_chars(io
, outstr
, -1, NULL
, NULL
);
1109 list
= g_list_next(list
);
1116 g_io_channel_unref (io
);
1123 gint
category_type_get(Category
*item
)
1125 if( (item
->flags
& (GF_INCOME
)) )
1132 static gint
category_change_type_eval(Category
*item
, gboolean isIncome
)
1134 if( (item
->flags
& (GF_INCOME
)) && !isIncome
)
1140 gint
category_change_type(Category
*item
, gboolean isIncome
)
1145 changes
+= category_change_type_eval(item
, isIncome
);
1147 item
->flags
&= ~(GF_INCOME
); //delete flag
1148 if(isIncome
== TRUE
)
1149 item
->flags
|= GF_INCOME
;
1151 // change also childs
1152 lcat
= list
= g_hash_table_get_values(GLOBALS
->h_cat
);
1153 while (list
!= NULL
)
1155 Category
*child
= list
->data
;
1157 if(child
->parent
== item
->key
)
1159 changes
+= category_change_type_eval(child
, isIncome
);
1160 child
->flags
&= ~(GF_INCOME
); //delete flag
1161 if(isIncome
== TRUE
)
1162 child
->flags
|= GF_INCOME
;
1164 list
= g_list_next(list
);
1177 * category_find_preset:
1179 * find a user language compatible file for category preset
1181 * Return value: a pathname to the file or NULL
1184 gchar
*category_find_preset(gchar
**lang
)
1191 DB( g_print("** category_find_preset **\n") );
1193 langs
= (gchar
**)g_get_language_names ();
1195 DB( g_print(" -> %d languages detected\n", g_strv_length(langs
)) );
1197 for(i
=0;i
<g_strv_length(langs
);i
++)
1199 DB( g_print(" -> %d '%s'\n", i
, langs
[i
]) );
1200 filename
= g_strdup_printf("hb-categories-%s.csv", langs
[i
]);
1201 gchar
*pathfilename
= g_build_filename(homebank_app_get_datas_dir(), filename
, NULL
);
1202 exists
= g_file_test(pathfilename
, G_FILE_TEST_EXISTS
);
1203 DB( g_print(" -> '%s' exists=%d\n", pathfilename
, exists
) );
1208 return pathfilename
;
1211 g_free(pathfilename
);
1214 DB( g_print("return NULL\n") );