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

Last change on this file since 6021 was 6021, checked in by sjboddie, 20 years ago

Added an unloaddefaultmacros() function to displayclass to allow us to
unload all the macros before reloading them again from the macro files.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 7.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 defaultmacros_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
100inline bool operator==(const fileinfoelement &x, const fileinfoelement &y) {
101 return (x.otherinfo==y.otherinfo);
102}
103
104inline bool operator<(const fileinfoelement &x, const fileinfoelement &y) {
105 return (x.otherinfo<y.otherinfo);
106}
107
108typedef map<text_t, fileinfoelement, lttext_t> fileinfomap;
109
110
111typedef map<text_t, text_t, lttext_t> paramhashtype;
112
113
114struct paramspec
115{
116 double spec;
117 text_t param;
118};
119
120inline bool operator==(const paramspec& x, const paramspec& y)
121{
122 return ((x.spec == y.spec) && (x.param == y.param));
123}
124
125inline bool operator!=(const paramspec& x, const paramspec& y)
126{
127 return ((x.spec != y.spec) || (x.param != y.param));
128}
129
130// note: paramspec is sorted by reverse spec and then param
131
132inline bool operator<(const paramspec& x, const paramspec& y)
133{
134 return ((x.spec > y.spec) ||
135 ((x.spec == y.spec) && (x.param < y.param)));
136}
137
138inline bool operator>(const paramspec& x, const paramspec& y)
139{
140 return ((x.spec < y.spec) ||
141 ((x.spec == y.spec) && (x.param > y.param)));
142}
143
144typedef vector<paramspec> paramspeclist;
145
146
147// NOTE: when macros are used within text they should have
148// underscores '_' on both sides, however, in calls to
149// setdefaultmacro and setmacro they should not have the
150// underscores. So you might have
151//
152// setmacro ("aname", "query", " some text ");
153// expandstring ("_aname_", expandedtext);
154//
155// because the input to expandstring is a block of text, not
156// a macroname
157
158class displayclass
159{
160public:
161 displayclass ();
162 ~displayclass ();
163
164 // isdefaultmacro sees if there is an entry in the list of
165 // default macros with the given package and macro name
166 // returns 0 if no macros in the package or in the global package
167 // were found
168 // 1 if no macros in the package were found but a macro
169 // in the global package was found
170 // 2 if a macro in the given package was found
171 int isdefaultmacro (text_t package, const text_t &macroname);
172
173 // setdefaultmacro adds an entry to the list of default macros
174 // returns 0 if there was no error,
175 // -1 if it redefined a macro
176 // -2 if it hid a Global macro
177 // -3 if it redefined a macro and hid a Global macro
178 // -4 if no macroname was supplied
179 int setdefaultmacro (text_t package, const text_t &macroname,
180 text_t params, const text_t &macrovalue);
181
182 // loads a default macro file (if it isn't already loaded)
183 // returns 0 if didn't need to load the file (it was already loaded)
184 // 1 if was (re)loaded
185 // -1 an error occurred while trying to load the file
186 int loaddefaultmacros (text_t thisfilename);
187
188 // unloads all default macros
189 void unloaddefaultmacros ();
190
191 // prepares to create a page: deletes all macros set with
192 // 'setmacro', checks all the default macro files to see
193 // if any need reloading, and sets the page parameters.
194 void openpage (const text_t &thispageparams,
195 const text_t &thisprecedence);
196
197 // changes the parameters for the current page.
198 void setpageparams (text_t thispageparams,
199 text_t thisprecedence);
200
201 // overrides (or sets) a macro for the current page.
202 // returns 0 if there was no error,
203 // -1 if it redefined a macro
204 // -4 if no macroname was supplied
205 int setmacro (const text_t &macroname,
206 text_t package,
207 const text_t &macrovalue);
208
209 void expandstring (const text_t &inputtext, text_t &outputtext);
210 void expandstring (text_t package, const text_t &inputtext,
211 text_t &outputtext, int recursiondepth = 0);
212
213 // these functions are not ment to be used directly, they are used to permit
214 // concise stream output like:
215 // cout << text_t2ascii << display << "_amacro_" << "_anothermacro_";
216 void setconvertclass (outconvertclass *theoutc) {outc = theoutc;}
217 outconvertclass *getconvertclass () {return outc;}
218
219 // say where any error logging goes, this can be NULL
220 // if no error logging is desired - returns previous value
221 ostream *displayclass::setlogout (ostream *thelogout);
222
223 // debug stuff
224 void printdefaultmacros ();
225 void printallparams ();
226
227protected:
228 // special variables to permit trickly output
229 outconvertclass *outc;
230
231 // general variables
232 text_tset allparams; // possible parameter combinations
233 defaultmacros_t *defaultmacros;
234 fileinfomap *defaultfiles;
235
236 // variables to do with a page expansion
237 text_t params;
238 text_t precedence;
239 paramspeclist *orderparamlist; // ordered by specificness
240 currentmacros_t *currentmacros; // only macros set by setmacro
241
242 // logging variable
243 ostream *logout;
244
245 // reloads any default macro files which have changed
246 // returns 0 no errors occurred
247 // -1 an error occurred while trying to load one of the files
248 int checkdefaultmacrofiles ();
249
250 int setdefaultmacro (text_t package, const text_t &macroname,
251 text_t params, const text_t &filename,
252 const text_t &macrovalue);
253
254 // evaluates a boolean expression
255 bool boolexpr (text_t package, const text_t &expr, int recursiondepth);
256
257 // (recursively) expand out a macro
258 // returns true if the macro was found
259 // false if the macro was not found
260 bool macro (const text_t &macroname, text_t macropackage,
261 const text_t &macroparam, text_t &outputtext,
262 int recursiondepth);
263};
264
265
266displayclass &operator<< (outconvertclass &theoutc, displayclass &display);
267displayclass &operator<< (displayclass &display, const text_t &t);
268
269#endif
Note: See TracBrowser for help on using the repository browser.