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

Last change on this file since 7733 was 7397, checked in by mdewsnip, 20 years ago

(Human Info) Use displayclass::defaultpackage instead of "Global".

  • 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 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 (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 (text_t package, const text_t &macroname,
189 text_t params, const text_t &macrovalue);
190
191 // loads a default macro file (if it isn't already loaded)
192 // returns 0 if didn't need to load the file (it was already loaded)
193 // 1 if was (re)loaded
194 // -1 an error occurred while trying to load the file
195 int loaddefaultmacros (text_t thisfilename);
196
197 // loads a collection specific macro file
198 // returns 0 if didn't need to load the file (it was already loaded)
199 // 1 if was (re)loaded
200 // -1 an error occurred while trying to load the file
201 int loadcollectionmacros (text_t thisfilename);
202
203 // unloads all default macros
204 void unloaddefaultmacros ();
205
206 // unloads all collection specific macros
207 void unloadcollectionmacros ();
208
209 // prepares to create a page: deletes all macros set with
210 // 'setmacro', checks all the default macro files to see
211 // if any need reloading, and sets the page parameters.
212 void openpage (const text_t &thispageparams,
213 const text_t &thisprecedence);
214
215 // changes the parameters for the current page.
216 void setpageparams (text_t thispageparams,
217 text_t thisprecedence);
218
219 // overrides (or sets) a macro for the current page.
220 // returns 0 if there was no error,
221 // -1 if it redefined a macro
222 // -4 if no macroname was supplied
223 int setmacro (const text_t &macroname,
224 text_t package,
225 const text_t &macrovalue);
226
227 void expandstring (const text_t &inputtext, text_t &outputtext);
228 void expandstring (text_t package, const text_t &inputtext,
229 text_t &outputtext, int recursiondepth = 0);
230
231 // these functions are not ment to be used directly, they are used to permit
232 // concise stream output like:
233 // cout << text_t2ascii << display << "_amacro_" << "_anothermacro_";
234 void setconvertclass (outconvertclass *theoutc) {outc = theoutc;}
235 outconvertclass *getconvertclass () {return outc;}
236
237 // say where any error logging goes, this can be NULL
238 // if no error logging is desired - returns previous value
239 ostream *displayclass::setlogout (ostream *thelogout);
240
241 // debug stuff
242 void printdefaultmacros ();
243 void printallparams ();
244
245protected:
246 // special variables to permit trickly output
247 outconvertclass *outc;
248
249 // general variables
250 text_tset allparams; // possible parameter combinations
251 parammacros_t *defaultmacros;
252 parammacros_t *collectionmacros;
253 fileinfomap *defaultfiles;
254
255 // variables to do with a page expansion
256 text_t params;
257 text_t precedence;
258 paramspeclist *orderparamlist; // ordered by specificness
259 currentmacros_t *currentmacros; // only macros set by setmacro
260
261 // logging variable
262 ostream *logout;
263
264 // reloads any default macro files which have changed
265 // returns 0 no errors occurred
266 // -1 an error occurred while trying to load one of the files
267 int checkdefaultmacrofiles ();
268
269 int loadparammacros (parammacros_t* macrotable, text_t thisfilename);
270
271 int setparammacro (parammacros_t* macrotable,
272 text_t package, const text_t &macroname,
273 text_t params, const text_t &filename,
274 const text_t &macrovalue);
275
276 int setdefaultmacro (text_t package, const text_t &macroname,
277 text_t params, const text_t &filename,
278 const text_t &macrovalue);
279
280 int setcollectionmacro (text_t package, const text_t &macroname,
281 text_t params, const text_t &filename,
282 const text_t &macrovalue);
283
284 // evaluates a boolean expression
285 bool boolexpr (text_t package, const text_t &expr, int recursiondepth);
286
287 mvalue* macro_find (parammacros_t* macrotable, text_t packagename,
288 const text_t &macroname);
289
290 // (recursively) expand out a macro
291 // returns true if the macro was found
292 // false if the macro was not found
293 bool macro (const text_t &macroname, text_t macropackage,
294 const text_t &macroparam, text_t &outputtext,
295 int recursiondepth);
296};
297
298
299displayclass &operator<< (outconvertclass &theoutc, displayclass &display);
300displayclass &operator<< (displayclass &display, const text_t &t);
301
302#endif
Note: See TracBrowser for help on using the repository browser.