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

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

made the splitparams a public method so we can use it elsewhere

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