GnuCash  5.6-150-g038405b370+
gnc-backend-xml.cpp
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 \********************************************************************/
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 
101 {
102  QofXmlBackendProvider (const char* name, const char* type) :
103  QofBackendProvider {name, type} {}
105  QofXmlBackendProvider operator=(QofXmlBackendProvider&) = delete;
107  QofXmlBackendProvider operator=(QofXmlBackendProvider&&) = delete;
108  ~QofXmlBackendProvider () = default;
109  QofBackend* create_backend(void) { return new GncXmlBackend; }
110  bool type_check(const char* type);
111 
112 };
113 
114 bool
116 {
117  GStatBuf sbuf;
118  int rc;
119  FILE* t;
120  gchar* filename;
121  QofBookFileType xml_type;
122  gboolean result;
123 
124  if (!uri)
125  {
126  return FALSE;
127  }
128 
129  filename = gnc_uri_get_path (uri);
130  t = g_fopen (filename, "r");
131  if (!t)
132  {
133  PINFO (" new file");
134  result = TRUE;
135  goto det_exit;
136  }
137  fclose (t);
138  rc = g_stat (filename, &sbuf);
139  if (rc < 0)
140  {
141  result = FALSE;
142  goto det_exit;
143  }
144  if (sbuf.st_size == 0)
145  {
146  PINFO (" empty file");
147  result = TRUE;
148  goto det_exit;
149  }
150  xml_type = gnc_is_xml_data_file_v2 (filename, NULL);
151  if ((xml_type == GNC_BOOK_XML2_FILE) ||
152  (xml_type == GNC_BOOK_XML1_FILE) ||
153  (xml_type == GNC_BOOK_POST_XML2_0_0_FILE))
154  {
155  result = TRUE;
156  goto det_exit;
157  }
158  PINFO (" %s is not a gnc XML file", filename);
159  result = FALSE;
160 
161 det_exit:
162  g_free (filename);
163  return result;
164 }
165 
166 /* ================================================================= */
167 
168 static void
169 business_core_xml_init (void)
170 {
171  /* Initialize our pointers into the backend subsystem */
172  gnc_address_xml_initialize ();
173  gnc_billterm_xml_initialize ();
174  gnc_customer_xml_initialize ();
175  gnc_employee_xml_initialize ();
176  gnc_entry_xml_initialize ();
177  gnc_invoice_xml_initialize ();
178  gnc_job_xml_initialize ();
179  gnc_order_xml_initialize ();
180  gnc_owner_xml_initialize ();
181  gnc_taxtable_xml_initialize ();
182  gnc_vendor_xml_initialize ();
183 }
184 
185 #ifndef GNC_NO_LOADABLE_MODULES
186 G_MODULE_EXPORT void
188 {
190 }
191 #endif
192 
193 void
195 {
196  const char* name {"GnuCash File Backend Version 2"};
197  auto prov = QofBackendProvider_ptr(new QofXmlBackendProvider{name, "xml"});
198 
199  qof_backend_register_provider(std::move(prov));
200  prov = QofBackendProvider_ptr(new QofXmlBackendProvider{name, "file"});
201  qof_backend_register_provider(std::move(prov));
202 
203  /* And the business objects */
204  business_core_xml_init ();
205 }
206 
207 /* ========================== END OF FILE ===================== */
void gnc_module_init_backend_xml(void)
Initialization function which can be used when this module is statically linked into the application...
#define PINFO(format, args...)
Print an informational note.
Definition: qoflog.h:256
void qof_backend_register_provider(QofBackendProvider_ptr &&prov)
Let the system know about a new provider of backends.
Definition: qofsession.cpp:90
gchar * gnc_uri_get_path(const gchar *uri)
Extracts the path part from a uri.
G_MODULE_EXPORT void qof_backend_module_init(void)
This is the standardized initialization function of a qof_backend GModule, but compiling this can be ...
QofBackend * create_backend(void)
Return a new, fully initialized backend.
api for Version 1 XML-based file format
api for GnuCash version 2 XML-based file format
All type declarations for the whole Gnucash engine.
Generic api to store and retrieve preferences.
bool type_check(const char *type)
Distinguish two providers with same access method.
Utility functions for convert uri in separate components and back.
load and save data to files