LCOV - code coverage report
Current view: top level - libgnucash/backend/xml - gnc-order-xml-v2.cpp (source / functions) Coverage Total Hit
Test: gnucash.info Lines: 10.8 % 139 15
Test Date: 2025-02-07 16:25:45 Functions: 21.7 % 23 5
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /********************************************************************\
       2                 :             :  * gnc-order-xml-v2.c -- order xml i/o implementation         *
       3                 :             :  *                                                                  *
       4                 :             :  * Copyright (C) 2002 Derek Atkins <warlord@MIT.EDU>                *
       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                 :             : \********************************************************************/
      24                 :             : #include <glib.h>
      25                 :             : 
      26                 :             : #include <config.h>
      27                 :             : #include <stdlib.h>
      28                 :             : #include <string.h>
      29                 :             : #include "gncOrderP.h"
      30                 :             : 
      31                 :             : #include "gnc-xml-helper.h"
      32                 :             : #include "sixtp.h"
      33                 :             : #include "sixtp-utils.h"
      34                 :             : #include "sixtp-parsers.h"
      35                 :             : #include "sixtp-utils.h"
      36                 :             : #include "sixtp-dom-parsers.h"
      37                 :             : #include "sixtp-dom-generators.h"
      38                 :             : 
      39                 :             : #include "gnc-xml.h"
      40                 :             : #include "io-gncxml-gen.h"
      41                 :             : #include "io-gncxml-v2.h"
      42                 :             : 
      43                 :             : #include "gnc-order-xml-v2.h"
      44                 :             : #include "gnc-owner-xml-v2.h"
      45                 :             : 
      46                 :             : #define _GNC_MOD_NAME   GNC_ID_ORDER
      47                 :             : 
      48                 :             : static QofLogModule log_module = GNC_MOD_IO;
      49                 :             : 
      50                 :             : const gchar* order_version_string = "2.0.0";
      51                 :             : 
      52                 :             : /* ids */
      53                 :             : #define gnc_order_string "gnc:GncOrder"
      54                 :             : #define order_guid_string "order:guid"
      55                 :             : #define order_id_string "order:id"
      56                 :             : #define order_owner_string "order:owner"
      57                 :             : #define order_opened_string "order:opened"
      58                 :             : #define order_closed_string "order:closed"
      59                 :             : #define order_notes_string "order:notes"
      60                 :             : #define order_reference_string "order:reference"
      61                 :             : #define order_active_string "order:active"
      62                 :             : #define order_slots_string "order:slots"
      63                 :             : 
      64                 :             : static void
      65                 :           0 : maybe_add_string (xmlNodePtr ptr, const char* tag, const char* str)
      66                 :             : {
      67                 :           0 :     if (str && *str)
      68                 :           0 :         xmlAddChild (ptr, text_to_dom_tree (tag, str));
      69                 :           0 : }
      70                 :             : 
      71                 :             : static xmlNodePtr
      72                 :           0 : order_dom_tree_create (GncOrder* order)
      73                 :             : {
      74                 :             :     xmlNodePtr ret;
      75                 :             :     time64 tt;
      76                 :             : 
      77                 :           0 :     ret = xmlNewNode (NULL, BAD_CAST gnc_order_string);
      78                 :           0 :     xmlSetProp (ret, BAD_CAST "version", BAD_CAST order_version_string);
      79                 :             : 
      80                 :           0 :     xmlAddChild (ret, guid_to_dom_tree (order_guid_string,
      81                 :           0 :                                         qof_instance_get_guid (QOF_INSTANCE (order))));
      82                 :             : 
      83                 :           0 :     xmlAddChild (ret, text_to_dom_tree (order_id_string,
      84                 :             :                                         gncOrderGetID (order)));
      85                 :             : 
      86                 :           0 :     xmlAddChild (ret, gnc_owner_to_dom_tree (order_owner_string,
      87                 :           0 :                                              gncOrderGetOwner (order)));
      88                 :             : 
      89                 :           0 :     tt = gncOrderGetDateOpened (order);
      90                 :           0 :     xmlAddChild (ret, time64_to_dom_tree (order_opened_string, tt));
      91                 :             : 
      92                 :           0 :     tt = gncOrderGetDateClosed (order);
      93                 :           0 :     if (tt != INT64_MAX)
      94                 :           0 :         xmlAddChild (ret, time64_to_dom_tree (order_closed_string, tt));
      95                 :             : 
      96                 :           0 :     maybe_add_string (ret, order_notes_string, gncOrderGetNotes (order));
      97                 :           0 :     maybe_add_string (ret, order_reference_string, gncOrderGetReference (order));
      98                 :             : 
      99                 :           0 :     xmlAddChild (ret, int_to_dom_tree (order_active_string,
     100                 :           0 :                                        gncOrderGetActive (order)));
     101                 :             : 
     102                 :             :     /* xmlAddChild won't do anything with a NULL, so tests are superfluous. */
     103                 :           0 :     xmlAddChild (ret, qof_instance_slots_to_dom_tree (order_slots_string,
     104                 :           0 :                                                       QOF_INSTANCE (order)));
     105                 :             : 
     106                 :           0 :     return ret;
     107                 :             : }
     108                 :             : 
     109                 :             : /***********************************************************************/
     110                 :             : 
     111                 :             : struct order_pdata
     112                 :             : {
     113                 :             :     GncOrder* order;
     114                 :             :     QofBook* book;
     115                 :             : };
     116                 :             : 
     117                 :             : static inline gboolean
     118                 :           0 : set_string (xmlNodePtr node, GncOrder* order,
     119                 :             :             void (*func) (GncOrder* order, const char* txt))
     120                 :             : {
     121                 :           0 :     char* txt = dom_tree_to_text (node);
     122                 :           0 :     g_return_val_if_fail (txt, FALSE);
     123                 :             : 
     124                 :           0 :     func (order, txt);
     125                 :             : 
     126                 :           0 :     g_free (txt);
     127                 :           0 :     return TRUE;
     128                 :             : }
     129                 :             : 
     130                 :             : static inline gboolean
     131                 :           0 : set_time64 (xmlNodePtr node, GncOrder* order,
     132                 :             :               void (*func) (GncOrder* order, time64 tt))
     133                 :             : {
     134                 :           0 :     time64 time = dom_tree_to_time64 (node);
     135                 :           0 :     if (!dom_tree_valid_time64 (time, node->name)) time = 0;
     136                 :           0 :     func (order, time);
     137                 :           0 :     return TRUE;
     138                 :             : }
     139                 :             : 
     140                 :             : static gboolean
     141                 :           0 : order_guid_handler (xmlNodePtr node, gpointer order_pdata)
     142                 :             : {
     143                 :           0 :     struct order_pdata* pdata = static_cast<decltype (pdata)> (order_pdata);
     144                 :             :     GncGUID* guid;
     145                 :             :     GncOrder* order;
     146                 :             : 
     147                 :           0 :     guid = dom_tree_to_guid (node);
     148                 :           0 :     g_return_val_if_fail (guid, FALSE);
     149                 :           0 :     order = gncOrderLookup (pdata->book, guid);
     150                 :           0 :     if (order)
     151                 :             :     {
     152                 :           0 :         gncOrderDestroy (pdata->order);
     153                 :           0 :         pdata->order = order;
     154                 :           0 :         gncOrderBeginEdit (order);
     155                 :             :     }
     156                 :             :     else
     157                 :             :     {
     158                 :           0 :         gncOrderSetGUID (pdata->order, guid);
     159                 :             :     }
     160                 :             : 
     161                 :           0 :     guid_free (guid);
     162                 :             : 
     163                 :           0 :     return TRUE;
     164                 :             : }
     165                 :             : 
     166                 :             : static gboolean
     167                 :           0 : order_id_handler (xmlNodePtr node, gpointer order_pdata)
     168                 :             : {
     169                 :           0 :     struct order_pdata* pdata = static_cast<decltype (pdata)> (order_pdata);
     170                 :             : 
     171                 :           0 :     return set_string (node, pdata->order, gncOrderSetID);
     172                 :             : }
     173                 :             : 
     174                 :             : static gboolean
     175                 :           0 : order_owner_handler (xmlNodePtr node, gpointer order_pdata)
     176                 :             : {
     177                 :           0 :     struct order_pdata* pdata = static_cast<decltype (pdata)> (order_pdata);
     178                 :             :     GncOwner owner;
     179                 :             :     gboolean ret;
     180                 :             : 
     181                 :           0 :     ret = gnc_dom_tree_to_owner (node, &owner, pdata->book);
     182                 :           0 :     if (ret)
     183                 :           0 :         gncOrderSetOwner (pdata->order, &owner);
     184                 :             : 
     185                 :           0 :     return ret;
     186                 :             : }
     187                 :             : 
     188                 :             : static gboolean
     189                 :           0 : order_opened_handler (xmlNodePtr node, gpointer order_pdata)
     190                 :             : {
     191                 :           0 :     struct order_pdata* pdata = static_cast<decltype (pdata)> (order_pdata);
     192                 :             : 
     193                 :           0 :     return set_time64 (node, pdata->order, gncOrderSetDateOpened);
     194                 :             : }
     195                 :             : 
     196                 :             : static gboolean
     197                 :           0 : order_closed_handler (xmlNodePtr node, gpointer order_pdata)
     198                 :             : {
     199                 :           0 :     struct order_pdata* pdata = static_cast<decltype (pdata)> (order_pdata);
     200                 :             : 
     201                 :           0 :     return set_time64 (node, pdata->order, gncOrderSetDateClosed);
     202                 :             : }
     203                 :             : 
     204                 :             : static gboolean
     205                 :           0 : order_notes_handler (xmlNodePtr node, gpointer order_pdata)
     206                 :             : {
     207                 :           0 :     struct order_pdata* pdata = static_cast<decltype (pdata)> (order_pdata);
     208                 :             : 
     209                 :           0 :     return set_string (node, pdata->order, gncOrderSetNotes);
     210                 :             : }
     211                 :             : 
     212                 :             : static gboolean
     213                 :           0 : order_reference_handler (xmlNodePtr node, gpointer order_pdata)
     214                 :             : {
     215                 :           0 :     struct order_pdata* pdata = static_cast<decltype (pdata)> (order_pdata);
     216                 :             : 
     217                 :           0 :     return set_string (node, pdata->order, gncOrderSetReference);
     218                 :             : }
     219                 :             : 
     220                 :             : static gboolean
     221                 :           0 : order_active_handler (xmlNodePtr node, gpointer order_pdata)
     222                 :             : {
     223                 :           0 :     struct order_pdata* pdata = static_cast<decltype (pdata)> (order_pdata);
     224                 :             :     gint64 val;
     225                 :             :     gboolean ret;
     226                 :             : 
     227                 :           0 :     ret = dom_tree_to_integer (node, &val);
     228                 :           0 :     if (ret)
     229                 :           0 :         gncOrderSetActive (pdata->order, (gboolean)val);
     230                 :             : 
     231                 :           0 :     return ret;
     232                 :             : }
     233                 :             : 
     234                 :             : static gboolean
     235                 :           0 : order_slots_handler (xmlNodePtr node, gpointer order_pdata)
     236                 :             : {
     237                 :           0 :     struct order_pdata* pdata = static_cast<decltype (pdata)> (order_pdata);
     238                 :             : 
     239                 :           0 :     return dom_tree_create_instance_slots (node, QOF_INSTANCE (pdata->order));
     240                 :             : }
     241                 :             : 
     242                 :             : static struct dom_tree_handler order_handlers_v2[] =
     243                 :             : {
     244                 :             :     { order_guid_string, order_guid_handler, 1, 0 },
     245                 :             :     { order_id_string, order_id_handler, 1, 0 },
     246                 :             :     { order_owner_string, order_owner_handler, 1, 0 },
     247                 :             :     { order_opened_string, order_opened_handler, 1, 0 },
     248                 :             :     { order_closed_string, order_closed_handler, 0, 0 },
     249                 :             :     { order_notes_string, order_notes_handler, 0, 0 },
     250                 :             :     { order_reference_string, order_reference_handler, 0, 0 },
     251                 :             :     { order_active_string, order_active_handler, 1, 0 },
     252                 :             :     { order_slots_string, order_slots_handler, 0, 0 },
     253                 :             :     { NULL, 0, 0, 0 }
     254                 :             : };
     255                 :             : 
     256                 :             : static GncOrder*
     257                 :           0 : dom_tree_to_order (xmlNodePtr node, QofBook* book)
     258                 :             : {
     259                 :             :     struct order_pdata order_pdata;
     260                 :             :     gboolean successful;
     261                 :             : 
     262                 :           0 :     order_pdata.order = gncOrderCreate (book);
     263                 :           0 :     order_pdata.book = book;
     264                 :           0 :     gncOrderBeginEdit (order_pdata.order);
     265                 :             : 
     266                 :           0 :     successful = dom_tree_generic_parse (node, order_handlers_v2,
     267                 :             :                                          &order_pdata);
     268                 :             : 
     269                 :           0 :     if (successful)
     270                 :           0 :         gncOrderCommitEdit (order_pdata.order);
     271                 :             :     else
     272                 :             :     {
     273                 :           0 :         PERR ("failed to parse order tree");
     274                 :           0 :         gncOrderDestroy (order_pdata.order);
     275                 :           0 :         order_pdata.order = NULL;
     276                 :             :     }
     277                 :             : 
     278                 :           0 :     return order_pdata.order;
     279                 :             : }
     280                 :             : 
     281                 :             : static gboolean
     282                 :           0 : gnc_order_end_handler (gpointer data_for_children,
     283                 :             :                        GSList* data_from_children, GSList* sibling_data,
     284                 :             :                        gpointer parent_data, gpointer global_data,
     285                 :             :                        gpointer* result, const gchar* tag)
     286                 :             : {
     287                 :             :     GncOrder* order;
     288                 :           0 :     xmlNodePtr tree = (xmlNodePtr)data_for_children;
     289                 :           0 :     gxpf_data* gdata = (gxpf_data*)global_data;
     290                 :           0 :     QofBook* book = static_cast<decltype (book)> (gdata->bookdata);
     291                 :             : 
     292                 :           0 :     if (parent_data)
     293                 :             :     {
     294                 :           0 :         return TRUE;
     295                 :             :     }
     296                 :             : 
     297                 :             :     /* OK.  For some messed up reason this is getting called again with a
     298                 :             :        NULL tag.  So we ignore those cases */
     299                 :           0 :     if (!tag)
     300                 :             :     {
     301                 :           0 :         return TRUE;
     302                 :             :     }
     303                 :             : 
     304                 :           0 :     g_return_val_if_fail (tree, FALSE);
     305                 :             : 
     306                 :           0 :     order = dom_tree_to_order (tree, book);
     307                 :           0 :     if (order != NULL)
     308                 :             :     {
     309                 :           0 :         gdata->cb (tag, gdata->parsedata, order);
     310                 :             :     }
     311                 :             : 
     312                 :           0 :     xmlFreeNode (tree);
     313                 :             : 
     314                 :           0 :     return order != NULL;
     315                 :             : }
     316                 :             : 
     317                 :             : static sixtp*
     318                 :          21 : order_sixtp_parser_create (void)
     319                 :             : {
     320                 :          21 :     return sixtp_dom_parser_new (gnc_order_end_handler, NULL, NULL);
     321                 :             : }
     322                 :             : 
     323                 :             : static gboolean
     324                 :           0 : order_should_be_saved (GncOrder* order)
     325                 :             : {
     326                 :             :     const char* id;
     327                 :             : 
     328                 :             :     /* make sure this is a valid order before we save it -- should have an ID */
     329                 :           0 :     id = gncOrderGetID (order);
     330                 :           0 :     if (id == NULL || *id == '\0')
     331                 :           0 :         return FALSE;
     332                 :             : 
     333                 :           0 :     return TRUE;
     334                 :             : }
     335                 :             : 
     336                 :             : static void
     337                 :           0 : do_count (QofInstance* order_p, gpointer count_p)
     338                 :             : {
     339                 :           0 :     int* count = static_cast<decltype (count)> (count_p);
     340                 :           0 :     if (order_should_be_saved ((GncOrder*) order_p))
     341                 :           0 :         (*count)++;
     342                 :           0 : }
     343                 :             : 
     344                 :             : static int
     345                 :           4 : order_get_count (QofBook* book)
     346                 :             : {
     347                 :           4 :     int count = 0;
     348                 :           4 :     qof_object_foreach (_GNC_MOD_NAME, book, do_count, (gpointer) &count);
     349                 :           4 :     return count;
     350                 :             : }
     351                 :             : 
     352                 :             : static void
     353                 :           0 : xml_add_order (QofInstance* order_p, gpointer out_p)
     354                 :             : {
     355                 :             :     xmlNodePtr node;
     356                 :           0 :     GncOrder* order = (GncOrder*) order_p;
     357                 :           0 :     FILE* out = static_cast<decltype (out)> (out_p);
     358                 :             : 
     359                 :           0 :     if (ferror (out))
     360                 :           0 :         return;
     361                 :           0 :     if (!order_should_be_saved (order))
     362                 :           0 :         return;
     363                 :             : 
     364                 :           0 :     node = order_dom_tree_create (order);
     365                 :           0 :     xmlElemDump (out, NULL, node);
     366                 :           0 :     xmlFreeNode (node);
     367                 :           0 :     if (ferror (out) || fprintf (out, "\n") < 0)
     368                 :           0 :         return;
     369                 :             : }
     370                 :             : 
     371                 :             : static gboolean
     372                 :           4 : order_write (FILE* out, QofBook* book)
     373                 :             : {
     374                 :           4 :     qof_object_foreach_sorted (_GNC_MOD_NAME, book, xml_add_order, (gpointer) out);
     375                 :           4 :     return ferror (out) == 0;
     376                 :             : }
     377                 :             : 
     378                 :             : static gboolean
     379                 :           4 : order_ns (FILE* out)
     380                 :             : {
     381                 :           4 :     g_return_val_if_fail (out, FALSE);
     382                 :           4 :     return gnc_xml2_write_namespace_decl (out, "order");
     383                 :             : }
     384                 :             : 
     385                 :             : void
     386                 :          61 : gnc_order_xml_initialize (void)
     387                 :             : {
     388                 :             :     static GncXmlDataType_t be_data =
     389                 :             :     {
     390                 :             :         GNC_FILE_BACKEND_VERS,
     391                 :             :         gnc_order_string,
     392                 :             :         order_sixtp_parser_create,
     393                 :             :         NULL,           /* add_item */
     394                 :             :         order_get_count,
     395                 :             :         order_write,
     396                 :             :         NULL,           /* scrub */
     397                 :             :         order_ns,
     398                 :             :     };
     399                 :             : 
     400                 :          61 :     gnc_xml_register_backend(be_data);
     401                 :          61 : }
        

Generated by: LCOV version 2.0-1