1 /*
2  * The contents of this file are subject to the Mozilla Public
3  * License Version 1.1 (the "License"); you may not use this file
4  * except in compliance with the License. You may obtain a copy of
5  * the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS
8  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9  * implied. See the License for the specific language governing
10  * rights and limitations under the License.
11  *
12  * The Original Code is the Sablotron XSLT Processor.
13  *
14  * The Initial Developer of the Original Code is Ginger Alliance Ltd.
15  * Portions created by Ginger Alliance are Copyright (C) 2000-2002
16  * Ginger Alliance Ltd. All Rights Reserved.
17  *
18  * Contributor(s):
19  *
20  * Alternatively, the contents of this file may be used under the
21  * terms of the GNU General Public License Version 2 or later (the
22  * "GPL"), in which case the provisions of the GPL are applicable
23  * instead of those above.  If you wish to allow use of your
24  * version of this file only under the terms of the GPL and not to
25  * allow others to use your version of this file under the MPL,
26  * indicate your decision by deleting the provisions above and
27  * replace them with the notice and other provisions required by
28  * the GPL.  If you do not delete the provisions above, a recipient
29  * may use your version of this file under either the MPL or the
30  * GPL.
31  */
32 
33 
34 #ifndef BaseHIncl
35 #define BaseHIncl
36 
37 // GP: clean
38 
39 /****************************************
40 *                                       *
41     i n c l u d e s
42 *                                       *
43 ****************************************/
44 
45 #include <stdlib.h>
46 #include <stdio.h>
47 #include <assert.h>
48 #include <string.h>
49 
50 //#define SablotNSBegin namespace _sablotns {
51 //#define SablotNSEnd }
52 
53 #include "platform.h"
54 #include "sablot.h"
55 // #include "shandler.h"
56 
57 // see also end of file
58 
59 // **************************************
60 // enum for list sizes
61 
62 //start the internal namespace
63 
64 enum ListSize
65 {
66     LIST_SIZE_1 = 0,
67     LIST_SIZE_2 = 1,
68     LIST_SIZE_SMALL = 2,
69     LIST_SIZE_8 = 3,
70     LIST_SIZE_MEDIUM = 4,
71     LIST_SIZE_LARGE = 5,
72     LIST_SIZE_EXPR_TOKENS = LIST_SIZE_MEDIUM
73 };
74 
75 // powers of 2
76 #define TWO_TO(EXPONENT) (1L << EXPONENT)
77 
78 /****************************************
79 *                                       *
80     c o n s t a n t s
81 *                                       *
82 ****************************************/
83 
84 //maximum expression length in chars:
85 #define MAX_EXPR_LEN 80
86 
87 //max # of params and args passed to SablotProcessVA
88 #define ARG_COUNT 32
89 
90 // the size of the buffer allocated from expat
91 #define PARSE_BUFSIZE 16384
92 // this is for the buffer with raw text to convert
93 #define PARSE_CONV_BUFSIZE PARSE_BUFSIZE * 4
94 
95 #define EPS 1E-10
96 #define PRIORITY_NOMATCH -10e6
97 
98 #define NS_SEP '`'
99 
100 
101 /****************************************
102 *                                       *
103     e n u m s
104 *                                       *
105 ****************************************/
106 
107 // vertex type. To be replaced by OTYPE (Sablot object type) (??)
108 
109 enum VTYPE
110 {
111     VT_VERTEX,VT_ROOT,VT_ELEMENT,VT_ATTRIBUTE,VT_TEXT,
112     VT_PI, VT_COMMENT, VT_NAMESPACE,
113     VT_VERTEX_WF = VT_VERTEX,
114     VT_ATTRIBUTE_WF = VT_ATTRIBUTE,
115     VT_TEXT_WF = VT_TEXT,
116     VT_BASE = 0x000f,
117     VT_DADDY_FLAG = 0x2000,
118     VT_DADDY_WF = VT_VERTEX | VT_DADDY_FLAG,
119     VT_ELEMENT_WF = VT_ELEMENT | VT_DADDY_FLAG,  //WF = with flags
120     VT_ROOT_WF = VT_ROOT | VT_DADDY_FLAG,
121     VT_XSL = 0x4000,
122     VT_XSL_ELEMENT_WF = VT_XSL | VT_ELEMENT_WF,
123     VT_EXT = 0x8000,
124     VT_EXT_ELEMENT_WF = VT_EXT | VT_ELEMENT_WF,
125     VT_TOP_FOREIGN = 0x10000 //used during parse
126 };
127 
128 // SpeakMode. Used to distinguish between vertex output for logging and for 'official' output.
129 
130 enum SpeakMode
131 {
132     SM_NAME = 1,
133         SM_CONTENTS = 2,
134 
135     SM_OFFICIAL = SM_NAME | SM_CONTENTS,
136 
137         SM_INS_SPACES = 0x1000,
138         SM_DESCRIBE = 0x2000,
139         SM_ESCAPE = 0x4000
140 };
141 
142 /****************************************
143     X S L   e n u m s
144 ****************************************/
145 // the corresponding string tables are declared later and defined in base.cpp
146 
147 enum    XSL_OP
148 {
149     XSL_APPLY_IMPORTS, XSL_APPLY_TEMPLATES,
150     XSL_ATTRIBUTE, XSL_ATTRIBUTE_SET,
151     XSL_CALL_TEMPLATE, XSL_CHOOSE,
152     XSL_COMMENT, XSL_COPY, XSL_COPY_OF,
153     XSL_DECIMAL_FORMAT, XSL_ELEMENT,
154     XSL_FALLBACK, XSL_FOR_EACH,
155     XSL_IF, XSL_IMPORT,
156     XSL_INCLUDE, XSL_KEY,
157     XSL_MESSAGE, XSL_NAMESPACE_ALIAS,
158     XSL_NUMBER, XSL_OTHERWISE,
159     XSL_OUTPUT, XSL_PARAM,
160     XSL_PRESERVE_SPACE, XSL_PROCESSING_INSTR,
161     XSL_SORT, XSL_STRIP_SPACE,
162     XSL_STYLESHEET, XSL_TEMPLATE,
163     XSL_TEXT, XSL_TRANSFORM,
164     XSL_VALUE_OF, XSL_VARIABLE,
165     XSL_WHEN, XSL_WITH_PARAM,
166     XSL_NONE
167 };
168 
169 enum XSL_ATT
170 {
171     XSLA_CASE_ORDER, XSLA_CDATA_SECT_ELEMS, XSLA_COUNT,
172     XSLA_DATA_TYPE, XSLA_DECIMAL_SEPARATOR, XSLA_DIGIT, XSLA_DISABLE_OUTPUT_ESC, XSLA_DOCTYPE_PUBLIC, XSLA_DOCTYPE_SYSTEM,
173     XSLA_ELEMENTS, XSLA_ENCODING, XSLA_EXCL_RES_PREFIXES, XSLA_EXT_ELEM_PREFIXES,
174     XSLA_FORMAT, XSLA_FROM,
175     XSLA_GROUPING_SEPARATOR, XSLA_GROUPING_SIZE,
176     XSLA_HREF,
177     XSLA_ID, XSLA_INDENT, XSLA_INFINITY,
178     XSLA_LANG, XSLA_LETTER_VALUE, XSLA_LEVEL,
179     XSLA_MATCH, XSLA_MEDIA_TYPE, XSLA_METHOD, XSLA_MINUS_SIGN, XSLA_MODE,
180     XSLA_NAME, XSLA_NAMESPACE, XSLA_NAN,
181     XSLA_OMIT_XML_DECL, XSLA_ORDER,
182     XSLA_PATTERN_SEPARATOR, XSLA_PERCENT, XSLA_PER_MILLE, XSLA_PRIORITY,
183     XSLA_RESULT_PREFIX,
184     XSLA_SELECT, XSLA_STANDALONE, XSLA_STYLESHEET_PREFIX,
185     XSLA_TERMINATE, XSLA_TEST,
186     XSLA_USE, XSLA_USE_ATTR_SETS,
187     XSLA_VALUE, XSLA_VERSION,
188     XSLA_ZERO_DIGIT,
189     XSLA_NONE
190 };
191 
192 enum ExAxis
193 {
194     AXIS_ANCESTOR, AXIS_ANC_OR_SELF, AXIS_ATTRIBUTE, AXIS_CHILD,
195         AXIS_DESCENDANT, AXIS_DESC_OR_SELF, AXIS_FOLLOWING, AXIS_FOLL_SIBLING,
196         AXIS_NAMESPACE, AXIS_PARENT, AXIS_PRECEDING, AXIS_PREC_SIBLING,
197         AXIS_SELF,
198         AXIS_NONE,
199 
200         //not recognized in stylesheet:
201         AXIS_ROOT
202 };
203 
204 enum ExNodeType
205 {
206     EXNODE_NODE,
207     EXNODE_TEXT,
208     EXNODE_PI,
209     EXNODE_COMMENT,
210     EXNODE_NONE
211 };
212 
213 /****************************************
214 *                                       *
215     t y p e d e f s
216 *                                       *
217 ****************************************/
218 
219 /*    the following was truly a burden:
220 enum Bool
221 {
222     FALSE = 0,
223         TRUE = 1
224 };
225 */
226 
227 #define ITEM_NOT_FOUND      (unsigned long) -1L
228 #define PHRASE_NOT_FOUND    ITEM_NOT_FOUND
229 #define UNDEF_PHRASE        (unsigned long) -2L
230 
231 typedef unsigned long oolong;
232 typedef oolong HashId;
233 typedef HashId Phrase;
234 
235 typedef int Bool;
236 #ifndef FALSE
237 #define FALSE 0
238 #endif
239 #ifndef TRUE
240 #define TRUE 1
241 #endif
242 
243 
244 /****************************************
245 *                                       *
246     m a c r o s
247 *                                       *
248 ****************************************/
249 
250 //this gets rid of warnings on platforms where pointer is
251 //sized differently from boolean
252 #define sabassert(x) (assert(!!(x)))
253 
254 #define NONZERO(x) (sabassert(x),x)
255 #define NZ(x) NONZERO(x)
256 #define cdelete(PTR) {if (PTR) delete PTR; PTR = NULL;}
257 #define ccdelete(PTR, TYPED) {if (PTR) delete TYPED; PTR = NULL;}
258 #define cdeleteArr(PTR) {if (PTR) delete[] PTR; PTR = NULL;}
259 
260 #ifdef _DEBUG
261 #define cast(TYPE,PTR) (NZ(dynamic_cast<TYPE>(PTR)))
262 #else
263 #define cast(TYPE,PTR) ((TYPE) PTR)
264 #endif
265 
266 // macros for checking vertex types
267 
268 #define baseType(v) (((Vertex*)NZ(v)) -> vt & VT_BASE)
269 #define basetype(v) baseType(v)
270 #define isDaddy(v) (((Vertex*)NZ(v)) -> vt & VT_DADDY_FLAG)
271 #define isRoot(v) (baseType(v) == VT_ROOT)
272 #define isPI(v) (baseType(v) == VT_PI)
273 #define isComment(v) (baseType(v) == VT_COMMENT)
274 #define isElement(v) (baseType(v) == VT_ELEMENT || baseType(v) == VT_ROOT)
275 #define isText(v) (baseType(v) == VT_TEXT)
276 #define isXSL(v) (((Vertex*)NZ(v)) -> vt & VT_XSL)
277 #define isExt(v) (((Vertex*)NZ(v)) -> vt & VT_EXT)
278 #define isXSLElement(v) ((isElement(v)) && (isXSL(v)))
279 #define isExtension(v) ((isElement(v)) && (isExt(v)))
280 #define isAttr(v) (baseType(v) == VT_ATTRIBUTE)
281 #define isNS(v) (baseType(v) == VT_NAMESPACE)
282 
283 // macros for downcasting
284 
285 #define toV(v) ((Vertex*)v)
286 #define toNS(v) (cast(NmSpace*,toV(v)))
287 #define toE(v) (cast(Element*,toV(v)))
288 #define toX(v) (cast(XSLElement*,toV(v)))
289 #define toExtension(v) (cast(ExtensionElement*,toV(v)))
290 #define toA(v) (cast(Attribute*,toV(v)))
291 #define toText(v) (cast(Text*,toV(v)))
292 #define toD(v) (cast(Daddy*,toV(v)))
293 #define toRoot(v) (cast(RootNode*,toV(v)))
294 #define toComment(v) (cast(Comment*,toV(v)))
295 #define toPI(v) (cast(ProcInstr*,toV(v)))
296 // for "casting" a root node to the containing document
297 #define toTree(v) (&(toRoot(v) -> getOwner()))
298 // casts a Tree object to an SXP_Document
299 #define toSXP_Document(t) &((t).getRoot())
300 
301 #define toPhysical(v) toV(v)
302 
303 #define OK FALSE
304 #define NOT_OK TRUE
305 #define eFlag Bool
306 #define BOOL Bool
307 
308 // return OK, putting a VALUE in a VARiable
309 #define RetOK( VAR, VALUE ) {VAR = VALUE; return OK;}
310 
311 // to support the change to Unicode
312 #define Mchar char
313 #define Mstrlen strlen
314 #define Mstrchr strchr
315 #define Mstrcmp strcmp
316 #define Mstrncmp strncmp
317 
318 // parsing macros
319 
320 extern const char* theWhitespace;
321 #define skipWhite(p)    p+=strspn((p), theWhitespace)
322 #define isWhite(c)  (!!(strchr(theWhitespace,(c))))
323 #define isAllWhite( STR ) (!((STR)[strspn((STR), theWhitespace)]))
324 
325 // messaging macro from "Advanced Windows"
326 // #pragma Msg(some_message) emits a message at compile time
327 #define MsgToS(x) #x
328 #define MsgToS2(x) MsgToS(x)
329 #define Msg(x) message(__FILE__"(" MsgToS2(__LINE__) "): "#x"        <---")
330 
331 /****************************************
332 *                                       *
333     e x t e r n s
334 *                                       *
335 ****************************************/
336 
337 extern const char* xslOpNames[];
338 extern const char* xslAttNames[];
339 extern const char* axisNames[];
340 extern const char* exNodeTypeNames[];
341 extern const char* vertexTypeNames[];
342 extern int lookup(const char* str, const char** table);
343 extern int lookupNoCase(const char* str, const char** table);
344 extern int fcomp(double,double);        //float comparison
345 extern Bool isstd(const char *);
346 extern int stdclose(FILE *f);
347 extern FILE* stdopen(const char *fn, const char *mode);
348 extern Bool strEqNoCase(const char*, const char*);
349 //
350 class SituationObj;
351 // extern Processor* proc;
352 // extern SituationObj situation;
353 //extern void pushNewHandler();
354 //extern void popNewHandler();
355 
356 extern SchemeHandler *theSchemeHandler;
357 extern MessageHandler *theMessageHandler;
358 extern SAXHandler *theSAXHandler;
359 extern void* theSAXUserData;
360 
361 //XML name checks
362 extern Bool isValidNCName(const char* name);
363 extern Bool isValidQName(const char* name);
364 
365 //string parsing
366 class Str;
367 extern Bool getWhDelimString(char *&list, Str& firstPart);
368 
369 /*
370   the XSLT namespace
371 */
372 
373 /* class Str; */
374 extern const char *theXSLTNamespace, *oldXSLTNamespace, *theXMLNamespace,
375   *theXHTMLNamespace, *theXMLNSNamespace, *theSabExtNamespace,
376   *theEXSLTDynNamespace;
377 
378 //
379 //  escapes
380 //
381 
382 extern const char
383     *escNewline, *escTab, *escQuote, *escApos,
384     *escLess, *escGreater;
385 
386 /****************************************
387 *                                       *
388     c l a s s e s
389 *                                       *
390 ****************************************/
391 
392 /*
393 class SabObj
394 {
395 public:
396     void *operator new(unsigned int);
397     void operator delete(void *, unsigned int);
398 };
399 */
400 
401 /*****************************************************************
402 Memory leak test stuff
403 *****************************************************************/
404 #if defined(CHECK_LEAKS)
405 #ifdef WIN32
406 #define CRTDBG_MAP_ALLOC
407 #include <crtdbg.h>
408 #else // if defined(__linux__)
409 #include <mcheck.h>
410 #endif
411 #endif
412 
413 extern void checkLeak();
414 extern void memStats();
415 
416 class Str;
417 extern Str getMillisecsDiff(double originalTime);
418 
419 
420 /****************************************
421 *                                       *
422     f i n a l   i n c l u d e s
423 *                                       *
424 ****************************************/
425 
426 #ifndef BaseH_NoErrorH
427 #include "situa.h"
428 #else
429 #undef BaseH_NoErrorH
430 #endif
431 
432 #ifdef MPATROL
433 #include <mpatrol.h>
434 #endif
435 
436 #endif //ifndef BaseHIncl
437