source: main/tags/2.53/gsdl/lib/display.h@ 32727

Last change on this file since 32727 was 8727, checked in by kjdon, 19 years ago

added some changes made by Emanuel Dejanu (Simple Words)

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 10.1 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 static text_t defaultpackage;
169
170 displayclass ();
171 ~displayclass ();
172
173 // isdefaultmacro sees if there is an entry in the list of
174 // default macros with the given package and macro name
175 // returns 0 if no macros in the package or in the global package
176 // were found
177 // 1 if no macros in the package were found but a macro
178 // in the global package was found
179 // 2 if a macro in the given package was found
180 int isdefaultmacro (const text_t &package, const text_t &macroname);
181
182 // setdefaultmacro adds an entry to the list of default macros
183 // returns 0 if there was no error,
184 // -1 if it redefined a macro
185 // -2 if it hid a Global macro
186 // -3 if it redefined a macro and hid a Global macro
187 // -4 if no macroname was supplied
188 int setdefaultmacro (const text_t &package,
189 const text_t &macroname,
190 const text_t &params,
191 const text_t &macrovalue);
192
193 // loads a default macro file (if it isn't already loaded)
194 // returns 0 if didn't need to load the file (it was already loaded)
195 // 1 if was (re)loaded
196 // -1 an error occurred while trying to load the file
197 int loaddefaultmacros (const text_t &thisfilename);
198
199
200 // overrides (or sets) a macro for the current page.
201 // returns 0 if there was no error,
202 // -1 if it redefined a macro
203 // -4 if no macroname was supplied
204 int setcollectionmacro (const text_t &package,
205 const text_t &macroname,
206 text_t params,
207 const text_t &macrovalue);
208
209 // loads a collection specific macro file
210 // returns 0 if didn't need to load the file (it was already loaded)
211 // 1 if was (re)loaded
212 // -1 an error occurred while trying to load the file
213 int loadcollectionmacros (const text_t &thisfilename);
214
215 // unloads all default macros
216 void unloaddefaultmacros ();
217
218 // unloads all collection specific macros
219 void unloadcollectionmacros ();
220
221 // prepares to create a page: deletes all macros set with
222 // 'setmacro', checks all the default macro files to see
223 // if any need reloading, and sets the page parameters.
224 void openpage (const text_t &thispageparams,
225 const text_t &thisprecedence);
226
227 // changes the parameters for the current page.
228 void setpageparams (text_t thispageparams,
229 text_t thisprecedence);
230
231 // overrides (or sets) a macro for the current page.
232 // returns 0 if there was no error,
233 // -1 if it redefined a macro
234 // -4 if no macroname was supplied
235 int setmacro (const text_t &macroname,
236 const text_t &package,
237 const text_t &macrovalue);
238
239// havemacro sees if there is an entry in the list of macros or
240// default macros with the given package and macro name
241// returns 0 if no macros in the package or in the global package
242// were found
243// 1 if no macros in the package were found but a macro
244// in the global package was found
245// 2 if a macro in the given package was found
246// 4 if no macros in the package were found but a macro
247// in the global package was found in default macros
248// 8 if a macro in the given package was found in default
249 int havemacro(const text_t &package, const text_t &macroname);
250
251 int setmacroif (const text_t &macro_to_set_and_test,
252 const text_t &macro_to_set_if_macro_not_set,
253 const text_t &macroname,
254 const text_t& package);
255
256 inline void expandstring (const text_t &inputtext, text_t &outputtext) { expandstring(defaultpackage, inputtext, outputtext); }
257
258 void expandstring (const text_t &package, const text_t &inputtext,
259 text_t &outputtext, int recursiondepth = 0);
260
261 // these functions are not ment to be used directly, they are used to permit
262 // concise stream output like:
263 // cout << text_t2ascii << display << "_amacro_" << "_anothermacro_";
264 void setconvertclass (outconvertclass *theoutc) {outc = theoutc;}
265 outconvertclass *getconvertclass () {return outc;}
266
267 // say where any error logging goes, this can be NULL
268 // if no error logging is desired - returns previous value
269 ostream *displayclass::setlogout (ostream *thelogout);
270
271 // debug stuff
272 void printdefaultmacros ();
273 void printallparams ();
274
275protected:
276 // special variables to permit trickly output
277 outconvertclass *outc;
278
279 // general variables
280 text_tset allparams; // possible parameter combinations
281 parammacros_t *defaultmacros;
282 parammacros_t *collectionmacros;
283 fileinfomap *defaultfiles;
284
285 // variables to do with a page expansion
286 text_t params;
287 text_t precedence;
288 paramspeclist *orderparamlist; // ordered by specificness
289 currentmacros_t *currentmacros; // only macros set by setmacro
290
291 // logging variable
292 ostream *logout;
293
294 // reloads any default macro files which have changed
295 // returns 0 no errors occurred
296 // -1 an error occurred while trying to load one of the files
297 int checkdefaultmacrofiles ();
298
299 int loadparammacros (parammacros_t* macrotable, const text_t &thisfilename);
300
301 int setparammacro (parammacros_t* macrotable,
302 const text_t &package,
303 const text_t &macroname,
304 text_t params, const text_t &filename,
305 const text_t &macrovalue);
306
307 int setdefaultmacro (const text_t &package,
308 const text_t &macroname,
309 text_t params, const text_t &filename,
310 const text_t &macrovalue);
311
312 int setcollectionmacro(const text_t &package,
313 const text_t &macroname,
314 text_t params,
315 const text_t &filename,
316 const text_t &macrovalue);
317
318 // evaluates a boolean expression
319 bool boolexpr (const text_t &package, const text_t &expr,
320 int recursiondepth);
321
322 mvalue* macro_find (parammacros_t* macrotable,
323 const text_t &packagename,
324 const text_t &macroname);
325
326 // (recursively) expand out a macro
327 // returns true if the macro was found
328 // false if the macro was not found
329 bool macro (const text_t &macroname,
330 const text_t &macropackage,
331 const text_t &macroparam, text_t &outputtext,
332 int recursiondepth);
333};
334
335
336displayclass &operator<< (outconvertclass &theoutc, displayclass &display);
337displayclass &operator<< (displayclass &display, const text_t &t);
338
339#endif
Note: See TracBrowser for help on using the repository browser.