[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
|
---|
| 66 |
|
---|
[1076] | 67 | #else
|
---|
| 68 | # include <iostream>
|
---|
| 69 | # include <fstream>
|
---|
[1217] | 70 |
|
---|
| 71 | typedef std::basic_ifstream<unsigned char> unistream;
|
---|
| 72 |
|
---|
[1076] | 73 | #endif
|
---|
| 74 |
|
---|
| 75 |
|
---|
| 76 | // use the standard namespace
|
---|
| 77 | #if defined(GSDL_USE_OBJECTSPACE)
|
---|
| 78 | using namespace ospace::std;
|
---|
| 79 | #elif !defined(GSDL_NAMESPACE_BROKEN)
|
---|
| 80 | using namespace std;
|
---|
| 81 | #endif
|
---|
| 82 |
|
---|
| 83 |
|
---|
| 84 |
|
---|
| 85 | // MAXRECURSIONDEPTH is a cutoff to catch
|
---|
| 86 | // cyclic macros (a includes b and b includes a)
|
---|
| 87 | #define MAXRECURSIONDEPTH 30
|
---|
| 88 |
|
---|
| 89 | // class prototypes
|
---|
| 90 | class defaultmacros_t;
|
---|
| 91 | class currentmacros_t;
|
---|
| 92 |
|
---|
| 93 | // a few supporting types
|
---|
| 94 | // fileelement isn't finished yet, I have to find out
|
---|
| 95 | // more about getting information about files
|
---|
| 96 | struct fileinfoelement
|
---|
| 97 | {
|
---|
| 98 | int otherinfo;
|
---|
| 99 | };
|
---|
| 100 |
|
---|
| 101 | inline bool operator==(const fileinfoelement &x, const fileinfoelement &y) {
|
---|
| 102 | return (x.otherinfo==y.otherinfo);
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | inline bool operator<(const fileinfoelement &x, const fileinfoelement &y) {
|
---|
| 106 | return (x.otherinfo<y.otherinfo);
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | typedef map<text_t, fileinfoelement, lttext_t> fileinfomap;
|
---|
| 110 |
|
---|
| 111 |
|
---|
| 112 | typedef map<text_t, text_t, lttext_t> paramhashtype;
|
---|
| 113 |
|
---|
| 114 |
|
---|
| 115 | struct paramspec
|
---|
| 116 | {
|
---|
| 117 | double spec;
|
---|
| 118 | text_t param;
|
---|
| 119 | };
|
---|
| 120 |
|
---|
| 121 | inline bool operator==(const paramspec& x, const paramspec& y)
|
---|
| 122 | {
|
---|
| 123 | return ((x.spec == y.spec) && (x.param == y.param));
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | inline bool operator!=(const paramspec& x, const paramspec& y)
|
---|
| 127 | {
|
---|
| 128 | return ((x.spec != y.spec) || (x.param != y.param));
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | // note: paramspec is sorted by reverse spec and then param
|
---|
| 132 |
|
---|
| 133 | inline bool operator<(const paramspec& x, const paramspec& y)
|
---|
| 134 | {
|
---|
| 135 | return ((x.spec > y.spec) ||
|
---|
| 136 | ((x.spec == y.spec) && (x.param < y.param)));
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | inline 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 |
|
---|
| 145 | typedef vector<paramspec> paramspeclist;
|
---|
| 146 |
|
---|
| 147 |
|
---|
| 148 | // NOTE: when macros are used within text they should have
|
---|
| 149 | // underscores '_' on both sides, however, in calls to
|
---|
| 150 | // setdefaultmacro and setmacro they should not have the
|
---|
| 151 | // underscores. So you might have
|
---|
| 152 | //
|
---|
| 153 | // setmacro ("aname", "query", " some text ");
|
---|
| 154 | // expandstring ("_aname_", expandedtext);
|
---|
| 155 | //
|
---|
| 156 | // because the input to expandstring is a block of text, not
|
---|
| 157 | // a macroname
|
---|
| 158 |
|
---|
| 159 | class displayclass
|
---|
| 160 | {
|
---|
| 161 | public:
|
---|
| 162 | displayclass ();
|
---|
| 163 | ~displayclass ();
|
---|
| 164 |
|
---|
| 165 | // isdefaultmacro sees if there is an entry in the list of
|
---|
| 166 | // default macros with the given package and macro name
|
---|
| 167 | // returns 0 if no macros in the package or in the global package
|
---|
| 168 | // were found
|
---|
| 169 | // 1 if no macros in the package were found but a macro
|
---|
| 170 | // in the global package was found
|
---|
| 171 | // 2 if a macro in the given package was found
|
---|
| 172 | int isdefaultmacro (text_t package, const text_t ¯oname);
|
---|
| 173 |
|
---|
| 174 | // setdefaultmacro adds an entry to the list of default macros
|
---|
| 175 | // returns 0 if there was no error,
|
---|
| 176 | // -1 if it redefined a macro
|
---|
| 177 | // -2 if it hid a Global macro
|
---|
| 178 | // -3 if it redefined a macro and hid a Global macro
|
---|
| 179 | // -4 if no macroname was supplied
|
---|
| 180 | int setdefaultmacro (text_t package, const text_t ¯oname,
|
---|
| 181 | text_t params, const text_t ¯ovalue);
|
---|
| 182 |
|
---|
| 183 | // loads a default macro file (if it isn't already loaded)
|
---|
| 184 | // returns 0 if didn't need to load the file (it was already loaded)
|
---|
| 185 | // 1 if was (re)loaded
|
---|
| 186 | // -1 an error occurred while trying to load the file
|
---|
| 187 | int loaddefaultmacros (text_t thisfilename);
|
---|
| 188 |
|
---|
| 189 | // prepares to create a page: deletes all macros set with
|
---|
| 190 | // 'setmacro', checks all the default macro files to see
|
---|
| 191 | // if any need reloading, and sets the page parameters.
|
---|
| 192 | void openpage (const text_t &thispageparams,
|
---|
| 193 | const text_t &thisprecedence);
|
---|
| 194 |
|
---|
| 195 | // changes the parameters for the current page.
|
---|
| 196 | void setpageparams (text_t thispageparams,
|
---|
| 197 | text_t thisprecedence);
|
---|
| 198 |
|
---|
| 199 | // overrides (or sets) a macro for the current page.
|
---|
| 200 | // returns 0 if there was no error,
|
---|
| 201 | // -1 if it redefined a macro
|
---|
| 202 | // -4 if no macroname was supplied
|
---|
| 203 | int setmacro (const text_t ¯oname,
|
---|
| 204 | text_t package,
|
---|
| 205 | const text_t ¯ovalue);
|
---|
| 206 |
|
---|
| 207 | void expandstring (const text_t &inputtext, text_t &outputtext);
|
---|
| 208 | void expandstring (text_t package, const text_t &inputtext,
|
---|
| 209 | text_t &outputtext, int recursiondepth = 0);
|
---|
| 210 |
|
---|
| 211 | // these functions are not ment to be used directly, they are used to permit
|
---|
| 212 | // concise stream output like:
|
---|
| 213 | // cout << text_t2ascii << display << "_amacro_" << "_anothermacro_";
|
---|
| 214 | void setconvertclass (outconvertclass *theoutc) {outc = theoutc;}
|
---|
| 215 | outconvertclass *getconvertclass () {return outc;}
|
---|
| 216 |
|
---|
| 217 | // say where any error logging goes, this can be NULL
|
---|
| 218 | // if no error logging is desired - returns previous value
|
---|
| 219 | ostream *setlogout (ostream *thelogout);
|
---|
| 220 |
|
---|
| 221 | // debug stuff
|
---|
| 222 | void printdefaultmacros ();
|
---|
| 223 | void printallparams ();
|
---|
| 224 |
|
---|
| 225 | protected:
|
---|
| 226 | // special variables to permit trickly output
|
---|
| 227 | outconvertclass *outc;
|
---|
| 228 |
|
---|
| 229 | // general variables
|
---|
| 230 | text_tset allparams; // possible parameter combinations
|
---|
| 231 | defaultmacros_t *defaultmacros;
|
---|
| 232 | fileinfomap *defaultfiles;
|
---|
| 233 |
|
---|
| 234 | // variables to do with a page expansion
|
---|
| 235 | text_t params;
|
---|
| 236 | text_t precedence;
|
---|
| 237 | paramspeclist *orderparamlist; // ordered by specificness
|
---|
| 238 | currentmacros_t *currentmacros; // only macros set by setmacro
|
---|
| 239 |
|
---|
| 240 | // logging variable
|
---|
| 241 | ostream *logout;
|
---|
| 242 |
|
---|
| 243 | // reloads any default macro files which have changed
|
---|
| 244 | // returns 0 no errors occurred
|
---|
| 245 | // -1 an error occurred while trying to load one of the files
|
---|
| 246 | int checkdefaultmacrofiles ();
|
---|
| 247 |
|
---|
| 248 | int setdefaultmacro (text_t package, const text_t ¯oname,
|
---|
| 249 | text_t params, const text_t &filename,
|
---|
| 250 | const text_t ¯ovalue);
|
---|
| 251 |
|
---|
| 252 | // evaluates a boolean expression
|
---|
| 253 | bool boolexpr (text_t package, const text_t &expr, int recursiondepth);
|
---|
| 254 |
|
---|
| 255 | // (recursively) expand out a macro
|
---|
| 256 | // returns true if the macro was found
|
---|
| 257 | // false if the macro was not found
|
---|
| 258 | bool macro (const text_t ¯oname, text_t macropackage,
|
---|
| 259 | const text_t ¯oparam, text_t &outputtext,
|
---|
| 260 | int recursiondepth);
|
---|
| 261 | };
|
---|
| 262 |
|
---|
| 263 |
|
---|
| 264 | displayclass &operator<< (outconvertclass &theoutc, displayclass &display);
|
---|
| 265 | displayclass &operator<< (displayclass &display, const text_t &t);
|
---|
| 266 |
|
---|
| 267 | #endif
|
---|