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  GncGUID* guid;
86 
87  ENTER ("(lot=%p)", pdata->lot);
88  guid = dom_tree_to_guid (node);
89  gnc_lot_set_guid (pdata->lot, *guid);
90 
91  guid_free (guid);
92 
93  LEAVE ("");
94  return TRUE;
95 }
96 
97 static gboolean
98 lot_slots_handler (xmlNodePtr node, gpointer p)
99 {
100  struct lot_pdata* pdata = static_cast<decltype (pdata)> (p);
101  gboolean success;
102 
103  ENTER ("(lot=%p)", pdata->lot);
104  success = dom_tree_create_instance_slots (node, QOF_INSTANCE (pdata->lot));
105 
106  LEAVE ("");
107  g_return_val_if_fail (success, FALSE);
108  return TRUE;
109 }
110 
111 
112 static struct dom_tree_handler lot_handlers_v2[] =
113 {
114  { lot_id_string, lot_id_handler, 1, 0 },
115  { lot_slots_string, lot_slots_handler, 0, 0 },
116  { NULL, 0, 0, 0 }
117 };
118 
119 static gboolean
120 gnc_lot_end_handler (gpointer data_for_children,
121  GSList* data_from_children, GSList* sibling_data,
122  gpointer parent_data, gpointer global_data,
123  gpointer* result, const gchar* tag)
124 {
125  GNCLot* lot;
126  xmlNodePtr tree = (xmlNodePtr)data_for_children;
127  gxpf_data* gdata = (gxpf_data*)global_data;
128  QofBook* book = static_cast<decltype (book)> (gdata->bookdata);
129 
130  if (parent_data)
131  {
132  return TRUE;
133  }
134 
135  /* OK. For some messed up reason this is getting called again with a
136  NULL tag. So we ignore those cases */
137  if (!tag)
138  {
139  return TRUE;
140  }
141 
142  g_return_val_if_fail (tree, FALSE);
143 
144  lot = dom_tree_to_lot (tree, book);
145  ENTER ("(lot=%p)", lot);
146  if (lot != NULL)
147  {
148  gdata->cb (tag, gdata->parsedata, lot);
149  }
150 
151  xmlFreeNode (tree);
152 
153  LEAVE ("");
154  return lot != NULL;
155 }
156 
157 GNCLot*
158 dom_tree_to_lot (xmlNodePtr node, QofBook* book)
159 {
160  struct lot_pdata pdata;
161  GNCLot* lot;
162  gboolean successful;
163 
164  lot = gnc_lot_new (book);
165  ENTER ("(lot=%p)", lot);
166 
167  pdata.lot = lot;
168  pdata.book = book;
169 
170  successful = dom_tree_generic_parse (node, lot_handlers_v2,
171  &pdata);
172  if (!successful)
173  {
174  PERR ("failed to parse lot");
175  gnc_lot_destroy (lot);
176  lot = NULL;
177  }
178 
179  LEAVE ("");
180  return lot;
181 }
182 
183 sixtp*
184 gnc_lot_sixtp_parser_create (void)
185 {
186  return sixtp_dom_parser_new (gnc_lot_end_handler, NULL, NULL);
187 }
188 
189 /* ================== 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
The type used to store guids in C.
Definition: guid.h:75