LCOV - code coverage report
Current view: top level - libgnucash/backend/xml - gnc-recurrence-xml-v2.cpp (source / functions) Coverage Total Hit
Test: gnucash.info Lines: 90.9 % 44 40
Test Date: 2026-11-24 04:31:04 Functions: 100.0 % 8 8
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /*
       2              :  * gnc-recurrence-xml-v2.c -- xml routines for Recurrence
       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 <string.h>
      27              : #include "qof.h"
      28              : #include "Recurrence.h"
      29              : 
      30              : #include "gnc-xml.h"
      31              : #include "gnc-xml-helper.h"
      32              : 
      33              : #include "sixtp.h"
      34              : #include "sixtp-utils.h"
      35              : #include "sixtp-parsers.h"
      36              : #include "sixtp-utils.h"
      37              : #include "sixtp-dom-parsers.h"
      38              : #include "sixtp-dom-generators.h"
      39              : #include "io-gncxml-v2.h"
      40              : 
      41              : static QofLogModule log_module = GNC_MOD_IO;
      42              : 
      43              : const gchar* recurrence_version_string = "1.0.0";
      44              : #define recurrence_root          "gnc:recurrence"
      45              : #define recurrence_mult          "recurrence:mult"
      46              : #define recurrence_period_type   "recurrence:period_type"
      47              : #define recurrence_start         "recurrence:start"
      48              : #define recurrence_weekend_adj   "recurrence:weekend_adj"
      49              : 
      50              : //TODO: I think three of these functions rightly belong in Recurrence.c.
      51              : 
      52              : static gboolean
      53            6 : recurrence_period_type_handler (xmlNodePtr node, gpointer d)
      54              : {
      55            6 :     auto r = static_cast<Recurrence*>(d);
      56            6 :     auto set_ptype = [](Recurrence *r, const char* txt)
      57              :     {
      58            6 :         r->ptype = recurrencePeriodTypeFromString (txt);
      59            6 :     };
      60            6 :     apply_xmlnode_text (set_ptype, r, node);
      61            6 :     return (r->ptype != -1);
      62              : }
      63              : 
      64              : static gboolean
      65            6 : recurrence_start_date_handler (xmlNodePtr node, gpointer r)
      66              : {
      67            6 :     auto d = dom_tree_to_gdate (node);
      68            6 :     g_return_val_if_fail (d, FALSE);
      69            6 :     g_return_val_if_fail (g_date_valid (&d.value()), FALSE);
      70            6 :     ((Recurrence*) r)->start = *d;
      71            6 :     return TRUE;
      72              : }
      73              : 
      74              : static gboolean
      75            6 : recurrence_mult_handler (xmlNodePtr node, gpointer r)
      76              : {
      77            6 :     return dom_tree_to_guint16 (node, & ((Recurrence*)r)->mult);
      78              : }
      79              : 
      80              : static gboolean
      81            2 : recurrence_weekend_adj_handler (xmlNodePtr node, gpointer d)
      82              : {
      83            2 :     auto r = static_cast<Recurrence*>(d);
      84            2 :     auto set_wadj = [](Recurrence *r, const char* txt)
      85              :     {
      86            2 :         r->wadj = recurrenceWeekendAdjustFromString (txt);
      87            2 :     };
      88            2 :     apply_xmlnode_text (set_wadj, r, node);
      89            2 :     return (r->wadj != -1);
      90              : }
      91              : 
      92              : static struct dom_tree_handler recurrence_dom_handlers[] =
      93              : {
      94              :     { recurrence_mult, recurrence_mult_handler, 1, 0 },
      95              :     { recurrence_period_type, recurrence_period_type_handler, 1, 0 },
      96              :     { recurrence_start, recurrence_start_date_handler, 1, 0 },
      97              :     { recurrence_weekend_adj, recurrence_weekend_adj_handler, 0, 0 },
      98              :     { NULL, NULL, 0, 0 }
      99              : };
     100              : 
     101              : Recurrence*
     102            6 : dom_tree_to_recurrence (xmlNodePtr node)
     103              : {
     104              :     gboolean successful;
     105              :     Recurrence* r;
     106              : 
     107            6 :     r = g_new (Recurrence, 1);
     108              :     /* In case the file doesn't have a weekend adjustment element */
     109            6 :     r->wadj = WEEKEND_ADJ_NONE;
     110            6 :     successful = dom_tree_generic_parse (node, recurrence_dom_handlers, r);
     111            6 :     if (!successful)
     112              :     {
     113            0 :         PERR ("failed to parse recurrence node");
     114            0 :         xmlElemDump (stdout, NULL, node);
     115            0 :         g_free (r);
     116            0 :         r = NULL;
     117              :     }
     118            6 :     return r;
     119              : }
     120              : 
     121              : xmlNodePtr
     122            2 : recurrence_to_dom_tree (const gchar* tag, const Recurrence* r)
     123              : {
     124              :     xmlNodePtr n;
     125              :     PeriodType pt;
     126              :     GDate d;
     127              :     WeekendAdjust wadj;
     128              : 
     129            2 :     n = xmlNewNode (NULL, BAD_CAST tag);
     130            2 :     xmlSetProp (n, BAD_CAST "version", BAD_CAST recurrence_version_string);
     131            2 :     xmlAddChild (n, guint_to_dom_tree (recurrence_mult,
     132              :                                        recurrenceGetMultiplier (r)));
     133            2 :     pt = recurrenceGetPeriodType (r);
     134            2 :     xmlAddChild (n, text_to_dom_tree (recurrence_period_type,
     135              :                                       recurrencePeriodTypeToString (pt)));
     136            2 :     d = recurrenceGetDate (r);
     137            2 :     xmlAddChild (n, gdate_to_dom_tree (recurrence_start, &d));
     138            2 :     wadj = recurrenceGetWeekendAdjust (r);
     139            2 :     if (wadj != WEEKEND_ADJ_NONE)
     140              :     {
     141              :         /* In r17725 and r17751, I introduced this extra XML child
     142              :         element, but this means a gnucash-2.2.x cannot read the SX
     143              :         recurrence of a >=2.3.x file anymore, which is bad. In order
     144              :         to improve this broken backward compatibility for most of the
     145              :         cases, we don't write out this XML element as long as it is
     146              :         only "none". */
     147            2 :         xmlAddChild (n, text_to_dom_tree (recurrence_weekend_adj,
     148              :                                           recurrenceWeekendAdjustToString (wadj)));
     149              :     }
     150            2 :     return n;
     151              : }
        

Generated by: LCOV version 2.0-1