source: trunk/gsdl/lib/display.h@ 7396

Last change on this file since 7396 was 7386, checked in by davidb, 20 years ago

Modifications to macro data-structures to support collection specific
macros that override existing macros and allow for the introduction
of new collection specific ones.

Main change has been to rename 'defaultmacro_t' to 'parammacro_t' since
there are now two member fields in displayclass that use this: one
for default macros, and another for collection macros.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 8.8 KB
Line 
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 *********************************************************************/
25
26
27#ifndef DISPLAY_H
28#define DISPLAY_H
29
30#include "gsdlconf.h"
31#include "text_t.h"
32
33
34#if defined(GSDL_USE_OBJECTSPACE)
35# include <ospace\std\vector>
36# include <ospace\std\list>
37# include <ospace\std\set>
38# include <ospace\std\map>
39# include <ospace\std\algorithm>
40#elif defined(GSDL_USE_STL_H)
41# include <vector.h>
42# include <list.h>
43# include <set.h>
44# include <map.h>
45# if defined(GSDL_USE_ALGO_H)
46# include <algo.h>
47# else
48# include <algorithm.h>
49# endif
50#else
51# include <vector>
52# include <list>
53# include <set>
54# include <map>
55# include <algorithm>
56#endif
57
58#if defined(GSDL_USE_OBJECTSPACE)
59# include <ospace\std\iostream>
60# include <ospace\std\fstream>
61#elif defined(GSDL_USE_IOS_H)
62# include <iostream.h>
63# include <fstream.h>
64
65#define unistream ifstream
66#else
67# include <iostream>
68# include <fstream>
69
70typedef std::basic_ifstream<unsigned char> unistream;
71
72#endif
73
74
75// use the standard namespace
76#if defined(GSDL_USE_OBJECTSPACE)
77using namespace ospace::std;
78#elif !defined(GSDL_NAMESPACE_BROKEN)
79using namespace std;
80#endif
81
82
83
84// MAXRECURSIONDEPTH is a cutoff to catch
85// cyclic macros (a includes b and b includes a)
86#define MAXRECURSIONDEPTH 30
87
88// class prototypes
89class parammacros_t;
90class currentmacros_t;
91
92// a few supporting types
93// fileelement isn't finished yet, I have to find out
94// more about getting information about files
95struct fileinfoelement
96{
97 int otherinfo;
98};
99
100// macro value structure
101struct mvalue
102{
103 text_t filename;
104 text_t value;
105};
106
107inline bool operator==(const fileinfoelement &x, const fileinfoelement &y) {
108 return (x.otherinfo==y.otherinfo);
109}
110
111inline bool operator<(const fileinfoelement &x, const fileinfoelement &y) {
112 return (x.otherinfo<y.otherinfo);
113}
114
115typedef map<text_t, fileinfoelement, lttext_t> fileinfomap;
116
117
118typedef map<text_t, text_t, lttext_t> paramhashtype;
119
120
121struct paramspec
122{
123 double spec;
124 text_t param;
125};
126
127inline bool operator==(const paramspec& x, const paramspec& y)
128{
129 return ((x.spec == y.spec) && (x.param == y.param));
130}
131
132inline bool operator!=(const paramspec& x, const paramspec& y)
133{
134 return ((x.spec != y.spec) || (x.param != y.param));
135}
136
137// note: paramspec is sorted by reverse spec and then param
138
139inline bool operator<(const paramspec& x, const paramspec& y)
140{
141 return ((x.spec > y.spec) ||
142 ((x.spec == y.spec) && (x.param < y.param)));
143}
144
145inline bool operator>(const paramspec& x, const paramspec& y)
146{
147 return ((x.spec < y.spec) ||
148 ((x.spec == y.spec) && (x.param > y.param)));
149}
150
151typedef vector<paramspec> paramspeclist;
152
153
154// NOTE: when macros are used within text they should have
155// underscores '_' on both sides, however, in calls to
156// setdefaultmacro and setmacro they should not have the
157// underscores. So you might have
158//
159// setmacro ("aname", "query", " some text ");
160// expandstring ("_aname_", expandedtext);
161//
162// because the input to expandstring is a block of text, not
163// a macroname
164
165class displayclass
166{
167public:
168 displayclass ();
169 ~displayclass ();
170
171 // isdefaultmacro sees if there is an entry in the list of
172 // default macros with the given package and macro name
173 // returns 0 if no macros in the package or in the global package
174 // were found
175 // 1 if no macros in the package were found but a macro
176 // in the global package was found
177 // 2 if a macro in the given package was found
178 int isdefaultmacro (text_t package, const text_t &macroname);
179
180 // setdefaultmacro adds an entry to the list of default macros
181 // returns 0 if there was no error,
182 // -1 if it redefined a macro
183 // -2 if it hid a Global macro
184 // -3 if it redefined a macro and hid a Global macro
185 // -4 if no macroname was supplied
186 int setdefaultmacro (text_t package, const text_t &macroname,
187 text_t params, const text_t &macrovalue);
188
189 // loads a default macro file (if it isn't already loaded)
190 // returns 0 if didn't need to load the file (it was already loaded)
191 // 1 if was (re)loaded
192 // -1 an error occurred while trying to load the file
193 int loaddefaultmacros (text_t thisfilename);
194
195 // loads a collection specific macro file
196 // returns 0 if didn't need to load the file (it was already loaded)
197 // 1 if was (re)loaded
198 // -1 an error occurred while trying to load the file
199 int loadcollectionmacros (text_t thisfilename);
200
201 // unloads all default macros
202 void unloaddefaultmacros ();
203
204 // unloads all collection specific macros
205 void unloadcollectionmacros ();
206
207 // prepares to create a page: deletes all macros set with
208 // 'setmacro', checks all the default macro files to see
209 // if any need reloading, and sets the page parameters.
210 void openpage (const text_t &thispageparams,
211 const text_t &thisprecedence);
212
213 // changes the parameters for the current page.
214 void setpageparams (text_t thispageparams,
215 text_t thisprecedence);
216
217 // overrides (or sets) a macro for the current page.
218 // returns 0 if there was no error,
219 // -1 if it redefined a macro
220 // -4 if no macroname was supplied
221 int setmacro (const text_t &macroname,
222 text_t package,
223 const text_t &macrovalue);
224
225 void expandstring (const text_t &inputtext, text_t &outputtext);
226 void expandstring (text_t package, const text_t &inputtext,
227 text_t &outputtext, int recursiondepth = 0);
228
229 // these functions are not ment to be used directly, they are used to permit
230 // concise stream output like:
231 // cout << text_t2ascii << display << "_amacro_" << "_anothermacro_";
232 void setconvertclass (outconvertclass *theoutc) {outc = theoutc;}
233 outconvertclass *getconvertclass () {return outc;}
234
235 // say where any error logging goes, this can be NULL
236 // if no error logging is desired - returns previous value
237 ostream *displayclass::setlogout (ostream *thelogout);
238
239 // debug stuff
240 void printdefaultmacros ();
241 void printallparams ();
242
243protected:
244 // special variables to permit trickly output
245 outconvertclass *outc;
246
247 // general variables
248 text_tset allparams; // possible parameter combinations
249 parammacros_t *defaultmacros;
250 parammacros_t *collectionmacros;
251 fileinfomap *defaultfiles;
252
253 // variables to do with a page expansion
254 text_t params;
255 text_t precedence;
256 paramspeclist *orderparamlist; // ordered by specificness
257 currentmacros_t *currentmacros; // only macros set by setmacro
258
259 // logging variable
260 ostream *logout;
261
262 // reloads any default macro files which have changed
263 // returns 0 no errors occurred
264 // -1 an error occurred while trying to load one of the files
265 int checkdefaultmacrofiles ();
266
267 int loadparammacros (parammacros_t* macrotable, text_t thisfilename);
268
269 int setparammacro (parammacros_t* macrotable,
270 text_t package, const text_t &macroname,
271 text_t params, const text_t &filename,
272 const text_t &macrovalue);
273
274 int setdefaultmacro (text_t package, const text_t &macroname,
275 text_t params, const text_t &filename,
276 const text_t &macrovalue);
277
278 int setcollectionmacro (text_t package, const text_t &macroname,
279 text_t params, const text_t &filename,
280 const text_t &macrovalue);
281
282 // evaluates a boolean expression
283 bool boolexpr (text_t package, const text_t &expr, int recursiondepth);
284
285 mvalue* macro_find (parammacros_t* macrotable, text_t packagename,
286 const text_t &macroname);
287
288 // (recursively) expand out a macro
289 // returns true if the macro was found
290 // false if the macro was not found
291 bool macro (const text_t &macroname, text_t macropackage,
292 const text_t &macroparam, text_t &outputtext,
293 int recursiondepth);
294};
295
296
297displayclass &operator<< (outconvertclass &theoutc, displayclass &display);
298displayclass &operator<< (displayclass &display, const text_t &t);
299
300#endif
Note: See TracBrowser for help on using the repository browser.