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