GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
sixtp-stack.c
1 /********************************************************************
2  * sixtp-stack.c *
3  * Copyright 2001 Gnumatic, Inc. *
4  * *
5  * This program is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU General Public License as *
7  * published by the Free Software Foundation; either version 2 of *
8  * the License, or (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License*
16  * along with this program; if not, contact: *
17  * *
18  * Free Software Foundation Voice: +1-617-542-5942 *
19  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
20  * Boston, MA 02110-1301, USA [email protected] *
21  * *
22  ********************************************************************/
23 
24 #include "config.h"
25 
26 #include "sixtp.h"
27 #include "sixtp-stack.h"
28 
29 void
30 sixtp_stack_frame_destroy(sixtp_stack_frame *sf)
31 {
32  GSList *lp;
33 
34  /* cleanup all the child data */
35  for (lp = sf->data_from_children; lp; lp = lp->next)
36  {
37  sixtp_child_result_destroy((sixtp_child_result *) lp->data);
38  }
39  g_slist_free(sf->data_from_children);
40  sf->data_from_children = NULL;
41 
42  g_free(sf);
43 }
44 
46 sixtp_stack_frame_new(sixtp* next_parser, char *tag)
47 {
48  sixtp_stack_frame* new_frame;
49 
50  new_frame = g_new0(sixtp_stack_frame, 1);
51  new_frame->parser = next_parser;
52  new_frame->tag = tag;
53  new_frame->data_for_children = NULL;
54  new_frame->data_from_children = NULL;
55  new_frame->frame_data = NULL;
56  new_frame->line = new_frame->col = -1;
57 
58  return new_frame;
59 }
60 
61 void
62 sixtp_stack_frame_print(sixtp_stack_frame *sf, gint indent, FILE *f)
63 {
64  gchar *is = g_strnfill(indent, ' ');
65 
66  fprintf(f, "%s(stack-frame %p\n", is, sf);
67  fprintf(f, "%s (line %d) (col %d)\n", is, sf->line, sf->col );
68  fprintf(f, "%s (parser %p)\n", is, sf->parser);
69  fprintf(f, "%s (tag %s)\n", is, sf->tag ? sf->tag : "(null)");
70  fprintf(f, "%s (data-for-children %p)\n", is,
71  sf->data_for_children);
72 
73  {
74  GSList *lp;
75  fprintf(f, "%s (data-from-children", is);
76  for (lp = sf->data_from_children; lp; lp = lp->next)
77  {
78  fputc(' ', f);
79  sixtp_child_result_print((sixtp_child_result *) lp->data, f);
80  }
81  fprintf(f, ")\n");
82  }
83 
84  fprintf(f, "%s (frame-data %p))\n", is, sf->frame_data);
85  fflush(f);
86  g_free(is);
87 }
88 
89 GSList*
90 sixtp_pop_and_destroy_frame(GSList *frame_stack)
91 {
92  sixtp_stack_frame *dead_frame = (sixtp_stack_frame *) frame_stack->data;
93  GSList* result;
94 
95  result = g_slist_next(frame_stack);
96  sixtp_stack_frame_destroy(dead_frame);
97  g_slist_free_1(frame_stack);
98  return(result);
99 }
100 
101 void
102 sixtp_print_frame_stack(GSList *stack, FILE *f)
103 {
104  /* first, some debugging output */
105  GSList *printcopy = g_slist_reverse(g_slist_copy(stack));
106  GSList *lp;
107  int indent = 0;
108 
109  for (lp = printcopy; lp; lp = lp->next)
110  {
111  sixtp_stack_frame *frame = (sixtp_stack_frame *) lp->data;
112  sixtp_stack_frame_print(frame, indent, f);
113  indent += 2;
114  }
115 
116 }
117 
118 
119 /* Parser context */
121 sixtp_context_new(sixtp *initial_parser, gpointer global_data,
122  gpointer top_level_data)
123 {
125 
126  ret = g_new0(sixtp_parser_context, 1);
127 
128  ret->handler.startElement = sixtp_sax_start_handler;
129  ret->handler.endElement = sixtp_sax_end_handler;
130  ret->handler.characters = sixtp_sax_characters_handler;
131  ret->handler.getEntity = sixtp_sax_get_entity_handler;
132 
133  ret->data.parsing_ok = TRUE;
134  ret->data.stack = NULL;
135  ret->data.global_data = global_data;
136 
137  ret->top_frame = sixtp_stack_frame_new(initial_parser, NULL);
138 
139  ret->top_frame_data = top_level_data;
140 
141  ret->data.stack = g_slist_prepend(ret->data.stack,
142  (gpointer) ret->top_frame);
143 
144  if (initial_parser->start_handler)
145  {
146  if (!initial_parser->start_handler(NULL,
147  &ret->top_frame_data,
148  &ret->data.global_data,
149  &ret->top_frame->data_for_children,
150  &ret->top_frame->frame_data,
151  NULL, NULL))
152  {
153  sixtp_handle_catastrophe(&ret->data);
154  sixtp_context_destroy(ret);
155  return NULL;
156  }
157  }
158 
159  return ret;
160 }
161 
162 void
163 sixtp_context_run_end_handler(sixtp_parser_context* ctxt)
164 {
165  if (ctxt->top_frame->parser->end_handler)
166  {
167  ctxt->data.parsing_ok &=
168  ctxt->top_frame->parser->end_handler(
169  ctxt->top_frame->data_for_children,
170  ctxt->top_frame->data_from_children,
171  NULL,
172  ctxt->top_frame_data,
173  ctxt->data.global_data,
174  &ctxt->top_frame->frame_data,
175  NULL);
176  }
177 }
178 
179 void
180 sixtp_context_destroy(sixtp_parser_context* context)
181 {
182  sixtp_stack_frame_destroy(context->top_frame);
183  g_slist_free(context->data.stack);
184  context->data.saxParserCtxt->userData = NULL;
185  context->data.saxParserCtxt->sax = NULL;
186  xmlFreeParserCtxt(context->data.saxParserCtxt);
187  context->data.saxParserCtxt = NULL;
188  g_free(context);
189 }
Definition: sixtp.h:93