LCOV - code coverage report
Current view: top level - libgnucash/backend/xml - gnc-budget-xml-v2.cpp (source / functions) Coverage Total Hit
Test: gnucash.info Lines: 67.7 % 65 44
Test Date: 2025-02-07 16:25:45 Functions: 81.8 % 11 9
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /*
       2                 :             :  * gnc-budget-xml-v2.c -- budget xml i/o implementation
       3                 :             :  *
       4                 :             :  * Copyright (C) 2005 Chris Shoemaker <c.shoemaker@cox.net>
       5                 :             :  *
       6                 :             :  * This program is free software; you can redistribute it and/or
       7                 :             :  * modify it under the terms of the GNU General Public License as
       8                 :             :  * published by the Free Software Foundation; either version 2 of
       9                 :             :  * the License, or (at your option) any later version.
      10                 :             :  *
      11                 :             :  * This program 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, contact:
      18                 :             :  *
      19                 :             :  * Free Software Foundation           Voice:  +1-617-542-5942
      20                 :             :  * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652
      21                 :             :  * Boston, MA  02110-1301,  USA       gnu@gnu.org
      22                 :             :  */
      23                 :             : #include <glib.h>
      24                 :             : 
      25                 :             : #include <config.h>
      26                 :             : #include <stdlib.h>
      27                 :             : #include <string.h>
      28                 :             : 
      29                 :             : #include "gnc-xml-helper.h"
      30                 :             : #include "sixtp.h"
      31                 :             : #include "sixtp-utils.h"
      32                 :             : #include "sixtp-parsers.h"
      33                 :             : #include "sixtp-utils.h"
      34                 :             : #include "sixtp-dom-parsers.h"
      35                 :             : #include "sixtp-dom-generators.h"
      36                 :             : 
      37                 :             : #include "gnc-xml.h"
      38                 :             : #include "io-gncxml-gen.h"
      39                 :             : #include "io-gncxml-v2.h"
      40                 :             : 
      41                 :             : static QofLogModule log_module = GNC_MOD_IO;
      42                 :             : 
      43                 :             : const gchar* budget_version_string = "2.0.0";
      44                 :             : 
      45                 :             : /* ids */
      46                 :             : #define gnc_budget_string       "gnc:budget"
      47                 :             : #define bgt_id_string           "bgt:id"
      48                 :             : #define bgt_name_string         "bgt:name"
      49                 :             : #define bgt_description_string  "bgt:description"
      50                 :             : #define bgt_num_periods_string  "bgt:num-periods"
      51                 :             : #define bgt_recurrence_string   "bgt:recurrence"
      52                 :             : #define bgt_slots_string        "bgt:slots"
      53                 :             : 
      54                 :             : xmlNodePtr
      55                 :           0 : gnc_budget_dom_tree_create (GncBudget* bgt)
      56                 :             : {
      57                 :             :     xmlNodePtr ret;
      58                 :             : 
      59                 :           0 :     ENTER ("(budget=%p)", bgt);
      60                 :             : 
      61                 :           0 :     ret = xmlNewNode (NULL, BAD_CAST gnc_budget_string);
      62                 :           0 :     xmlSetProp (ret, BAD_CAST "version", BAD_CAST budget_version_string);
      63                 :             : 
      64                 :             :     /* field: GncGUID */
      65                 :           0 :     xmlAddChild (ret, guid_to_dom_tree (bgt_id_string,
      66                 :             :                                         gnc_budget_get_guid (bgt)));
      67                 :             :     /* field: char* name */
      68                 :           0 :     xmlAddChild (ret, text_to_dom_tree (bgt_name_string,
      69                 :             :                                         gnc_budget_get_name (bgt)));
      70                 :             :     /* field: char* description */
      71                 :           0 :     xmlAddChild (ret, text_to_dom_tree (bgt_description_string,
      72                 :             :                                         gnc_budget_get_description (bgt)));
      73                 :             :     /* field: guint num_periods */
      74                 :           0 :     xmlAddChild (ret, guint_to_dom_tree (bgt_num_periods_string,
      75                 :             :                                          gnc_budget_get_num_periods (bgt)));
      76                 :             :     /* field: Recurrence*  */
      77                 :           0 :     xmlAddChild (ret, recurrence_to_dom_tree (bgt_recurrence_string,
      78                 :             :                                               gnc_budget_get_recurrence (bgt)));
      79                 :             :     /* xmlAddChild won't do anything with a NULL, so tests are superfluous. */
      80                 :           0 :     xmlAddChild (ret, qof_instance_slots_to_dom_tree (bgt_slots_string,
      81                 :           0 :                                                       QOF_INSTANCE (bgt)));
      82                 :             : 
      83                 :           0 :     LEAVE (" ");
      84                 :           0 :     return ret;
      85                 :             : }
      86                 :             : 
      87                 :             : /***********************************************************************/
      88                 :             : static inline gboolean
      89                 :           2 : set_string (xmlNodePtr node, GncBudget* bgt,
      90                 :             :             void (*func) (GncBudget* bgt, const gchar* txt))
      91                 :             : {
      92                 :           2 :     gchar* txt = dom_tree_to_text (node);
      93                 :           2 :     g_return_val_if_fail (txt, FALSE);
      94                 :             : 
      95                 :           2 :     func (bgt, txt);
      96                 :           2 :     g_free (txt);
      97                 :           2 :     return TRUE;
      98                 :             : }
      99                 :             : 
     100                 :             : static gboolean
     101                 :           1 : budget_id_handler (xmlNodePtr node, gpointer bgt)
     102                 :             : {
     103                 :             :     GncGUID* guid;
     104                 :             : 
     105                 :           1 :     guid = dom_tree_to_guid (node);
     106                 :           1 :     g_return_val_if_fail (guid, FALSE);
     107                 :           1 :     qof_instance_set_guid (QOF_INSTANCE (bgt), guid);
     108                 :           1 :     guid_free (guid);
     109                 :           1 :     return TRUE;
     110                 :             : }
     111                 :             : 
     112                 :             : static gboolean
     113                 :           1 : budget_name_handler (xmlNodePtr node, gpointer bgt)
     114                 :             : {
     115                 :           1 :     return set_string (node, GNC_BUDGET (bgt), gnc_budget_set_name);
     116                 :             : }
     117                 :             : 
     118                 :             : static gboolean
     119                 :           1 : budget_description_handler (xmlNodePtr node, gpointer bgt)
     120                 :             : {
     121                 :           1 :     return set_string (node, GNC_BUDGET (bgt), gnc_budget_set_description);
     122                 :             : }
     123                 :             : 
     124                 :             : static gboolean
     125                 :           1 : budget_num_periods_handler (xmlNodePtr node, gpointer bgt)
     126                 :             : {
     127                 :             :     guint num_periods;
     128                 :             : 
     129                 :           1 :     if (dom_tree_to_guint (node, &num_periods))
     130                 :             :     {
     131                 :           1 :         gnc_budget_set_num_periods (GNC_BUDGET (bgt), num_periods);
     132                 :           1 :         return TRUE;
     133                 :             :     }
     134                 :             :     else
     135                 :           0 :         return FALSE;
     136                 :             : }
     137                 :             : 
     138                 :             : static gboolean
     139                 :           1 : budget_recurrence_handler (xmlNodePtr node, gpointer bgt)
     140                 :             : {
     141                 :             :     Recurrence* r;
     142                 :             : 
     143                 :           1 :     if ((r = dom_tree_to_recurrence (node)) == NULL)
     144                 :           0 :         return FALSE;
     145                 :             : 
     146                 :           1 :     gnc_budget_set_recurrence (GNC_BUDGET (bgt), r);
     147                 :           1 :     g_free (r);
     148                 :           1 :     return TRUE;
     149                 :             : }
     150                 :             : 
     151                 :             : static gboolean
     152                 :           0 : budget_slots_handler (xmlNodePtr node, gpointer bgt)
     153                 :             : {
     154                 :           0 :     return dom_tree_create_instance_slots (node, QOF_INSTANCE (bgt));
     155                 :             : }
     156                 :             : 
     157                 :             : static struct dom_tree_handler budget_handlers[] =
     158                 :             : {
     159                 :             :     { bgt_id_string, budget_id_handler, 1, 0 },
     160                 :             :     { bgt_name_string, budget_name_handler, 0, 0 },
     161                 :             :     { bgt_description_string, budget_description_handler, 0, 0 },
     162                 :             :     { bgt_num_periods_string, budget_num_periods_handler, 1, 0 },
     163                 :             :     { bgt_recurrence_string, budget_recurrence_handler, 1, 0 },
     164                 :             :     { bgt_slots_string, budget_slots_handler, 0, 0},
     165                 :             :     { NULL, 0, 0, 0 }
     166                 :             : };
     167                 :             : 
     168                 :             : static gboolean
     169                 :          10 : gnc_budget_end_handler (gpointer data_for_children,
     170                 :             :                         GSList* data_from_children, GSList* sibling_data,
     171                 :             :                         gpointer parent_data, gpointer global_data,
     172                 :             :                         gpointer* result, const gchar* tag)
     173                 :             : {
     174                 :             :     GncBudget* bgt;
     175                 :          10 :     xmlNodePtr tree = (xmlNodePtr)data_for_children;
     176                 :          10 :     gxpf_data* gdata = (gxpf_data*)global_data;
     177                 :          10 :     QofBook* book = static_cast<decltype (book)> (gdata->bookdata);
     178                 :             : 
     179                 :          10 :     if (parent_data)
     180                 :             :     {
     181                 :           9 :         return TRUE;
     182                 :             :     }
     183                 :             : 
     184                 :             :     /* OK.  For some messed up reason this is getting called again with a
     185                 :             :        NULL tag.  So we ignore those cases */
     186                 :           1 :     if (!tag)
     187                 :             :     {
     188                 :           0 :         return TRUE;
     189                 :             :     }
     190                 :             : 
     191                 :           1 :     g_return_val_if_fail (tree, FALSE);
     192                 :             : 
     193                 :           1 :     bgt = dom_tree_to_budget (tree, book);
     194                 :           1 :     xmlFreeNode (tree);
     195                 :           1 :     if (bgt != NULL)
     196                 :             :     {
     197                 :             :         /* ends up calling book_callback */
     198                 :           1 :         gdata->cb (tag, gdata->parsedata, bgt);
     199                 :             :     }
     200                 :             : 
     201                 :           1 :     return bgt != NULL;
     202                 :             : }
     203                 :             : 
     204                 :             : 
     205                 :             : GncBudget*
     206                 :           1 : dom_tree_to_budget (xmlNodePtr node, QofBook* book)
     207                 :             : {
     208                 :             :     GncBudget* bgt;
     209                 :             : 
     210                 :           1 :     bgt = gnc_budget_new (book);
     211                 :           1 :     if (!dom_tree_generic_parse (node, budget_handlers, bgt))
     212                 :             :     {
     213                 :           0 :         PERR ("failed to parse budget tree");
     214                 :           0 :         gnc_budget_destroy (bgt);
     215                 :           0 :         bgt = NULL;
     216                 :             :     }
     217                 :           1 :     return bgt;
     218                 :             : }
     219                 :             : 
     220                 :             : sixtp*
     221                 :          21 : gnc_budget_sixtp_parser_create (void)
     222                 :             : {
     223                 :          21 :     return sixtp_dom_parser_new (gnc_budget_end_handler, NULL, NULL);
     224                 :             : }
     225                 :             : /* ======================  END OF FILE ===================*/
        

Generated by: LCOV version 2.0-1