1 | /**********************************************************************
|
---|
2 | *
|
---|
3 | * display.h -- Context sensitive macro language
|
---|
4 | * Copyright (C) 1999 The New Zealand Digital Library Project
|
---|
5 | *
|
---|
6 | * PUT COPYRIGHT NOTICE HERE
|
---|
7 | *
|
---|
8 | * $Id: display.h 182 1999-03-01 20:39:56Z sjboddie $
|
---|
9 | *
|
---|
10 | *********************************************************************/
|
---|
11 |
|
---|
12 |
|
---|
13 | #ifndef DISPLAY_H
|
---|
14 | #define DISPLAY_H
|
---|
15 |
|
---|
16 | #include "gsdlconf.h"
|
---|
17 | #include "text_t.h"
|
---|
18 |
|
---|
19 |
|
---|
20 | #if defined(GSDL_USE_OBJECTSPACE)
|
---|
21 | # include <ospace\std\vector>
|
---|
22 | # include <ospace\std\list>
|
---|
23 | # include <ospace\std\set>
|
---|
24 | # include <ospace\std\map>
|
---|
25 | # include <ospace\std\algorithm>
|
---|
26 | #elif defined(GSDL_USE_STL_H)
|
---|
27 | # include <vector.h>
|
---|
28 | # include <list.h>
|
---|
29 | # include <set.h>
|
---|
30 | # include <map.h>
|
---|
31 | # if defined(GSDL_USE_ALGO_H)
|
---|
32 | # include <algo.h>
|
---|
33 | # else
|
---|
34 | # include <algorithm.h>
|
---|
35 | # endif
|
---|
36 | #else
|
---|
37 | # include <vector>
|
---|
38 | # include <list>
|
---|
39 | # include <set>
|
---|
40 | # include <map>
|
---|
41 | # include <algorithm>
|
---|
42 | #endif
|
---|
43 |
|
---|
44 | #if defined(GSDL_USE_OBJECTSPACE)
|
---|
45 | # include <ospace\std\iostream>
|
---|
46 | # include <ospace\std\fstream>
|
---|
47 | #elif defined(GSDL_USE_IOS_H)
|
---|
48 | # include <iostream.h>
|
---|
49 | # include <fstream.h>
|
---|
50 | #else
|
---|
51 | # include <iostream>
|
---|
52 | # include <fstream>
|
---|
53 | #endif
|
---|
54 |
|
---|
55 |
|
---|
56 | // use the standard namespace
|
---|
57 | #if defined(GSDL_USE_OBJECTSPACE)
|
---|
58 | using namespace ospace::std;
|
---|
59 | #elif !defined(GSDL_NAMESPACE_BROKEN)
|
---|
60 | using namespace std;
|
---|
61 | #endif
|
---|
62 |
|
---|
63 |
|
---|
64 |
|
---|
65 | // MAXRECURSIONDEPTH is a cutoff to catch
|
---|
66 | // cyclic macros (a includes b and b includes a)
|
---|
67 | #define MAXRECURSIONDEPTH 30
|
---|
68 |
|
---|
69 |
|
---|
70 | // class prototypes
|
---|
71 | class defaultmacros_t;
|
---|
72 | class currentmacros_t;
|
---|
73 |
|
---|
74 | // a few supporting types
|
---|
75 | // fileelement isn't finished yet, I have to find out
|
---|
76 | // more about getting information about files
|
---|
77 | struct fileinfoelement
|
---|
78 | {
|
---|
79 | int otherinfo;
|
---|
80 | };
|
---|
81 |
|
---|
82 | inline bool operator==(const fileinfoelement &x, const fileinfoelement &y) {
|
---|
83 | return (x.otherinfo==y.otherinfo);
|
---|
84 | }
|
---|
85 |
|
---|
86 | inline bool operator<(const fileinfoelement &x, const fileinfoelement &y) {
|
---|
87 | return (x.otherinfo<y.otherinfo);
|
---|
88 | }
|
---|
89 |
|
---|
90 | typedef map<text_t, fileinfoelement, lttext_t> fileinfomap;
|
---|
91 |
|
---|
92 |
|
---|
93 | typedef map<text_t, text_t, lttext_t> paramhashtype;
|
---|
94 |
|
---|
95 |
|
---|
96 | struct paramspec
|
---|
97 | {
|
---|
98 | double spec;
|
---|
99 | text_t param;
|
---|
100 | };
|
---|
101 |
|
---|
102 | inline bool operator==(const paramspec& x, const paramspec& y)
|
---|
103 | {
|
---|
104 | return ((x.spec == y.spec) && (x.param == y.param));
|
---|
105 | }
|
---|
106 |
|
---|
107 | inline bool operator!=(const paramspec& x, const paramspec& y)
|
---|
108 | {
|
---|
109 | return ((x.spec != y.spec) || (x.param != y.param));
|
---|
110 | }
|
---|
111 |
|
---|
112 | // note: paramspec is sorted by reverse spec and then param
|
---|
113 |
|
---|
114 | inline bool operator<(const paramspec& x, const paramspec& y)
|
---|
115 | {
|
---|
116 | return ((x.spec > y.spec) ||
|
---|
117 | ((x.spec == y.spec) && (x.param < y.param)));
|
---|
118 | }
|
---|
119 |
|
---|
120 | inline bool operator>(const paramspec& x, const paramspec& y)
|
---|
121 | {
|
---|
122 | return ((x.spec < y.spec) ||
|
---|
123 | ((x.spec == y.spec) && (x.param > y.param)));
|
---|
124 | }
|
---|
125 |
|
---|
126 | typedef vector<paramspec> paramspeclist;
|
---|
127 |
|
---|
128 |
|
---|
129 | // NOTE: when macros are used within text they should have
|
---|
130 | // underscores '_' on both sides, however, in calls to
|
---|
131 | // setdefaultmacro and setmacro they should not have the
|
---|
132 | // underscores. So you might have
|
---|
133 | //
|
---|
134 | // setmacro ("aname", "query", " some text ");
|
---|
135 | // expandstring ("_aname_", expandedtext);
|
---|
136 | //
|
---|
137 | // because the input to expandstring is a block of text, not
|
---|
138 | // a macroname
|
---|
139 |
|
---|
140 | class displayclass
|
---|
141 | {
|
---|
142 | public:
|
---|
143 | displayclass ();
|
---|
144 | ~displayclass ();
|
---|
145 |
|
---|
146 | // isdefaultmacro sees if there is an entry in the list of
|
---|
147 | // default macros with the given package and macro name
|
---|
148 | // returns 0 if no macros in the package or in the global package
|
---|
149 | // were found
|
---|
150 | // 1 if no macros in the package were found but a macro
|
---|
151 | // in the global package was found
|
---|
152 | // 2 if a macro in the given package was found
|
---|
153 | int isdefaultmacro (text_t package, const text_t ¯oname);
|
---|
154 |
|
---|
155 | // setdefaultmacro adds an entry to the list of default macros
|
---|
156 | // returns 0 if there was no error,
|
---|
157 | // -1 if it redefined a macro
|
---|
158 | // -2 if it hid a Global macro
|
---|
159 | // -3 if it redefined a macro and hid a Global macro
|
---|
160 | // -4 if no macroname was supplied
|
---|
161 | int setdefaultmacro (text_t package, const text_t ¯oname,
|
---|
162 | text_t params, const text_t ¯ovalue);
|
---|
163 |
|
---|
164 | // loads a default macro file (if it isn't already loaded)
|
---|
165 | // returns 0 if didn't need to load the file (it was already loaded)
|
---|
166 | // 1 if was (re)loaded
|
---|
167 | // -1 an error occurred while trying to load the file
|
---|
168 | int loaddefaultmacros (text_t thisfilename);
|
---|
169 |
|
---|
170 | // prepares to create a page: deletes all macros set with
|
---|
171 | // 'setmacro', checks all the default macro files to see
|
---|
172 | // if any need reloading, and sets the page parameters.
|
---|
173 | void openpage (const text_t &thispageparams,
|
---|
174 | const text_t &thisprecedence);
|
---|
175 |
|
---|
176 | // changes the parameters for the current page.
|
---|
177 | void setpageparams (text_t thispageparams,
|
---|
178 | text_t thisprecedence);
|
---|
179 |
|
---|
180 | // overrides (or sets) a macro for the current page.
|
---|
181 | // returns 0 if there was no error,
|
---|
182 | // -1 if it redefined a macro
|
---|
183 | // -4 if no macroname was supplied
|
---|
184 | int setmacro (const text_t ¯oname,
|
---|
185 | text_t package,
|
---|
186 | const text_t ¯ovalue);
|
---|
187 |
|
---|
188 | void expandstring (const text_t &inputtext, text_t &outputtext);
|
---|
189 | void expandstring (text_t package, const text_t &inputtext,
|
---|
190 | text_t &outputtext, int recursiondepth = 0);
|
---|
191 |
|
---|
192 | // these functions are not ment to be used directly, they are used to permit
|
---|
193 | // concise stream output like:
|
---|
194 | // cout << text_t2ascii << display << "_amacro_" << "_anothermacro_";
|
---|
195 | void setconvertclass (outconvertclass *theoutc) {outc = theoutc;}
|
---|
196 | outconvertclass *getconvertclass () {return outc;}
|
---|
197 |
|
---|
198 | // say where any error logging goes, this can be NULL
|
---|
199 | // if no error logging is desired
|
---|
200 | void setlogout (ostream *thelogout) {logout = thelogout;}
|
---|
201 |
|
---|
202 | // debug stuff
|
---|
203 | void printdefaultmacros ();
|
---|
204 | void printallparams ();
|
---|
205 |
|
---|
206 | protected:
|
---|
207 | // special variables to permit trickly output
|
---|
208 | outconvertclass *outc;
|
---|
209 |
|
---|
210 | // general variables
|
---|
211 | text_tset allparams; // possible parameter combinations
|
---|
212 | defaultmacros_t *defaultmacros;
|
---|
213 | fileinfomap *defaultfiles;
|
---|
214 |
|
---|
215 | // variables to do with a page expansion
|
---|
216 | text_t params;
|
---|
217 | text_t precedence;
|
---|
218 | paramspeclist *orderparamlist; // ordered by specificness
|
---|
219 | currentmacros_t *currentmacros; // only macros set by setmacro
|
---|
220 |
|
---|
221 | // logging variable
|
---|
222 | ostream *logout;
|
---|
223 |
|
---|
224 | // reloads any default macro files which have changed
|
---|
225 | // returns 0 no errors occurred
|
---|
226 | // -1 an error occurred while trying to load one of the files
|
---|
227 | int checkdefaultmacrofiles ();
|
---|
228 |
|
---|
229 | int setdefaultmacro (text_t package, const text_t ¯oname,
|
---|
230 | text_t params, const text_t &filename,
|
---|
231 | const text_t ¯ovalue);
|
---|
232 |
|
---|
233 | // evaluates a boolean expression
|
---|
234 | bool boolexpr (text_t package, const text_t &expr, int recursiondepth);
|
---|
235 |
|
---|
236 | // (recursively) expand out a macro
|
---|
237 | // returns true if the macro was found
|
---|
238 | // false if the macro was not found
|
---|
239 | bool macro (const text_t ¯oname, text_t macropackage,
|
---|
240 | const text_t ¯oparam, text_t &outputtext,
|
---|
241 | int recursiondepth);
|
---|
242 | };
|
---|
243 |
|
---|
244 |
|
---|
245 | displayclass &operator<< (outconvertclass &theoutc, displayclass &display);
|
---|
246 | displayclass &operator<< (displayclass &display, const text_t &t);
|
---|
247 |
|
---|
248 | #endif
|
---|
249 |
|
---|