GnuCash  5.6-150-g038405b370+
gnc-lot-xml-v2.cpp
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 gnc_lot_dom_tree_create (GNCLot* lot)
57 {
58  xmlNodePtr ret;
59 
60  ENTER ("(lot=%p)", lot);
61  ret = xmlNewNode (NULL, BAD_CAST gnc_lot_string);
62  xmlSetProp (ret, BAD_CAST "version", BAD_CAST lot_version_string);
63 
64  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  xmlAddChild (ret, qof_instance_slots_to_dom_tree (lot_slots_string,
67  QOF_INSTANCE (lot)));
68 
69  LEAVE ("");
70  return ret;
71 }
72 
73 /* =================================================================== */
74 
75 struct lot_pdata
76 {
77  GNCLot* lot;
78  QofBook* book;
79 };
80 
81 static gboolean
82 lot_id_handler (xmlNodePtr node, gpointer p)
83 {
84  struct lot_pdata* pdata = static_cast<decltype (pdata)> (p);
85 
86  ENTER ("(lot=%p)", pdata->lot);
87  auto guid = dom_tree_to_guid (node);
88  gnc_lot_set_guid (pdata->lot, *guid);
89 
90  LEAVE ("");
91  return TRUE;
92 }
93 
94 static gboolean
95 lot_slots_handler (xmlNodePtr node, gpointer p)
96 {
97  struct lot_pdata* pdata = static_cast<decltype (pdata)> (p);
98  gboolean success;
99 
100  ENTER ("(lot=%p)", pdata->lot);
101  success = dom_tree_create_instance_slots (node, QOF_INSTANCE (pdata->lot));
102 
103  LEAVE ("");
104  g_return_val_if_fail (success, FALSE);
105  return TRUE;
106 }
107 
108 
109 static struct dom_tree_handler lot_handlers_v2[] =
110 {
111  { lot_id_string, lot_id_handler, 1, 0 },
112  { lot_slots_string, lot_slots_handler, 0, 0 },
113  { NULL, 0, 0, 0 }
114 };
115 
116 static gboolean
117 gnc_lot_end_handler (gpointer data_for_children,
118  GSList* data_from_children, GSList* sibling_data,
119  gpointer parent_data, gpointer global_data,
120  gpointer* result, const gchar* tag)
121 {
122  GNCLot* lot;
123  xmlNodePtr tree = (xmlNodePtr)data_for_children;
124  gxpf_data* gdata = (gxpf_data*)global_data;
125  QofBook* book = static_cast<decltype (book)> (gdata->bookdata);
126 
127  if (parent_data)
128  {
129  return TRUE;
130  }
131 
132  /* OK. For some messed up reason this is getting called again with a
133  NULL tag. So we ignore those cases */
134  if (!tag)
135  {
136  return TRUE;
137  }
138 
139  g_return_val_if_fail (tree, FALSE);
140 
141  lot = dom_tree_to_lot (tree, book);
142  ENTER ("(lot=%p)", lot);
143  if (lot != NULL)
144  {
145  gdata->cb (tag, gdata->parsedata, lot);
146  }
147 
148  xmlFreeNode (tree);
149 
150  LEAVE ("");
151  return lot != NULL;
152 }
153 
154 GNCLot*
155 dom_tree_to_lot (xmlNodePtr node, QofBook* book)
156 {
157  struct lot_pdata pdata;
158  GNCLot* lot;
159  gboolean successful;
160 
161  lot = gnc_lot_new (book);
162  ENTER ("(lot=%p)", lot);
163 
164  pdata.lot = lot;
165  pdata.book = book;
166 
167  successful = dom_tree_generic_parse (node, lot_handlers_v2,
168  &pdata);
169  if (!successful)
170  {
171  PERR ("failed to parse lot");
172  gnc_lot_destroy (lot);
173  lot = NULL;
174  }
175 
176  LEAVE ("");
177  return lot;
178 }
179 
180 sixtp*
181 gnc_lot_sixtp_parser_create (void)
182 {
183  return sixtp_dom_parser_new (gnc_lot_end_handler, NULL, NULL);
184 }
185 
186 /* ================== END OF FILE ========================== */
Definition: sixtp.h:129
#define PERR(format, args...)
Log a serious error.
Definition: qoflog.h:244
#define ENTER(format, args...)
Print a function entry debugging message.
Definition: qoflog.h:272
api for GnuCash version 2 XML-based file format
#define LEAVE(format, args...)
Print a function exit debugging message.
Definition: qoflog.h:282