[1076] | 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>
|
---|
[1217] | 64 |
|
---|
| 65 | #define unistream ifstream
|
---|
[1076] | 66 | #else
|
---|
| 67 | # include <iostream>
|
---|
| 68 | # include <fstream>
|
---|
[1217] | 69 |
|
---|
| 70 | typedef std::basic_ifstream<unsigned char> unistream;
|
---|
| 71 |
|
---|
[1076] | 72 | #endif
|
---|
| 73 |
|
---|
| 74 |
|
---|
| 75 | // use the standard namespace
|
---|
| 76 | #if defined(GSDL_USE_OBJECTSPACE)
|
---|
| 77 | using namespace ospace::std;
|
---|
| 78 | #elif !defined(GSDL_NAMESPACE_BROKEN)
|
---|
| 79 | using 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
|
---|
[7386] | 89 | class parammacros_t;
|
---|
[1076] | 90 | class 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
|
---|
| 95 | struct fileinfoelement
|
---|
| 96 | {
|
---|
| 97 | int otherinfo;
|
---|
| 98 | };
|
---|
| 99 |
|
---|
[7386] | 100 | // macro value structure
|
---|
| 101 | struct mvalue
|
---|
| 102 | {
|
---|
| 103 | text_t filename;
|
---|
| 104 | text_t value;
|
---|
| 105 | };
|
---|
| 106 |
|
---|
[1076] | 107 | inline bool operator==(const fileinfoelement &x, const fileinfoelement &y) {
|
---|
| 108 | return (x.otherinfo==y.otherinfo);
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | inline bool operator<(const fileinfoelement &x, const fileinfoelement &y) {
|
---|
| 112 | return (x.otherinfo<y.otherinfo);
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | typedef map<text_t, fileinfoelement, lttext_t> fileinfomap;
|
---|
| 116 |
|
---|
| 117 |
|
---|
| 118 | typedef map<text_t, text_t, lttext_t> paramhashtype;
|
---|
[9932] | 119 | void splitparams (const text_t ¶mstring, paramhashtype ¶mhash);
|
---|
[1076] | 120 |
|
---|
| 121 |
|
---|
| 122 | struct paramspec
|
---|
| 123 | {
|
---|
| 124 | double spec;
|
---|
| 125 | text_t param;
|
---|
| 126 | };
|
---|
| 127 |
|
---|
| 128 | inline bool operator==(const paramspec& x, const paramspec& y)
|
---|
| 129 | {
|
---|
| 130 | return ((x.spec == y.spec) && (x.param == y.param));
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | inline 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 |
|
---|
| 140 | inline 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 |
|
---|
| 146 | inline 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 |
|
---|
| 152 | typedef vector<paramspec> paramspeclist;
|
---|
| 153 |
|
---|
| 154 |
|
---|
[9932] | 155 |
|
---|
[1076] | 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 |
|
---|
| 167 | class displayclass
|
---|
| 168 | {
|
---|
| 169 | public:
|
---|
[7397] | 170 | static text_t defaultpackage;
|
---|
| 171 |
|
---|
[1076] | 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
|
---|
[8727] | 182 | int isdefaultmacro (const text_t &package, const text_t ¯oname);
|
---|
[1076] | 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
|
---|
[8727] | 190 | int setdefaultmacro (const text_t &package,
|
---|
| 191 | const text_t ¯oname,
|
---|
| 192 | const text_t ¶ms,
|
---|
| 193 | const text_t ¯ovalue);
|
---|
[1076] | 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
|
---|
[8727] | 199 | int loaddefaultmacros (const text_t &thisfilename);
|
---|
[6021] | 200 |
|
---|
[8485] | 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
|
---|
[8727] | 206 | int setcollectionmacro (const text_t &package,
|
---|
| 207 | const text_t ¯oname,
|
---|
| 208 | text_t params,
|
---|
| 209 | const text_t ¯ovalue);
|
---|
[8485] | 210 |
|
---|
[7386] | 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
|
---|
[8727] | 215 | int loadcollectionmacros (const text_t &thisfilename);
|
---|
[7386] | 216 |
|
---|
[6021] | 217 | // unloads all default macros
|
---|
| 218 | void unloaddefaultmacros ();
|
---|
[7386] | 219 |
|
---|
| 220 | // unloads all collection specific macros
|
---|
| 221 | void unloadcollectionmacros ();
|
---|
[1076] | 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 ¯oname,
|
---|
[8727] | 238 | const text_t &package,
|
---|
[1076] | 239 | const text_t ¯ovalue);
|
---|
| 240 |
|
---|
[8727] | 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 ¯oname);
|
---|
| 252 |
|
---|
| 253 | int setmacroif (const text_t ¯o_to_set_and_test,
|
---|
| 254 | const text_t ¯o_to_set_if_macro_not_set,
|
---|
| 255 | const text_t ¯oname,
|
---|
| 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,
|
---|
[1076] | 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
|
---|
[6021] | 271 | ostream *displayclass::setlogout (ostream *thelogout);
|
---|
[1076] | 272 |
|
---|
| 273 | // debug stuff
|
---|
| 274 | void printdefaultmacros ();
|
---|
| 275 | void printallparams ();
|
---|
| 276 |
|
---|
| 277 | protected:
|
---|
| 278 | // special variables to permit trickly output
|
---|
| 279 | outconvertclass *outc;
|
---|
| 280 |
|
---|
| 281 | // general variables
|
---|
[7386] | 282 | text_tset allparams; // possible parameter combinations
|
---|
| 283 | parammacros_t *defaultmacros;
|
---|
| 284 | parammacros_t *collectionmacros;
|
---|
| 285 | fileinfomap *defaultfiles;
|
---|
[1076] | 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 |
|
---|
[8727] | 301 | int loadparammacros (parammacros_t* macrotable, const text_t &thisfilename);
|
---|
[7386] | 302 |
|
---|
| 303 | int setparammacro (parammacros_t* macrotable,
|
---|
[8727] | 304 | const text_t &package,
|
---|
| 305 | const text_t ¯oname,
|
---|
[7386] | 306 | text_t params, const text_t &filename,
|
---|
| 307 | const text_t ¯ovalue);
|
---|
| 308 |
|
---|
[8727] | 309 | int setdefaultmacro (const text_t &package,
|
---|
| 310 | const text_t ¯oname,
|
---|
[1076] | 311 | text_t params, const text_t &filename,
|
---|
| 312 | const text_t ¯ovalue);
|
---|
| 313 |
|
---|
[8727] | 314 | int setcollectionmacro(const text_t &package,
|
---|
| 315 | const text_t ¯oname,
|
---|
| 316 | text_t params,
|
---|
| 317 | const text_t &filename,
|
---|
| 318 | const text_t ¯ovalue);
|
---|
| 319 |
|
---|
[1076] | 320 | // evaluates a boolean expression
|
---|
[8727] | 321 | bool boolexpr (const text_t &package, const text_t &expr,
|
---|
| 322 | int recursiondepth);
|
---|
[1076] | 323 |
|
---|
[8727] | 324 | mvalue* macro_find (parammacros_t* macrotable,
|
---|
| 325 | const text_t &packagename,
|
---|
[7386] | 326 | const text_t ¯oname);
|
---|
| 327 |
|
---|
[1076] | 328 | // (recursively) expand out a macro
|
---|
| 329 | // returns true if the macro was found
|
---|
| 330 | // false if the macro was not found
|
---|
[8727] | 331 | bool macro (const text_t ¯oname,
|
---|
| 332 | const text_t ¯opackage,
|
---|
[1076] | 333 | const text_t ¯oparam, text_t &outputtext,
|
---|
| 334 | int recursiondepth);
|
---|
| 335 | };
|
---|
| 336 |
|
---|
| 337 |
|
---|
| 338 | displayclass &operator<< (outconvertclass &theoutc, displayclass &display);
|
---|
| 339 | displayclass &operator<< (displayclass &display, const text_t &t);
|
---|
| 340 |
|
---|
| 341 | #endif
|
---|