LCOV - code coverage report
Current view: top level - libgnucash/backend/xml - gnc-lot-xml-v2.cpp (source / functions) Coverage Total Hit
Test: gnucash.info Lines: 61.1 % 54 33
Test Date: 2025-02-07 16:25:45 Functions: 66.7 % 6 4
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /********************************************************************\
       2                 :             :  * gnc-lot-xml-v2.c -- lot xml i/o implementation                   *
       3                 :             :  *                                                                  *
       4                 :             :  * Copyright (C) 2001 James LewisMoss <dres@debian.org>             *
       5                 :             :  * Copyright (C) 2002 Linas Vepstas <linas@linas.org>               *
       6                 :             :  *                                                                  *
       7                 :             :  * This program is free software; you can redistribute it and/or    *
       8                 :             :  * modify it under the terms of the GNU General Public License as   *
       9                 :             :  * published by the Free Software Foundation; either version 2 of   *
      10                 :             :  * the License, or (at your option) any later version.              *
      11                 :             :  *                                                                  *
      12                 :             :  * This program is distributed in the hope that it will be useful,  *
      13                 :             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
      14                 :             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
      15                 :             :  * GNU General Public License for more details.                     *
      16                 :             :  *                                                                  *
      17                 :             :  * You should have received a copy of the GNU General Public License*
      18                 :             :  * along with this program; if not, contact:                        *
      19                 :             :  *                                                                  *
      20                 :             :  * Free Software Foundation           Voice:  +1-617-542-5942       *
      21                 :             :  * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
      22                 :             :  * Boston, MA  02110-1301,  USA       gnu@gnu.org                   *
      23                 :             :  *                                                                  *
      24                 :             : \********************************************************************/
      25                 :             : #include <glib.h>
      26                 :             : 
      27                 :             : #include <config.h>
      28                 :             : #include <stdlib.h>
      29                 :             : #include <string.h>
      30                 :             : #include "gnc-lot.h"
      31                 :             : #include "gnc-lot-p.h"
      32                 :             : 
      33                 :             : #include "gnc-xml-helper.h"
      34                 :             : #include "sixtp.h"
      35                 :             : #include "sixtp-utils.h"
      36                 :             : #include "sixtp-parsers.h"
      37                 :             : #include "sixtp-utils.h"
      38                 :             : #include "sixtp-dom-parsers.h"
      39                 :             : #include "sixtp-dom-generators.h"
      40                 :             : 
      41                 :             : #include "gnc-xml.h"
      42                 :             : #include "io-gncxml-gen.h"
      43                 :             : #include "io-gncxml-v2.h"
      44                 :             : #include "sixtp-dom-parsers.h"
      45                 :             : 
      46                 :             : static QofLogModule log_module = GNC_MOD_IO;
      47                 :             : 
      48                 :             : const gchar* lot_version_string = "2.0.0";
      49                 :             : 
      50                 :             : /* ids */
      51                 :             : #define gnc_lot_string "gnc:lot"
      52                 :             : #define lot_id_string "lot:id"
      53                 :             : #define lot_slots_string "lot:slots"
      54                 :             : 
      55                 :             : xmlNodePtr
      56                 :           2 : gnc_lot_dom_tree_create (GNCLot* lot)
      57                 :             : {
      58                 :             :     xmlNodePtr ret;
      59                 :             : 
      60                 :           2 :     ENTER ("(lot=%p)", lot);
      61                 :           2 :     ret = xmlNewNode (NULL, BAD_CAST gnc_lot_string);
      62                 :           2 :     xmlSetProp (ret, BAD_CAST "version", BAD_CAST lot_version_string);
      63                 :             : 
      64                 :           2 :     xmlAddChild (ret, guid_to_dom_tree (lot_id_string, gnc_lot_get_guid (lot)));
      65                 :             :     /* xmlAddChild won't do anything with a NULL, so tests are superfluous. */
      66                 :           2 :     xmlAddChild (ret, qof_instance_slots_to_dom_tree (lot_slots_string,
      67                 :           2 :                                                       QOF_INSTANCE (lot)));
      68                 :             : 
      69                 :           2 :     LEAVE ("");
      70                 :           2 :     return ret;
      71                 :             : }
      72                 :             : 
      73                 :             : /* =================================================================== */
      74                 :             : 
      75                 :             : struct lot_pdata
      76                 :             : {
      77                 :             :     GNCLot*  lot;
      78                 :             :     QofBook* book;
      79                 :             : };
      80                 :             : 
      81                 :             : static gboolean
      82                 :           3 : lot_id_handler (xmlNodePtr node, gpointer p)
      83                 :             : {
      84                 :           3 :     struct lot_pdata* pdata = static_cast<decltype (pdata)> (p);
      85                 :             :     GncGUID* guid;
      86                 :             : 
      87                 :           3 :     ENTER ("(lot=%p)", pdata->lot);
      88                 :           3 :     guid = dom_tree_to_guid (node);
      89                 :           3 :     gnc_lot_set_guid (pdata->lot, *guid);
      90                 :             : 
      91                 :           3 :     guid_free (guid);
      92                 :             : 
      93                 :           3 :     LEAVE ("");
      94                 :           3 :     return TRUE;
      95                 :             : }
      96                 :             : 
      97                 :             : static gboolean
      98                 :           3 : lot_slots_handler (xmlNodePtr node, gpointer p)
      99                 :             : {
     100                 :           3 :     struct lot_pdata* pdata = static_cast<decltype (pdata)> (p);
     101                 :             :     gboolean success;
     102                 :             : 
     103                 :           3 :     ENTER ("(lot=%p)", pdata->lot);
     104                 :           3 :     success = dom_tree_create_instance_slots (node, QOF_INSTANCE (pdata->lot));
     105                 :             : 
     106                 :           3 :     LEAVE ("");
     107                 :           3 :     g_return_val_if_fail (success, FALSE);
     108                 :           3 :     return TRUE;
     109                 :             : }
     110                 :             : 
     111                 :             : 
     112                 :             : static struct dom_tree_handler lot_handlers_v2[] =
     113                 :             : {
     114                 :             :     { lot_id_string, lot_id_handler, 1, 0 },
     115                 :             :     { lot_slots_string, lot_slots_handler, 0, 0 },
     116                 :             :     { NULL, 0, 0, 0 }
     117                 :             : };
     118                 :             : 
     119                 :             : static gboolean
     120                 :           0 : gnc_lot_end_handler (gpointer data_for_children,
     121                 :             :                      GSList* data_from_children, GSList* sibling_data,
     122                 :             :                      gpointer parent_data, gpointer global_data,
     123                 :             :                      gpointer* result, const gchar* tag)
     124                 :             : {
     125                 :             :     GNCLot* lot;
     126                 :           0 :     xmlNodePtr tree = (xmlNodePtr)data_for_children;
     127                 :           0 :     gxpf_data* gdata = (gxpf_data*)global_data;
     128                 :           0 :     QofBook* book = static_cast<decltype (book)> (gdata->bookdata);
     129                 :             : 
     130                 :           0 :     if (parent_data)
     131                 :             :     {
     132                 :           0 :         return TRUE;
     133                 :             :     }
     134                 :             : 
     135                 :             :     /* OK.  For some messed up reason this is getting called again with a
     136                 :             :        NULL tag.  So we ignore those cases */
     137                 :           0 :     if (!tag)
     138                 :             :     {
     139                 :           0 :         return TRUE;
     140                 :             :     }
     141                 :             : 
     142                 :           0 :     g_return_val_if_fail (tree, FALSE);
     143                 :             : 
     144                 :           0 :     lot = dom_tree_to_lot (tree, book);
     145                 :           0 :     ENTER ("(lot=%p)", lot);
     146                 :           0 :     if (lot != NULL)
     147                 :             :     {
     148                 :           0 :         gdata->cb (tag, gdata->parsedata, lot);
     149                 :             :     }
     150                 :             : 
     151                 :           0 :     xmlFreeNode (tree);
     152                 :             : 
     153                 :           0 :     LEAVE ("");
     154                 :           0 :     return lot != NULL;
     155                 :             : }
     156                 :             : 
     157                 :             : GNCLot*
     158                 :           3 : dom_tree_to_lot (xmlNodePtr node, QofBook* book)
     159                 :             : {
     160                 :             :     struct lot_pdata pdata;
     161                 :             :     GNCLot* lot;
     162                 :             :     gboolean successful;
     163                 :             : 
     164                 :           3 :     lot = gnc_lot_new (book);
     165                 :           3 :     ENTER ("(lot=%p)", lot);
     166                 :             : 
     167                 :           3 :     pdata.lot = lot;
     168                 :           3 :     pdata.book = book;
     169                 :             : 
     170                 :           3 :     successful = dom_tree_generic_parse (node, lot_handlers_v2,
     171                 :             :                                          &pdata);
     172                 :           3 :     if (!successful)
     173                 :             :     {
     174                 :           0 :         PERR ("failed to parse lot");
     175                 :           0 :         gnc_lot_destroy (lot);
     176                 :           0 :         lot = NULL;
     177                 :             :     }
     178                 :             : 
     179                 :           3 :     LEAVE ("");
     180                 :           3 :     return lot;
     181                 :             : }
     182                 :             : 
     183                 :             : sixtp*
     184                 :           0 : gnc_lot_sixtp_parser_create (void)
     185                 :             : {
     186                 :           0 :     return sixtp_dom_parser_new (gnc_lot_end_handler, NULL, NULL);
     187                 :             : }
     188                 :             : 
     189                 :             : /* ================== END OF FILE ========================== */
        

Generated by: LCOV version 2.0-1