Debian Codegraph @

src/tinyxpath/xpath_stack.cpp

raw symbols SCIP
1 /*
2 www.sourceforge.net/projects/tinyxpath
3 Copyright (c) 2002-2004 Yves Berquin ([email protected])
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any
7 damages arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any
10 purpose, including commercial applications, and to alter it and
11 redistribute it freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must
14 not claim that you wrote the original software. If you use this
15 software in a product, an acknowledgment in the product documentation
16 would be appreciated but is not required.
17
18 2. Altered source versions must be plainly marked as such, and
19 must not be misrepresented as being the original software.
20
21 3. This notice may not be removed or altered from any source
22 distribution.
23 */
24
25 /**
26 \file xpath_stack.cpp
27 \author Yves Berquin
28 */
29
30 #include "xpath_stack.h"
31 #include "xpath_processor.h"
32
33 namespace TinyXPath
34 {
35
36 /// xpath_stack element. Derives from expression_result, and also contains a pointer to the next element on the stack
37 class result_and_next : public expression_result
38 {
39 public :
40 /// Constructor
41 result_and_next (expression_result er_in, result_and_next * rnp_in_next) :
42 expression_result (er_in)
43 {
44 rnp_next = rnp_in_next;
45 }
46 /// Return next element
47 result_and_next * rnp_get_next ()
48 {
49 return rnp_next;
50 }
51 #ifdef TINYXPATH_DEBUG
52 void v_dump ();
53 #endif
54 protected :
55 /// Next element in the stack
56 result_and_next * rnp_next;
57 } ;
58
59 #ifdef TINYXPATH_DEBUG
60 /// Dump a stack element to stdout
61 void result_and_next::v_dump ()
62 {
63 expression_result::v_dump ();
64 }
65 #endif
66
67 /// Constructor
68 xpath_stack::xpath_stack ()
69 {
70 rnp_first = NULL;
71 u_size = 0;
72 }
73
74 /// Destructor
75 xpath_stack::~ xpath_stack ()
76 {
77 while (rnp_first)
78 v_pop_one ();
79 }
80
81 /// Pops the top integer from the stack
82 int xpath_stack::i_pop_int ()
83 {
84 int i_res;
85
86 i_res = erp_top () -> i_get_int ();
87 v_pop ();
88 return i_res;
89 }
90
91 /// Pops the top string from the stack
92 TIXML_STRING xpath_stack::S_pop_string ()
93 {
94 TIXML_STRING S_res;
95
96 S_res = erp_top () -> S_get_string ();
97 v_pop ();
98 return S_res;
99 }
100
101 /// Pops the top bool from the stack
102 bool xpath_stack::o_pop_bool ()
103 {
104 bool o_res;
105
106 o_res = erp_top () -> o_get_bool ();
107 v_pop ();
108 return o_res;
109 }
110
111 /// Pops the top node_set from the stack
112 node_set xpath_stack::ns_pop_node_set ()
113 {
114 node_set ns_ret;
115
116 ns_ret = * (erp_top () -> nsp_get_node_set ());
117 v_pop ();
118 return ns_ret;
119 }
120
121 /// Query the top integer
122 int xpath_stack::i_top_int ()
123 {
124 int i_res;
125
126 i_res = erp_top () -> i_get_int ();
127 return i_res;
128 }
129
130 /// Push a new element on the stack
131 void xpath_stack::v_push (expression_result er_res)
132 {
133 result_and_next * rnp_new;
134
135 rnp_new = new result_and_next (er_res, rnp_first);
136 rnp_first = rnp_new;
137 u_size++;
138 }
139
140 /// Push an integer on the stack
141 void xpath_stack::v_push_int (int i_elem, const char * cp_comment)
142 {
143 expression_result er_res;
144 er_res . v_set_int (i_elem);
145 if (cp_comment)
146 er_res . v_set_comment (cp_comment);
147 v_push (er_res);
148 }
149
150 /// Push a double on the stack
151 void xpath_stack::v_push_double (double d_elem)
152 {
153 expression_result er_res;
154 er_res . v_set_double (d_elem);
155 v_push (er_res);
156 }
157
158 /// Push a node_set on the stack
159 void xpath_stack::v_push_node_set (node_set * nsp_ptr)
160 {
161 expression_result er_res;
162 er_res . v_set_node_set (nsp_ptr);
163 v_push (er_res);
164 }
165
166 /// Push a bool on the stack
167 void xpath_stack::v_push_bool (bool o_in)
168 {
169 expression_result er_res;
170 er_res . v_set_bool (o_in);
171 v_push (er_res);
172 }
173
174 /// Push a string on the stack
175 void xpath_stack::v_push_string (TIXML_STRING S_in)
176 {
177 expression_result er_res;
178 er_res . v_set_string (S_in);
179 v_push (er_res);
180 }
181
182 /// Retrieve top expression from the stack
183 expression_result * xpath_stack::erp_top ()
184 {
185 if (! rnp_first)
186 throw execution_error (34);
187 return rnp_first;
188 }
189
190 /// Retrieve a previous expression from the stack
191 expression_result * xpath_stack::erp_previous (
192 unsigned u_nb) ///< Nb of items to skip.
193 {
194 result_and_next * rnp_current;
195 unsigned u_prev;
196
197 assert (u_nb <= u_size);
198 rnp_current = rnp_first;
199 for (u_prev = 0; u_prev < u_nb; u_prev++)
200 {
201 assert (rnp_current);
202 rnp_current = rnp_current -> rnp_get_next ();
203 }
204 return rnp_current;
205 }
206
207 /// Pop one element from the stack
208 void xpath_stack::v_pop_one ()
209 {
210 result_and_next * rnp_old;
211
212 if (! u_size)
213 throw execution_error (35);
214 if (! rnp_first)
215 throw execution_error (36);
216 rnp_old = rnp_first;
217 rnp_first = rnp_first -> rnp_get_next ();
218 delete rnp_old;
219 u_size--;
220 }
221
222 /// Pop N element from the stack
223 void xpath_stack::v_pop (unsigned u_nb)
224 {
225 unsigned u_elem;
226
227 for (u_elem = 0; u_elem < u_nb; u_elem++)
228 v_pop_one ();
229 }
230
231 #ifdef TINYXPATH_DEBUG
232 /// Dumps the content of the stack to stdout
233 void xpath_stack::v_dump ()
234 {
235 result_and_next * rnp_ptr;
236
237 printf ("--- stack start (%d elem) ---\n", u_size);
238 rnp_ptr = rnp_first;
239 while (rnp_ptr)
240 {
241 rnp_ptr -> v_dump ();
242 rnp_ptr = rnp_ptr -> rnp_get_next ();
243 }
244 printf ("--- stack end ---\n");
245 }
246 #endif
247
248 }