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

            Line data    Source code
       1              : /********************************************************************
       2              :  * gnc-backend-xml.c: load and save data to XML files               *
       3              :  *                                                                  *
       4              :  * This program is free software; you can redistribute it and/or    *
       5              :  * modify it under the terms of the GNU General Public License as   *
       6              :  * published by the Free Software Foundation; either version 2 of   *
       7              :  * the License, or (at your option) any later version.              *
       8              :  *                                                                  *
       9              :  * This program is distributed in the hope that it will be useful,  *
      10              :  * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
      11              :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
      12              :  * GNU General Public License for more details.                     *
      13              :  *                                                                  *
      14              :  * You should have received a copy of the GNU General Public License*
      15              :  * along with this program; if not, contact:                        *
      16              :  *                                                                  *
      17              :  * Free Software Foundation           Voice:  +1-617-542-5942       *
      18              :  * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
      19              :  * Boston, MA  02110-1301,  USA       gnu@gnu.org                   *
      20              : \********************************************************************/
      21              : /** @file gnc-backend-xml.c
      22              :  *  @brief load and save data to XML files
      23              :  *  @author Copyright (c) 2000 Gnumatic Inc.
      24              :  *  @author Copyright (c) 2002 Derek Atkins <warlord@MIT.EDU>
      25              :  *  @author Copyright (c) 2003 Linas Vepstas <linas@linas.org>
      26              :  *
      27              :  * This file implements the top-level QofBackend API for saving/
      28              :  * restoring data to/from an ordinary Unix filesystem file.
      29              :  */
      30              : #include <glib.h>
      31              : #include <glib/gi18n.h>
      32              : #include <glib/gstdio.h>
      33              : 
      34              : #include <config.h>
      35              : 
      36              : 
      37              : #include <libintl.h>
      38              : #include <locale.h>
      39              : #include <fcntl.h>
      40              : #include <limits.h>
      41              : #include <sys/types.h>
      42              : #ifdef HAVE_UNISTD_H
      43              : # include <unistd.h>
      44              : #else
      45              : # ifdef _MSC_VER
      46              :     typedef int ssize_t;
      47              : # endif
      48              : #endif
      49              : #include <errno.h>
      50              : #include <string.h>
      51              : #ifdef HAVE_DIRENT_H
      52              : # include <dirent.h>
      53              : #endif
      54              : #include <time.h>
      55              : #ifdef G_OS_WIN32
      56              : # include <io.h>
      57              : # define close _close
      58              : # define mktemp _mktemp
      59              : # define read _read
      60              : # define write _write
      61              : #endif
      62              : #include "platform.h"
      63              : #if COMPILER(MSVC)
      64              : # define g_fopen fopen
      65              : # define g_open _open
      66              : #endif
      67              : 
      68              : #include "qof.h"
      69              : #include "gnc-engine.h"
      70              : #include <gnc-uri-utils.h>
      71              : #include "gnc-prefs.h"
      72              : 
      73              : #ifndef HAVE_STRPTIME
      74              : # include "strptime.h"
      75              : #endif
      76              : 
      77              : #include <gnc-backend-prov.hpp>
      78              : #include "gnc-backend-xml.h"
      79              : #include <qof-backend.hpp>
      80              : #include "gnc-xml-backend.hpp"
      81              : #include "gnc-xml-helper.h"
      82              : #include "io-gncxml-v2.h"
      83              : #include "io-gncxml.h"
      84              : 
      85              : #include "gnc-address-xml-v2.h"
      86              : #include "gnc-bill-term-xml-v2.h"
      87              : #include "gnc-customer-xml-v2.h"
      88              : #include "gnc-employee-xml-v2.h"
      89              : #include "gnc-entry-xml-v2.h"
      90              : #include "gnc-invoice-xml-v2.h"
      91              : #include "gnc-job-xml-v2.h"
      92              : #include "gnc-order-xml-v2.h"
      93              : #include "gnc-owner-xml-v2.h"
      94              : #include "gnc-tax-table-xml-v2.h"
      95              : #include "gnc-vendor-xml-v2.h"
      96              : 
      97              : static QofLogModule log_module = GNC_MOD_BACKEND;
      98              : 
      99              : 
     100              : struct QofXmlBackendProvider : public QofBackendProvider
     101              : {
     102           30 :     QofXmlBackendProvider (const char* name, const char* type) :
     103           30 :         QofBackendProvider {name, type} {}
     104              :     QofXmlBackendProvider(QofXmlBackendProvider&) = delete;
     105              :     QofXmlBackendProvider operator=(QofXmlBackendProvider&) = delete;
     106              :     QofXmlBackendProvider(QofXmlBackendProvider&&) = delete;
     107              :     QofXmlBackendProvider operator=(QofXmlBackendProvider&&) = delete;
     108            4 :     ~QofXmlBackendProvider () = default;
     109           64 :     QofBackend* create_backend(void) { return new GncXmlBackend; }
     110              :     bool type_check(const char* type);
     111              : 
     112              : };
     113              : 
     114              : bool
     115           24 : QofXmlBackendProvider::type_check (const char *uri)
     116              : {
     117              :     GStatBuf sbuf;
     118              :     int rc;
     119              :     FILE* t;
     120              :     gchar* filename;
     121              :     QofBookFileType xml_type;
     122              :     gboolean result;
     123              : 
     124           24 :     if (!uri)
     125              :     {
     126            0 :         return FALSE;
     127              :     }
     128              : 
     129           24 :     filename = gnc_uri_get_path (uri);
     130           24 :     t = g_fopen (filename, "r");
     131           24 :     if (!t)
     132              :     {
     133            2 :         PINFO (" new file");
     134            2 :         result = TRUE;
     135            2 :         goto det_exit;
     136              :     }
     137           22 :     fclose (t);
     138           22 :     rc = g_stat (filename, &sbuf);
     139           22 :     if (rc < 0)
     140              :     {
     141            0 :         result = FALSE;
     142            0 :         goto det_exit;
     143              :     }
     144           22 :     if (sbuf.st_size == 0)
     145              :     {
     146            0 :         PINFO (" empty file");
     147            0 :         result = TRUE;
     148            0 :         goto det_exit;
     149              :     }
     150           22 :     xml_type = gnc_is_xml_data_file_v2 (filename, NULL);
     151           22 :     if ((xml_type == GNC_BOOK_XML2_FILE) ||
     152            0 :         (xml_type == GNC_BOOK_XML1_FILE) ||
     153              :         (xml_type == GNC_BOOK_POST_XML2_0_0_FILE))
     154              :     {
     155           22 :         result = TRUE;
     156           22 :         goto det_exit;
     157              :     }
     158            0 :     PINFO (" %s is not a gnc XML file", filename);
     159            0 :     result = FALSE;
     160              : 
     161           24 : det_exit:
     162           24 :     g_free (filename);
     163           24 :     return result;
     164              : }
     165              : 
     166              : /* ================================================================= */
     167              : 
     168              : static void
     169           15 : business_core_xml_init (void)
     170              : {
     171              :     /* Initialize our pointers into the backend subsystem */
     172           15 :     gnc_address_xml_initialize ();
     173           15 :     gnc_billterm_xml_initialize ();
     174           15 :     gnc_customer_xml_initialize ();
     175           15 :     gnc_employee_xml_initialize ();
     176           15 :     gnc_entry_xml_initialize ();
     177           15 :     gnc_invoice_xml_initialize ();
     178           15 :     gnc_job_xml_initialize ();
     179           15 :     gnc_order_xml_initialize ();
     180           15 :     gnc_owner_xml_initialize ();
     181           15 :     gnc_taxtable_xml_initialize ();
     182           15 :     gnc_vendor_xml_initialize ();
     183           15 : }
     184              : 
     185              : #ifndef GNC_NO_LOADABLE_MODULES
     186              : G_MODULE_EXPORT void
     187           15 : qof_backend_module_init (void)
     188              : {
     189           15 :     gnc_module_init_backend_xml ();
     190           15 : }
     191              : #endif
     192              : 
     193              : void
     194           15 : gnc_module_init_backend_xml (void)
     195              : {
     196           15 :     const char* name {"GnuCash File Backend Version 2"};
     197           15 :     auto prov = QofBackendProvider_ptr(new QofXmlBackendProvider{name, "xml"});
     198              : 
     199           15 :     qof_backend_register_provider(std::move(prov));
     200           15 :     prov = QofBackendProvider_ptr(new QofXmlBackendProvider{name, "file"});
     201           15 :     qof_backend_register_provider(std::move(prov));
     202              : 
     203              :     /* And the business objects */
     204           15 :     business_core_xml_init ();
     205           15 : }
     206              : 
     207              : /* ========================== END OF FILE ===================== */
        

Generated by: LCOV version 2.0-1