root/trunk/gsdl/lib/display.cpp @ 11105

Revision 11105, 52.0 KB (checked in by kjdon, 14 years ago)

fixed a bug where nested if statements would crap out if there was no space between the closing )

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
Line 
1/**********************************************************************
2 *
3 * display.cpp -- 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#include "display.h"
27#include "gsdlunicode.h"
28#include "unitool.h"
29#include <assert.h>
30
31// include to get NULL
32#include <stdlib.h>
33
34text_t displayclass::defaultpackage = "Global";
35
36/////////////////////////////////////
37// misc classes
38/////////////////////////////////////
39
40
41// precedencetype defines a 'precedence value' for each parameter
42typedef map<text_t, double, lttext_t> precedencetype;
43
44
45
46inline bool operator==(const mvalue &x, const mvalue &y) {
47  return (x.filename==y.filename && x.value==y.value);
48}
49
50inline bool operator<(const mvalue &x, const mvalue &y) {
51  return (x.filename<y.filename ||
52      (x.filename==y.filename && x.filename<y.filename));
53}
54
55
56
57/////////////////////////////////////
58// stuff for parammacros_t
59/////////////////////////////////////
60 
61typedef map<text_t, mvalue, lttext_t> defparammap;
62typedef map<text_t, defparammap, lttext_t> defmacromap;
63typedef map<text_t, defmacromap, lttext_t> defpackagemap;
64
65// all methods in parammacros_t assume the parameters
66// and packages in a standard form and not empty
67class parammacros_t
68{
69protected:
70  defpackagemap macros;
71
72public:
73  typedef defpackagemap::const_iterator const_package_iterator;
74  typedef defpackagemap::size_type package_size_type;
75
76  // setmacro returns 0 if there was no error,
77  //            -1 if it redefined a macro
78  //            -2 if it hid a Global macro
79  //            -3 if it redefined a macro and hid a Global macro
80  //            -4 if either a package, macroname, or params were not supplied
81  int setmacro(const text_t &package,
82      const text_t &macroname,
83      const text_t &params,
84      const text_t &filename,
85      const text_t &macrovalue);
86
87  void delmacro(const text_t &package,
88          const text_t &macroname,
89          const text_t &params);
90
91  void clear () {macros.erase(macros.begin(), macros.end());}
92
93  // top level stuff
94  const_package_iterator package_begin () const {return macros.begin();}
95  const_package_iterator package_end () const {return macros.end();}
96  bool package_empty () const {return macros.empty();}
97  package_size_type package_size() const {return macros.size();}
98
99  // methods to find things
100  defmacromap *package_find (const text_t &packagename);
101  defparammap *macro_find (const text_t &packagename,
102               const text_t &macroname);
103  mvalue *parameter_find (const text_t &packagename,
104              const text_t &macroname,
105              const text_t &parametername);
106};
107
108
109// setmacro returns 0 if there was no error,
110//            -1 if it redefined a macro
111//            -2 if it hid a Global macro
112//            -3 if it redefined a macro and hid a Global macro
113//            -4 if either a package, macroname, or params were not supplied
114int parammacros_t::setmacro(const text_t &package,
115             const text_t &macroname,
116             const text_t &params,
117             const text_t &filename,
118             const text_t &macrovalue)
119{
120  paramhashtype paramhash;
121
122  defmacromap *macromapptr;
123  defparammap *parammapptr;
124  mvalue *mvalueptr;
125
126  int warning = 0;
127
128  // check to see if we would be hiding a Global macro with the
129  // same name. Note: it doesn't matter what parameters are used
130  // a Global macro will still be hid by a non-Global one with
131  // the same name.
132  if ((package != displayclass::defaultpackage) &&
133      (macro_find (displayclass::defaultpackage, macroname) != NULL))
134    {
135      warning -= 2; // found the macroname
136    }
137
138  // define this macro
139  macromapptr = &(macros[package]); // -- package
140  parammapptr = &((*macromapptr)[macroname]); // -- macro name
141  // -- parameters
142  if ((*parammapptr).find(params) != (*parammapptr).end()) {
143    warning -= 1; // found the parameters
144  }
145  mvalueptr = &((*parammapptr)[params]);
146
147  // -- value
148  (*mvalueptr).filename = filename;
149  (*mvalueptr).value = macrovalue;
150
151  return warning;
152}
153
154void parammacros_t::delmacro(const text_t &package,
155                 const text_t &macroname,
156                 const text_t &params)
157{
158  // make sure everything was supplied
159  if (package.empty() || macroname.empty() || params.empty()) return;
160
161  // find the package and macroname
162  defparammap *parammapptr = macro_find(package, macroname);
163  if (parammapptr == NULL) return;
164
165  // find the parameters
166  defparammap::iterator parammapit = parammapptr->find(params);
167  if (parammapit == parammapptr->end()) return;
168
169  // finally delete this element
170  parammapptr->erase(parammapit);
171}
172
173defmacromap *parammacros_t::package_find (const text_t &packagename)
174{
175  if (packagename.empty()) return NULL;
176
177  defpackagemap::iterator it = macros.find(packagename);
178  if (it == macros.end()) return NULL;
179
180  return &((*it).second);
181}
182
183defparammap *parammacros_t::macro_find (const text_t &packagename,
184                    const text_t &macroname)
185{
186  defmacromap *macromapptr = package_find(packagename);
187  if (macromapptr == NULL || macroname.empty()) return NULL;
188 
189  defmacromap::iterator it = (*macromapptr).find(macroname);
190  if (it == (*macromapptr).end()) return NULL;
191
192  return &((*it).second);
193}
194
195mvalue *parammacros_t::parameter_find (const text_t &packagename,
196                     const text_t &macroname,
197                     const text_t &parametername)
198{
199  defparammap *parammapptr = macro_find(packagename, macroname);
200  if (parammapptr == NULL || parametername.empty()) return NULL;
201
202  defparammap::iterator it = (*parammapptr).find(parametername);
203  if (it == (*parammapptr).end()) return NULL;
204
205  return &((*it).second);
206}
207
208
209
210/////////////////////////////////////
211// stuff for currentmacros_t
212/////////////////////////////////////
213
214typedef map<text_t, mvalue, lttext_t> curmacromap;
215typedef map<text_t, curmacromap, lttext_t> curpackagemap;
216
217// all methods in currentmacros_t assume the parameters
218// and packages in a standard form and not empty
219class currentmacros_t
220{
221protected:
222  curpackagemap macros;
223
224public:
225  typedef curpackagemap::const_iterator const_package_iterator;
226  typedef curpackagemap::size_type package_size_type;
227
228  // setmacro returns 0 if there was no error,
229  //            -1 if it redefined a macro
230  //            -4 if either a package, or macroname were not supplied
231  int setmacro(const text_t &package,
232      const text_t &macroname,
233      const text_t &filename,
234      const text_t &macrovalue);
235
236  void delmacro(const text_t &package,
237        const text_t &macroname);
238
239  void clear () {macros.erase(macros.begin(), macros.end());}
240
241  // top level stuff
242  const_package_iterator package_begin () const {return macros.begin();}
243  const_package_iterator package_end () const {return macros.end();}
244  bool package_empty () const {return macros.empty();}
245  package_size_type package_size() const {return macros.size();}
246
247  // methods to find things
248  curmacromap *package_find (const text_t &packagename);
249  mvalue *macro_find (const text_t &packagename,
250              const text_t &macroname);
251};
252
253
254// setmacro returns 0 if there was no error,
255//            -1 if it redefined a macro
256//            -4 if either a package, or macroname were not supplied
257int currentmacros_t::setmacro(const text_t &package,
258             const text_t &macroname,
259             const text_t &filename,
260             const text_t &macrovalue)
261{
262  int warning = 0;
263
264  // make sure everything was supplied
265  if (package.empty() || macroname.empty()) return -4;
266
267  // define this macro
268  curmacromap *macromapptr = &(macros[package]);
269  if ((*macromapptr).find(macroname) != (*macromapptr).end())
270    {
271      warning -= 1; // found the macroname
272    }
273  mvalue *mvalueptr = &((*macromapptr)[macroname]);
274
275  // -- value
276  (*mvalueptr).filename = filename;
277  (*mvalueptr).value = macrovalue;
278
279  return warning;
280}
281
282void currentmacros_t::delmacro(const text_t &package,
283                 const text_t &macroname)
284{
285  // make sure everything was supplied
286  if (package.empty() || macroname.empty()) return;
287
288  // find the package
289  curmacromap *macromapptr = package_find(package);
290  if (macromapptr == NULL) return;
291
292  // find the macroname
293  curmacromap::iterator macromapit = macromapptr->find(macroname);
294  if (macromapit == macromapptr->end()) return;
295
296  // finally delete this element
297  macromapptr->erase(macromapit);
298}
299
300curmacromap *currentmacros_t::package_find (const text_t &packagename)
301{
302  if (packagename.empty()) return NULL;
303
304  curpackagemap::iterator it = macros.find(packagename);
305  if (it == macros.end()) return NULL;
306
307  return &((*it).second);
308}
309
310mvalue *currentmacros_t::macro_find (const text_t &packagename,
311                     const text_t &macroname)
312{
313  curmacromap *macromapptr = package_find(packagename);
314  if (macromapptr == NULL || macroname.empty()) return NULL;
315
316  curmacromap::iterator it = (*macromapptr).find(macroname);
317  if (it == (*macromapptr).end()) return NULL;
318
319  return &((*it).second);
320}
321
322
323
324
325/////////////////////////////////////
326// support functions
327/////////////////////////////////////
328
329// this calculation of precedence will make one occurance of a high
330// precedence item override many occurances of lower precedence items
331void calcprecedence (const text_t &precedstr, precedencetype &precedhash)
332{
333  text_t param;
334  double multiple = 5.0;
335  double nextprec = multiple;
336
337  text_t::const_iterator here, end;
338
339  // set precedhash to an empty hash
340  precedhash.erase(precedhash.begin(), precedhash.end());
341
342  // iterate through paramstring ignoring space and extracting
343  // out key value pairs.
344  here = precedstr.begin();
345  end = precedstr.end();
346  while (here != end)
347    {
348      // get the next parameter
349      param.clear(); // set param to an empty string
350     
351      while (here != end)
352    {
353      if (*here == ',')
354        {
355          // found the end of the parameter
356          ++here;
357          break;
358        }
359      else if (*here == ' ')
360        {
361          // do nothing (ignore spaces)
362        }
363      else
364        {
365          // character in parameter
366          param.push_back (*here); // copy this character
367        }
368     
369      ++here;
370    }
371
372      // if a parameter was found insert its value
373      if (!param.empty())
374    {
375      precedhash[param] = nextprec;
376      nextprec *= multiple;
377    }
378    }
379}
380
381
382// decides how specific any given parameter is to the page parameters. Returns
383// negative if any options are different or else the sum of the precedence for
384// the options which match.
385double getspecificness(const paramhashtype &pageparams,
386               const paramhashtype &theseparams,
387               const precedencetype &precedhash)
388{
389  double specificness = 0.0;
390  paramhashtype::const_iterator here, pagevalueit;
391  precedencetype::const_iterator precedit;
392  text_t thesekey, thesevalue, constignore("ignore");
393 
394  here = theseparams.begin();
395  while (here != theseparams.end())
396    {
397      thesekey = (*here).first;
398      thesevalue = (*here).second;
399     
400      if (thesekey == constignore)
401    {
402      // do nothing, this is a placeholder
403    }
404      else
405    {
406      pagevalueit = pageparams.find(thesekey);
407      if ((pagevalueit != pageparams.end()) &&
408          ((*pagevalueit).second == thesevalue))
409        {
410          // this parameter fits, add its precedence value
411          precedit = precedhash.find(thesekey);
412          if (precedit == precedhash.end())
413        specificness += 1.0; // no precedence defined
414          else
415          specificness += (*precedit).second;
416        }
417      else
418        {
419          return -1.0; // did not match
420        }
421    }
422
423      ++here;
424    }
425 
426  return specificness;
427}
428
429
430void splitparams (const text_t &paramstring, paramhashtype &paramhash)
431{
432  text_t key;
433  text_t value;
434
435  text_t::const_iterator here, end;
436
437  // set paramhash to an empty hash
438  paramhash.erase(paramhash.begin(), paramhash.end());
439
440  // iterate through paramstring ignoring space and extracting
441  // out key value pairs.
442  here = paramstring.begin();
443  end  = paramstring.end();
444  while (here != end)
445    {
446      // reset key and value
447      key.clear();
448      value.clear();
449
450      // get key
451      while (here != end)
452    {
453      if (*here == ',')
454        {
455          // have a key with no value
456          break;
457        }
458      else if (*here == '=')
459        {
460          // found the end of the key
461          ++here;
462          break;
463        }
464      else if (*here == ' ')
465        {
466          // do nothing (ignore spaces)
467        }
468      else
469        {
470          // character in key
471          key.push_back (*here); // copy this character
472        }
473      ++here;
474    }
475
476      // get value
477      while (here != end)
478    {
479      if (*here == '=')
480        {
481          // multiple values for one key, ignore previous
482          // values
483          value.clear();
484        }
485      else if (*here == ',')
486        {
487          // found the end of the value
488          ++here;
489          break;
490        }
491      else if (*here == ' ')
492        {
493          // do nothing (ignore spaces)
494        }
495      else
496        {
497          // character in value
498          value.push_back (*here); // copy this character
499        }
500      ++here;
501    }
502   
503      // if a key was found insert the key/value pair in
504      // the map
505      if (!key.empty())
506    {
507      paramhash[key] = value;
508    }
509    }
510}
511
512
513void joinparams (const paramhashtype &paramhash, text_t &paramstring)
514{
515  //set paramstring to an empty string
516  paramstring.clear();
517
518  //iterate through the paramhash
519  paramhashtype::const_iterator thepair;
520  int firstparam = 1;
521
522  for (thepair=paramhash.begin();thepair!=paramhash.end(); ++thepair)
523    {
524      if (!firstparam) paramstring.push_back(',');
525      firstparam = 0;
526     
527      // insert pairs of "key=value"
528      text_t thevalue;
529      paramstring.append((*thepair).first);
530      paramstring.push_back('=');
531      paramstring.append((*thepair).second);
532    }
533}
534
535
536
537/////////////////////////////////////
538// parsing support functions
539/////////////////////////////////////
540
541inline int my_isalpha (unsigned short c)
542{
543  return ((c >= 'A' && c <= 'Z') ||
544      (c >= 'a' && c <= 'z'));
545}
546
547inline int my_isalphanum (unsigned short c)
548{
549  return ((c >= 'A' && c <= 'Z') ||
550      (c >= 'a' && c <= 'z') || (c >='0' && c <= '9'));
551}
552 
553
554
555inline unsigned short my_ttnextchar (text_t::const_iterator &here,
556                     text_t::const_iterator &end)
557{
558  if (here != end)
559    {
560      ++here;
561      if (here != end) return (*here);
562    }
563  return '\0';
564}
565
566
567
568/////////////////////////////////////
569// methods for display
570/////////////////////////////////////
571
572// public methods for display
573
574displayclass::displayclass ()
575{
576  defaultmacros    = new parammacros_t;
577  collectionmacros = new parammacros_t;
578  defaultfiles     = new fileinfomap;
579 
580  orderparamlist = new paramspeclist;
581  currentmacros = new currentmacros_t;
582
583  outc = NULL;
584
585  logout = NULL;
586
587  /* this is a bit of a hack, but if the displayclass has never seen this
588     parameter before when it does openpage() then macros with this set will
589     always be ignored. And we set collection-specific macros after doing
590     openpage() - jrm */
591  allparams.insert("l=en");
592}
593
594
595displayclass::~displayclass ()
596{
597  delete defaultmacros;
598  delete collectionmacros;
599  delete defaultfiles;
600 
601  delete orderparamlist;
602  delete currentmacros;
603}
604 
605// setdefaultmacro adds an entry to the list of default macros
606// returns 0 if there was no error,
607//        -1 if it redefined a macro
608//        -2 if it hid a Global macro
609//        -3 if it redefined a macro and hid a Global macro
610//        -4 if no macroname was supplied
611int displayclass::setdefaultmacro (const text_t &package, const text_t &macroname,
612                   const text_t &mparams, const text_t &macrovalue)
613{
614  return setdefaultmacro (package, macroname, mparams, "memory", macrovalue);
615}
616
617
618
619// as we are using one character lookahead the
620// value of line might be off by one.
621// the input file must be in the utf-8 or unicode format
622// initially for each file isunicode should be set to 0 and
623// bigendian should be set to 1
624// 0 will be returned when the end of the file has been found
625unsigned short my_uni_get (/*unistream*/ifstream &fin, int &line,
626               int &isunicode, int &bigendian) {
627  unsigned short c = 0;
628 
629  if (isunicode) {
630    // unicode text
631    // get the next two characters
632    unsigned char c1 = 0, c2 = 0;
633   
634    if (!fin.eof()) c1=fin.get();
635    if (!fin.eof()) c2=fin.get();
636    else c1 = 0;
637
638    // if they indicate the order get the next character
639    // otherwise just get these characters
640    if (c1 == 0xff && c2 == 0xfe) {
641      bigendian = 0;
642      c = my_uni_get (fin, line, isunicode, bigendian);
643    } else if (c1 == 0xfe && c2 == 0xff) {
644      bigendian = 1;
645      c = my_uni_get (fin, line, isunicode, bigendian);
646    } else c = (bigendian) ? (c1*256+c2) : (c2*256+c1);
647   
648  } else {
649    // utf-8 text
650    // how many characters we get depends on what we find
651    unsigned char c1 = 0, c2 = 0, c3 = 0;
652    while (!fin.eof()) {
653      c1=fin.get();
654      if (c1 == 0xfe || c1 == 0xff) {
655    // switch to unicode
656    isunicode = 1;
657    if (!fin.eof()) c2=fin.get();
658
659    if (c1 == 0xff && c2 == 0xfe) bigendian = 0;
660    else bigendian = 1;
661
662    c = my_uni_get (fin, line, isunicode, bigendian);
663    break;
664
665      } else if (c1 <= 0x7f) {
666    // one byte character
667    c = c1;
668    break;
669
670      } else if (c1 >= 0xc0 && c1 <= 0xdf) {
671    // two byte character
672    if (!fin.eof()) c2=fin.get();
673    c = ((c1 & 0x1f) << 6) + (c2 & 0x3f);
674    break;
675
676      } else if (c1 >= 0xe0 && c1 <= 0xef) {
677    // three byte character
678    if (!fin.eof()) c2=fin.get();
679    if (!fin.eof()) c3=fin.get();
680    c = ((c1 & 0xf) << 12) + ((c2 & 0x3f) << 6) + (c3 & 0x3f);
681    break;
682      }
683
684      // if we get here there was an error in the file, we should
685      // be able to recover from it however, maybe the file is in
686      // another encoding
687    }
688  }
689
690  if (c == '\n') ++line;
691  return c;
692}
693
694
695
696// loads a macro file into the specified macrotable datastructure
697// returns 0 if didn't need to load the file (it was already loaded)
698//         1 if was (re)loaded
699//        -1 an error occurred while trying to load the file
700int displayclass::loadparammacros (parammacros_t* macrotable, const text_t &thisfilename) {
701  // convert the filename to a C string
702  char *filenamestr = thisfilename.getcstr();
703  outconvertclass text_t2ascii;
704
705  // see if we need to open this file -- this isn't implemented yet
706 
707  // open the file
708  //  unistream fin (filenamestr);
709  ifstream fin (filenamestr);
710
711  if (fin.fail()) return -1; // read failed
712
713  text_t package = defaultpackage;
714  int line = 1;
715  int isunicode = 0, bigendian = 1;
716
717  // pre-fetch the next character
718  unsigned short c = my_uni_get(fin, line, isunicode, bigendian);
719
720  text_t macropackage, macroname, macroparameters, macrovalue;
721  int err; // for keeping track of whether an error occurred somewhere
722
723  while (!fin.eof()) {
724    // expect: white space, comment, "package", or macroname
725    if (is_unicode_space(c)) {
726      // found some white-space
727      c = my_uni_get(fin, line, isunicode, bigendian);
728
729    } else if (c == '#') {
730      // found the start of a comment
731      // skip all characters up to the end of the line
732      c = my_uni_get(fin, line, isunicode, bigendian); // skip the '#'
733      while (!fin.eof ()) {
734    if (c == '\n') break;
735    c = my_uni_get(fin, line, isunicode, bigendian);
736      }
737
738    } else if (c == 'p') {
739      // found the start of 'package' (hopefully)
740      // get everything up to the next space
741      text_t tmp;
742      while (!fin.eof() && my_isalpha(c)) {
743    tmp.push_back(c);
744    c = my_uni_get(fin, line, isunicode, bigendian);
745      }
746      // see if we have a package name
747      if (tmp == "package") {
748    // skip all white space
749    while (!fin.eof() && is_unicode_space(c))
750      c = my_uni_get(fin, line, isunicode, bigendian);
751   
752    // get the package name
753    tmp.clear(); // init tmp
754    while (!fin.eof() && my_isalphanum(c)) {
755      tmp.push_back(c);
756      c = my_uni_get(fin, line, isunicode, bigendian);
757    }
758    package = tmp;
759    if (package.empty()) package = defaultpackage;
760
761      } else {
762    // error
763    if (logout != NULL) {
764      (*logout) << text_t2ascii << text_t("Expected 'package' on line ") + line + text_t(" of ") + thisfilename + "\n";
765    }
766      }
767     
768    } else if (c == '_') {
769      // found the start of a macro (hopefully)
770      c = my_uni_get(fin, line, isunicode, bigendian); // skip the _
771
772      // init variables
773      err = 0;
774      macropackage = package;
775      macroname.clear(); // init macroname
776      macroparameters.clear(); // init macroname
777      macrovalue.clear(); // init macroname
778     
779      // get the macro name
780      while ((!fin.eof()) && (!is_unicode_space(c)) &&
781         (c != '\\') && (c != '_') &&(c != ':') &&
782         (macroname.size() < 80)) {
783    macroname.push_back(c);
784    c = my_uni_get(fin, line, isunicode, bigendian);
785      }
786     
787      if (c == ':') {
788    // we actually had the macro package
789    c = my_uni_get(fin, line, isunicode, bigendian); // skip :
790    macropackage = macroname;
791    macroname.clear ();
792   
793    // get the macro name (honest!)
794    while ((!fin.eof()) && (!is_unicode_space(c)) &&
795           (c != '\\') && (c != '_') &&(c != ':') &&
796           (macroname.size() < 80)) {
797      macroname.push_back(c);
798      c = my_uni_get(fin, line, isunicode, bigendian);
799    }
800      }
801     
802      if (!err && c == '_') {
803    c = my_uni_get(fin, line, isunicode, bigendian); // skip the _
804   
805    // skip all white space
806    while (!fin.eof() && is_unicode_space(c)) c = my_uni_get(fin, line, isunicode, bigendian);
807      } else if (!err) err = 1;
808
809      // get the macro parameters (optional)
810      if (!err && c == '[') {
811    c = my_uni_get(fin, line, isunicode, bigendian); // skip the [
812    while ((!fin.eof()) && (c != '\n') && (c != '\\') && (c != ']')) {
813      macroparameters.push_back(c);
814      c = my_uni_get(fin, line, isunicode, bigendian);
815    }
816   
817    if (c == ']') {
818      c = my_uni_get(fin, line, isunicode, bigendian); // skip the ]
819     
820      // skip all white space
821      while (!fin.eof() && is_unicode_space(c)) c = my_uni_get(fin, line, isunicode, bigendian);
822    }
823    else if (!err) err = 2;
824      }
825
826      // get the macro value
827      if (!err && c == '{') {
828    c = my_uni_get(fin, line, isunicode, bigendian); // skip the {
829    while ((!fin.eof()) && (c != '}')) {
830      if (c == '\\') {
831        macrovalue.push_back(c); // keep the '\'
832        c = my_uni_get(fin, line, isunicode, bigendian); // store the *next* value regardless
833        if (!fin.eof()) macrovalue.push_back(c);
834        c = my_uni_get(fin, line, isunicode, bigendian);
835      }
836      macrovalue.push_back(c);
837      c = my_uni_get(fin, line, isunicode, bigendian);
838    }
839   
840    if (c == '}') {
841      c = my_uni_get(fin, line, isunicode, bigendian); // skip the }
842     
843      // define the macro
844      err = setparammacro (macrotable, macropackage, macroname, macroparameters,
845                   thisfilename, macrovalue);
846      if ((err == -1 || err == -3) && logout != NULL) {
847        (*logout) << text_t2ascii << ("Warning: redefinition of _" + package + ":" + macroname + "_[" + macroparameters + "] on line " + line +  " of " + thisfilename + "\n");
848       
849      } else if (err == -2 && logout != NULL) {
850        // I don't think we really want this warning since it's a very
851        // common occurance - Stefan.
852
853        //      (*logout) << text_t2ascii << "Warning: _" <<
854        //        package << ":" << macroname << "_[" << macroparameters <<
855        //        "] on line ";
856        //      (*logout) << line;
857        //      (*logout) << text_t2ascii << " of " <<
858        //        thisfilename << " hides a Global macro with the same name\n";
859
860      } else if (err == -4 && logout != NULL) {
861        (*logout) << text_t2ascii << text_t("Error: macro name expected on line ") +  line + text_t(" of ") + thisfilename + "\n";
862      }       
863     
864      err = 0; // for the test below
865    }
866    else if (!err) err = 3;
867      }
868      else if (!err) err = 4;
869     
870      if (err) {
871    // found an error, skip to the end of the line
872    if (logout != NULL) {
873      text_t message = "Error: ";
874      if (err == 1) message += "'_'";
875      else if (err == 2) message += "']'";
876      else if (err == 3) message += "'}'";
877      else if (err == 4) message += "'{'";
878      message += " expected on line ";
879      message += line;
880      message += " of " + thisfilename + "\n";
881      (*logout) << text_t2ascii << message;
882     
883//    (*logout) << text_t2ascii << "Error: ";
884//    if (err == 1) (*logout) << text_t2ascii << "'_'";
885//    else if (err == 2) (*logout) << text_t2ascii << "']'";
886//    else if (err == 3) (*logout) << text_t2ascii << "'}'";
887//    else if (err == 4) (*logout) << text_t2ascii << "'{'";
888//    (*logout) << text_t2ascii << " expected on line ";
889//    (*logout) << line ;
890//    (*logout) << text_t2ascii << " of " << thisfilename << "\n";
891    }
892    while (!fin.eof ()) {
893      if (c == '\n') break;
894      c = my_uni_get(fin, line, isunicode, bigendian);
895    }
896      }
897     
898    } else {
899      // found an error, skip to the end of the line
900      if (logout != NULL) {
901    (*logout) << text_t2ascii << text_t("Error: Unexpected input on line ") + line + text_t(" of ") + thisfilename + "\n";
902      }
903      while (!fin.eof ()) {
904    if (c == '\n') break;
905    c = my_uni_get(fin, line, isunicode, bigendian);
906      }
907     
908    }
909  }
910 
911  fin.close ();
912 
913  // free up memory
914  delete []filenamestr;
915  return 0;
916}
917
918
919// loads a default macro file (if it isn't already loaded)
920// returns 0 if didn't need to load the file (it was already loaded)
921//         1 if was (re)loaded
922//        -1 an error occurred while trying to load the file
923int displayclass::loaddefaultmacros (const text_t &thisfilename)
924{
925  return loadparammacros(defaultmacros,thisfilename);
926}
927
928// loads a collection specific macro file
929// returns 0 if didn't need to load the file (it was already loaded)
930//         1 if was (re)loaded
931//        -1 an error occurred while trying to load the file
932int displayclass::loadcollectionmacros (const text_t &thisfilename)
933{
934  return loadparammacros(collectionmacros,thisfilename);
935}
936
937
938void displayclass::unloaddefaultmacros () {
939  defaultmacros->clear();
940}
941
942void displayclass::unloadcollectionmacros () {
943  collectionmacros->clear();
944}
945
946// prepares to create a page.
947void displayclass::openpage (const text_t &thispageparams,
948            const text_t &thisprecedence)
949{
950  // init variables
951  currentmacros->clear();
952
953  // reload any default macro files which have changed.
954  if (checkdefaultmacrofiles() < 0)
955    {
956      // print error message
957    }
958
959  setpageparams (thispageparams, thisprecedence);
960}
961
962
963// changes the parameters for the current page.
964void displayclass::setpageparams (text_t thispageparams,
965                 text_t thisprecedence)
966{
967  paramhashtype thissplitparams, splittheseparams;
968  precedencetype precedhash;
969  text_tset::iterator text_tsetit;
970  paramspec tmpps;
971
972  precedence = thisprecedence;
973  calcprecedence(thisprecedence, precedhash);
974
975  splitparams(thispageparams, thissplitparams);
976  joinparams(thissplitparams, thispageparams);
977  params = thispageparams;
978
979  // get a list of parameters with their specificness
980  orderparamlist->erase(orderparamlist->begin(), orderparamlist->end());
981  for (text_tsetit=allparams.begin();
982       text_tsetit!=allparams.end();
983       ++text_tsetit)
984    {
985      tmpps.param = (*text_tsetit);
986      splitparams(tmpps.param, splittheseparams);
987      tmpps.spec = getspecificness(thissplitparams, splittheseparams, precedhash);
988      if (tmpps.spec >= 0)
989    {
990      orderparamlist->push_back (tmpps);
991    }
992    }
993
994  // sort the list
995  sort(orderparamlist->begin(), orderparamlist->end());
996
997#if 0 // debugging
998  outconvertclass text_t2ascii;
999
1000  paramspeclist::iterator pshere = orderparamlist->begin();
1001  paramspeclist::iterator psend = orderparamlist->end();
1002  while (pshere != psend)
1003    {
1004      cerr << text_t2ascii << "param=" << (*pshere).param;
1005      cerr << " spec=" << (int)((*pshere).spec) << "\n";
1006      ++pshere;
1007    }
1008#endif
1009}
1010
1011
1012// overrides (or sets) a macro for the current page.
1013// returns 0 if there was no error,
1014//        -1 if it redefined a macro
1015//        -4 if no macroname was supplied
1016int displayclass::setmacro (const text_t &macroname,
1017                const text_t &package,
1018                const text_t &macrovalue)
1019{
1020  // make sure a macroname was supplied
1021  if (macroname.empty()) return -4;
1022
1023  // make package "Global" if it doesn't point to anything yet
1024  if (package.empty())
1025    return currentmacros->setmacro(defaultpackage, macroname, "memory", macrovalue);
1026 
1027  // set the macro
1028  return currentmacros->setmacro(package, macroname, "memory", macrovalue);
1029}
1030
1031int displayclass::setmacroif (const text_t &macro_to_set_and_test,
1032                  const text_t &macro_to_set_if_macro_not_set,
1033                  const text_t &macroname, 
1034                  const text_t &package) {
1035 
1036  if (macroname.empty()) return -4;
1037  if (macro_to_set_and_test.empty()) return -40;
1038 
1039  text_t temp;
1040  expandstring(defaultpackage, macro_to_set_and_test, temp);
1041 
1042  if (package.empty()) {
1043    if (temp == macro_to_set_and_test) return currentmacros->setmacro(defaultpackage, macroname, "memory", macro_to_set_if_macro_not_set);
1044    return currentmacros->setmacro(defaultpackage, macroname, "memory", macro_to_set_and_test);
1045  } else {
1046    if (temp == macro_to_set_and_test) return currentmacros->setmacro(package, macroname, "memory", macro_to_set_if_macro_not_set);
1047    return currentmacros->setmacro(package, macroname, "memory", macro_to_set_and_test);
1048   
1049  }
1050}
1051
1052void displayclass::expandstring (const text_t &package, const text_t &inputtext,
1053                 text_t &outputtext, int recursiondepth)
1054{
1055  text_t macroname, macropackage, macroargs;
1056  text_t::const_iterator tthere = inputtext.begin();
1057  text_t::const_iterator ttend = inputtext.end();
1058  unsigned short c = '\0';
1059  int openbrackets=0;
1060
1061  if (package.empty()) expandstring(defaultpackage, inputtext, outputtext, recursiondepth);
1062
1063  outputtext.clear();
1064  // use one-character lookahead
1065  if (tthere != ttend) c = (*tthere);
1066  while (tthere != ttend)
1067    {
1068      if (c == '\\')
1069    {
1070      // found an escape character
1071      c = my_ttnextchar (tthere, ttend); // skip the escape character
1072      if (tthere != ttend)
1073        {
1074          if (c == 'n') outputtext.push_back('\n');
1075          else if (c == 't') outputtext.push_back('\t');
1076          else outputtext.push_back(c);
1077        }
1078      c = my_ttnextchar (tthere, ttend);
1079     
1080    }
1081      else if (c == '_')
1082    {
1083      // might have found the start of a macro
1084     
1085      // embedded macros (macros within the names
1086      // of other macros) are no longer supported -- Rodger.
1087     
1088      // save the current state (in case we need to back out)
1089      text_t::const_iterator savedtthere = tthere;
1090      unsigned short savedc = c;
1091     
1092      macroname.clear();
1093      macropackage = package;
1094      macroargs.clear();
1095     
1096      c = my_ttnextchar (tthere, ttend); // skip the '_'
1097     
1098      // get the macroname
1099      while (tthere != ttend && (!is_unicode_space(c)) &&
1100         (c != '\\') && (c != '_') &&(c != ':') &&
1101         (macroname.size() < 80))
1102        {
1103          macroname.push_back((unsigned char)c);
1104          c = my_ttnextchar (tthere, ttend);
1105        }
1106     
1107      if (c == ':')
1108        {
1109          // we actually had the macro package
1110          c = my_ttnextchar (tthere, ttend);
1111          macropackage = macroname;
1112          macroname.clear ();
1113   
1114          // get the macro name (honest!)
1115          while ((tthere != ttend) && (!is_unicode_space(c)) &&
1116             (c != '\\') && (c != '_') &&(c != ':') &&
1117             (macroname.size() < 80))
1118        {
1119          macroname.push_back((unsigned char)c);
1120          c = my_ttnextchar (tthere, ttend);
1121        }
1122        }
1123     
1124      if ((tthere != ttend) && (c == '_') &&
1125          (macroname.size() > 0))
1126        {
1127          // found a macro!!!! (maybe)
1128          c = my_ttnextchar (tthere, ttend); // skip the '_'
1129   
1130          // get the parameters (if there are any)
1131          if ((tthere != ttend) && (c == '('))
1132        {
1133          c = my_ttnextchar (tthere, ttend); // skip '('
1134          ++openbrackets;
1135          // have to be careful of quotes
1136          unsigned short quote = '\0';
1137          while (tthere != ttend)
1138            {
1139              if (c == '\\')
1140            {
1141              // have an escape character, save escape
1142              // character and next character as well
1143              macroargs.push_back (c);
1144              c = my_ttnextchar (tthere, ttend);
1145         
1146            }
1147              else if (quote == '\0')
1148            {
1149              // not in a quote at the moment
1150              if (c == '(')
1151                {
1152                  ++openbrackets;
1153                }
1154              else if (c == ')')
1155                {
1156                  --openbrackets;
1157                  if (openbrackets==0) {
1158                // found end of the arguments
1159                c = my_ttnextchar (tthere, ttend); // skip ')'
1160                break;
1161                  } else {
1162                // nested brackets
1163                  }
1164                }
1165              else if (c == '\'' || c == '\"')
1166                {
1167                  // found a quote
1168                  quote = c;
1169                }
1170         
1171            }
1172              else
1173            {
1174              // we are in a quote
1175              if (c == quote) quote = '\0';
1176            }
1177       
1178              // save this character
1179              if (tthere != ttend) macroargs.push_back (c);
1180              c = my_ttnextchar (tthere, ttend);
1181            } // while (tthere != ttend)
1182        }
1183   
1184          //
1185          // we now have the macropackage, macroname, and macroargs
1186          //
1187   
1188          // try to expand out this macro
1189          text_t expandedmacro;
1190          if (macro (macroname, macropackage.empty()?defaultpackage:macropackage, macroargs, expandedmacro, recursiondepth))
1191        {
1192          // append the expanded macro
1193          outputtext += expandedmacro;
1194        }
1195          else
1196        {
1197          // wasn't a macro
1198          tthere = savedtthere;
1199          c = savedc;
1200          outputtext.push_back(c); // the '_'
1201          c = my_ttnextchar (tthere, ttend);
1202        }
1203   
1204
1205        }
1206      else
1207        {
1208          // wasn't a macro
1209          tthere = savedtthere;
1210          c = savedc;
1211          outputtext.push_back(c); // the '_'
1212          c = my_ttnextchar (tthere, ttend);
1213        }
1214
1215    }
1216      else
1217    {
1218      // nothing of interest
1219      outputtext.push_back(c); // the '_'
1220      c = my_ttnextchar (tthere, ttend);
1221    }
1222    } // end of while (tthere != ttend)
1223}
1224
1225ostream *displayclass::setlogout (ostream *thelogout) {
1226
1227  ostream *oldlogout = logout;
1228  logout = thelogout;
1229  return oldlogout;
1230}
1231
1232// protected methods for display
1233
1234// reloads any default macro files which have changed
1235// returns 0 no errors occurred
1236//        -1 an error occurred while trying to load one of the files
1237int displayclass::checkdefaultmacrofiles ()
1238{
1239  // this isn't implemented yet
1240  return 0;
1241}
1242
1243// isdefaultmacro sees if there is an entry in the list of
1244// default macros with the given package and macro name
1245// returns 0 if no macros in the package or in the global package
1246//           were found
1247//         1 if no macros in the package were found but a macro
1248//           in the global package was found
1249//         2 if a macro in the given package was found
1250int displayclass::isdefaultmacro (const text_t &package, const text_t &macroname) {
1251  // make sure a macroname was supplied
1252  if (macroname.empty()) return 0;
1253
1254  if (!package.empty()) {
1255    // check the given package
1256    if (defaultmacros->macro_find(package, macroname) != NULL) return 2;
1257  }
1258   
1259  // check the Global package
1260  if (defaultmacros->macro_find(defaultpackage, macroname) != NULL) return 1;
1261  // not found
1262  return 0;
1263}
1264
1265// havemacro sees if there is an entry in the list of macros or
1266// default macros with the given package and macro name
1267// returns 0 if no macros in the package or in the global package
1268//           were found
1269//         1 if no macros in the package were found but a macro
1270//           in the global package was found
1271//         2 if a macro in the given package was found
1272//         4 if no macros in the package were found but a macro
1273//           in the global package was found in default macros
1274//         8 if a macro in the given package was found in default
1275int displayclass::havemacro(const text_t &package, const text_t &macroname)
1276{
1277  // make sure a macroname was supplied
1278  if (macroname.empty()) return 0;
1279 
1280  // make package "Global" if it doesn't point to anything yet
1281  if (package.empty()) {
1282    // check the Global package in current macros list
1283    if (currentmacros->macro_find(defaultpackage, macroname) != NULL) return 1;
1284   
1285    // check the Global package in default macros list
1286    if (defaultmacros->macro_find(defaultpackage, macroname) != NULL) return 4;
1287  } else {
1288    // check the given package in current macros list
1289    if (currentmacros->macro_find(package, macroname) != NULL) return 2;
1290   
1291    // check the Global package in current macros list
1292    if (currentmacros->macro_find(defaultpackage, macroname) != NULL) return 1;
1293   
1294    // check the given package in default macros list
1295    if (defaultmacros->macro_find(package, macroname) != NULL) return 8;
1296   
1297    // check the Global package in default macros list
1298    if (defaultmacros->macro_find(defaultpackage, macroname) != NULL) return 4;
1299  }
1300
1301  // nothing was found
1302  return 0;
1303}
1304 
1305 
1306// setparammacro adds an entry to the list of default macros
1307// returns 0 if there was no error,
1308//        -1 if it redefined a macro
1309//        -2 if it hid a Global macro
1310//        -3 if it redefined a macro and hid a Global macro
1311//        -4 if no macroname was supplied
1312int displayclass::setparammacro (parammacros_t* macrotable,
1313                 const text_t &package,
1314                 const text_t &macroname,
1315                 text_t macroparams,
1316                 const text_t &filename,
1317                 const text_t &macrovalue)
1318{
1319  // make sure a macroname was supplied
1320  if (macroname.empty()) return -4;
1321
1322  // put the parameters in a standard form
1323  paramhashtype paramhash;
1324  if (macroparams.empty()) macroparams = "ignore=yes";
1325  splitparams (macroparams, paramhash);
1326  joinparams (paramhash, macroparams);
1327
1328  // remember these parameters
1329  allparams.insert (macroparams);
1330
1331  // remember this filename (this part isn't finished yet -- Rodger).
1332
1333  // set the macro
1334  if (package.empty()) {
1335    return macrotable->setmacro(defaultpackage, macroname, macroparams, filename, macrovalue);
1336  } else {
1337    return macrotable->setmacro(package, macroname, macroparams, filename, macrovalue);
1338  }
1339}
1340
1341// setdefaultmacro adds an entry to the list of default macros
1342// returns 0 if there was no error,
1343//        -1 if it redefined a macro
1344//        -2 if it hid a Global macro
1345//        -3 if it redefined a macro and hid a Global macro
1346//        -4 if no macroname was supplied
1347int displayclass::setdefaultmacro (const text_t &package,
1348                   const text_t &macroname,
1349                   text_t macroparams,
1350                   const text_t &filename,
1351                   const text_t &macrovalue)
1352{
1353  return setparammacro(defaultmacros,package,macroname,
1354               macroparams,filename,macrovalue);
1355}
1356
1357// public function
1358int displayclass::setcollectionmacro(const text_t &package,
1359                     const text_t &macroname,
1360                     text_t mparams,
1361                     const text_t &macrovalue)
1362{
1363  return setcollectionmacro(package, macroname, mparams, "memory", macrovalue);
1364}
1365
1366// setcollectionmacro adds an entry to the list of collection specific macros
1367// returns 0 if there was no error,
1368//        -1 if it redefined a macro
1369//        -2 if it hid a Global macro
1370//        -3 if it redefined a macro and hid a Global macro
1371//        -4 if no macroname was supplied
1372int displayclass::setcollectionmacro (const text_t &package,
1373                      const text_t &macroname,
1374                      text_t mparams,
1375                      const text_t &filename,
1376                      const text_t &macrovalue)
1377{
1378  return setparammacro(collectionmacros,package,macroname,mparams,
1379               filename,macrovalue);
1380}
1381
1382
1383// evaluates a boolean expression
1384// returns false if expr equals "" or "0".
1385// otherwise returns true *unless* expr is
1386// format "XXXX" eq/ne "dddd" in which case
1387// the two quoted strings are compared
1388bool displayclass::boolexpr (const text_t &package, const text_t &expr, int recursiondepth) {
1389  if (expr.empty()) return false;
1390
1391  text_t expexpr;
1392  if (package.empty()) {
1393    expandstring (defaultpackage, expr, expexpr, recursiondepth);
1394  } else {
1395    expandstring (package, expr, expexpr, recursiondepth);
1396  }
1397  if (expexpr.empty() || expexpr == "0") return false;
1398  // allow \" as start of exp as well as ", cos this is what the GLI writes out
1399  if (expr[0] != '\"' && !(expr[0] == '\\' && expr[1] == '"')) {
1400    return true;
1401  }
1402  // don't use expexpr while separating quoted parts of
1403  // expression just in case expanded out macros contain
1404  // quotes
1405  text_t::const_iterator here = expr.begin();
1406  text_t::const_iterator end = expr.end();
1407
1408  int quotecount = 0;
1409  text_t string1, expstring1;
1410  text_t string2, expstring2;
1411  text_t op;
1412  text_t combineop;
1413  bool result = false; // an empty string is false
1414  bool result2 = false;
1415
1416  while (here != end) {
1417    // get a comparison
1418    quotecount = 0;
1419    string1.clear();
1420    string2.clear();
1421    op.clear();
1422    while (here != end) {
1423      if (*here == '\\' && *(here+1) =='"') {
1424    ++quotecount; // found an escaped quote
1425    ++here;
1426      }
1427      else if (*here == '"') ++quotecount;
1428      else if (quotecount == 1) string1.push_back(*here);
1429      else if ((quotecount == 2) && !is_unicode_space (*here))
1430    op.push_back(*here);
1431      else if (quotecount == 3) string2.push_back(*here);
1432      else if (quotecount >= 4) break;
1433      ++here;
1434    }
1435   
1436    expandstring (package, string1, expstring1, recursiondepth);
1437    expandstring (package, string2, expstring2, recursiondepth);
1438
1439    // get next result
1440    result2 = true; // any badly formatted string will return true
1441    if (op == "eq") result2 = (expstring1 == expstring2);
1442    else if (op == "ne") result2 = (expstring1 != expstring2);
1443    else if (op == "gt") result2 = (expstring1 > expstring2);
1444    else if (op == "ge") result2 = (expstring1 >= expstring2);
1445    else if (op == "lt") result2 = (expstring1 < expstring2);
1446    else if (op == "le") result2 = (expstring1 <= expstring2);
1447    else if (op == "==") result2 = (expstring1.getint() == expstring2.getint());
1448    else if (op == "!=") result2 = (expstring1.getint() != expstring2.getint());
1449    else if (op == ">") result2 = (expstring1.getint() > expstring2.getint());
1450    else if (op == ">=") result2 = (expstring1.getint() >= expstring2.getint());
1451    else if (op == "<") result2 = (expstring1.getint() < expstring2.getint());
1452    else if (op == "<=") result2 = (expstring1.getint() <= expstring2.getint());
1453    else if (op == "sw") result2 = (starts_with(expstring1,expstring2));
1454    else if (op == "ew") result2 = (ends_with(expstring1,expstring2));
1455
1456    // combine the results
1457    if (combineop == "&&") result = (result && result2);
1458    else if (combineop == "||") result = (result || result2);
1459    else result = result2;
1460
1461    // get next combination operator
1462    combineop.clear();
1463    while (here != end && *here != '"') {
1464      if (!is_unicode_space(*here)) combineop.push_back(*here);
1465      ++here;
1466    }
1467  }
1468
1469  return result;
1470}
1471
1472
1473mvalue* displayclass::macro_find (parammacros_t* macrotable,
1474                  const text_t &packagename,
1475                  const text_t &macroname)
1476{
1477  mvalue *macrovalue = NULL;
1478
1479  // next look in the named parammacros for the named package
1480  if (macrotable->macro_find (packagename, macroname) != NULL)
1481    {
1482      paramspeclist::iterator paramhere, paramend;
1483
1484      paramhere = orderparamlist->begin(); paramend = orderparamlist->end();
1485      while ((macrovalue == NULL) && (paramhere != paramend))
1486    {
1487      macrovalue = macrotable->parameter_find(packagename, macroname,
1488                          (*paramhere).param);
1489      ++paramhere;
1490    }
1491    }
1492
1493  return macrovalue;
1494}
1495
1496
1497// (recursively) expand out a macro
1498// returns true  if the macro was found
1499//         false if the macro was not found
1500// macropackage should never be empty
1501bool displayclass::macro (const text_t &macroname,
1502              const text_t &macropackage,
1503              const text_t &macroparam,
1504              text_t &outputtext,
1505              int recursiondepth)
1506{
1507  assert(!macropackage.empty()); //???
1508  //outconvertclass text_t2ascii;
1509  text_tlist splitmacroparam;
1510  text_t::const_iterator hereit, endit;
1511  text_t aparam;
1512  unsigned short c = '\0', quote = '\0';
1513
1514  //  cerr << "r: " << recursiondepth << "\n";
1515  // check for deep recursion
1516  if (recursiondepth >= MAXRECURSIONDEPTH)
1517    {
1518      if (logout != NULL)
1519    (*logout) << "Warning: deep recursion, limiting macro expansion\n";
1520      return false;
1521    }
1522
1523  // check the macropackage
1524  //if (macropackage.empty()) macropackage = "Global";
1525
1526  // get the parameters (but don't expand them)
1527  if (macroparam.size() > 0) {
1528    // set up for loop
1529    hereit = macroparam.begin();
1530    endit = macroparam.end();
1531    if (hereit != endit) c = (*hereit);
1532    int openbrackets=0; // keep track of bracket level eg _If_(1, _macro_(arg))
1533            // this allows commas inside brackets (as arguments)
1534    while (hereit != endit) {
1535      // get the next parameter
1536      aparam.clear();
1537      quote = '\0'; // not-quoted unless proven quoted
1538     
1539      // ignore initial whitespace
1540      while ((hereit!=endit)&&is_unicode_space(c)) c=my_ttnextchar(hereit,endit);
1541     
1542      // look for the end of the parameter
1543      while (hereit != endit) {
1544    if (c == '\\') {
1545      // found escape character, also get next character
1546      aparam.push_back(c);
1547      c = my_ttnextchar (hereit, endit);
1548      if (hereit != endit) aparam.push_back(c);
1549     
1550    } else if (quote=='\0' && (c=='\'' /*|| c=='"'*/)) {
1551      // found a quoted section
1552      quote = c;
1553      if (openbrackets>0) aparam.push_back(c);
1554
1555    } else if (quote!='\0' && c==quote) {
1556      // found the end of a quote
1557      quote = '\0';
1558      if (openbrackets<0) aparam.push_back(c);
1559
1560    } else if (quote=='\0' && (c==',' || c==')') && openbrackets==0) {
1561      // found the end of a parameter
1562      c = my_ttnextchar (hereit, endit);
1563      break;
1564
1565    } else if (c=='(') {
1566      aparam.push_back(c);
1567      ++openbrackets;
1568    } else if (c==')') {
1569      --openbrackets;
1570      aparam.push_back(c);
1571    } else {
1572      // ordinary character
1573      aparam.push_back(c);
1574    }
1575
1576    c = my_ttnextchar (hereit, endit);
1577      }
1578
1579
1580      // add this parameter to the list
1581      splitmacroparam.push_back(aparam);
1582    }
1583  }
1584
1585  if (macroname == "If") {
1586    // get the condition, then clause and else clause
1587    text_tlist::iterator paramcond = splitmacroparam.begin();
1588    text_tlist::iterator paramend = splitmacroparam.end();
1589    text_tlist::iterator paramthen = paramend;
1590    text_tlist::iterator paramelse = paramend;
1591   
1592    if (paramcond != paramend) {
1593      paramthen = paramcond;
1594      ++paramthen;
1595    }
1596    if (paramthen != paramend) {
1597      paramelse = paramthen;
1598      ++paramelse;
1599    }
1600   
1601    // will always output something
1602    outputtext.clear();
1603
1604
1605    // expand out the first parameter
1606    if (paramcond != paramend) {
1607      /// text_t tmpoutput;
1608      /// expandstring (macropackage, *paramcond, tmpoutput, recursiondepth+1);
1609      /// lc (tmpoutput);
1610      // test the expanded string
1611      if (boolexpr (macropackage, *paramcond, recursiondepth+1)) {
1612    //(tmpoutput.size()) && (tmpoutput != "false") && (tmpoutput != "0")) {
1613    // true
1614    if (paramthen != paramend)
1615      expandstring (macropackage, *paramthen, outputtext, recursiondepth+1);
1616      } else {
1617    // false
1618    if (paramelse != paramend)
1619      expandstring (macropackage, *paramelse, outputtext, recursiondepth+1);
1620      }
1621    }
1622   
1623    return true;
1624  }
1625
1626  // try and find this macro
1627
1628  // this list might be replaced by something a little more
1629  // sophisticated in the future.
1630  text_tlist packagelist;
1631  packagelist.push_back (macropackage);
1632  packagelist.push_back ("Global");
1633
1634  text_tlist::iterator packagehere = packagelist.begin();
1635  text_tlist::iterator packageend = packagelist.end();
1636  mvalue *macrovalue = NULL;
1637  while (packagehere != packageend)
1638    {
1639      // first look in the currentmacros
1640      macrovalue = currentmacros->macro_find(*packagehere, macroname);
1641      if (macrovalue != NULL)
1642    break;
1643
1644      macrovalue = currentmacros->macro_find("Style", macroname);
1645      if (macrovalue != NULL)
1646    break;
1647
1648      // look in the collection specific macros
1649      macrovalue = macro_find(collectionmacros,*packagehere,macroname);
1650      if (macrovalue != NULL)
1651    break;
1652
1653      // and again in the collection specific package "Style"
1654      macrovalue = macro_find(collectionmacros,(char*)"Style",macroname);
1655      if (macrovalue != NULL)
1656    break;
1657
1658      // look in the default macros
1659      macrovalue = macro_find(defaultmacros,*packagehere,macroname);
1660      if (macrovalue != NULL)
1661    break;
1662
1663      // and again in the package "Style"
1664      macrovalue = macro_find(defaultmacros,(char*)"Style",macroname);
1665      if (macrovalue != NULL)
1666    break;
1667      ++packagehere;
1668    }
1669
1670  if (macrovalue != NULL)
1671    {
1672      // we found a macro
1673
1674      // replace all the option macros (_1_, _2_, ...) in the value of this macro
1675      // and decide if we need to expand the value of this macro
1676      bool needsexpansion = false;
1677      text_t tempmacrovalue;
1678      tempmacrovalue.clear();
1679      hereit = macrovalue->value.begin();
1680      endit = macrovalue->value.end();
1681      if (hereit != endit) c = (*hereit);
1682      while (hereit != endit)
1683    {
1684      if (c == '\\')
1685        {
1686          // found an escape character
1687          needsexpansion = true;
1688          tempmacrovalue.push_back(c);
1689          c = my_ttnextchar (hereit, endit);
1690          if (hereit != endit) tempmacrovalue.push_back(c);
1691          c = my_ttnextchar (hereit, endit);
1692        }
1693      else if (c == '_')
1694        {
1695          text_t::const_iterator savedhereit = hereit;
1696
1697          // attempt to get a number
1698          int argnum = 0;
1699          unsigned short digc = my_ttnextchar (hereit, endit);
1700          while (digc >= '0' && digc <= '9') {
1701        argnum = argnum*10 + digc - '0';
1702        digc = my_ttnextchar (hereit, endit);
1703          }
1704          if (digc == '_' && argnum > 0) {
1705        // found an option macro, append the appropriate text
1706        text_tlist::iterator ttlisthere = splitmacroparam.begin();
1707        text_tlist::iterator ttlistend = splitmacroparam.end();
1708        while ((argnum > 1) && (ttlisthere != ttlistend)) {
1709          --argnum;
1710          ++ttlisthere;
1711        }
1712        if (ttlisthere != ttlistend) {
1713          tempmacrovalue.append(*ttlisthere);
1714          if (findchar((*ttlisthere).begin(), (*ttlisthere).end(), '_') != (*ttlisthere).end())
1715            needsexpansion = true;
1716        }
1717
1718        c = my_ttnextchar (hereit, endit);
1719
1720          } else {
1721        // wasn't a option macro
1722        needsexpansion = true;
1723        tempmacrovalue.push_back(c);
1724        hereit = savedhereit;
1725        c = my_ttnextchar (hereit, endit);
1726          }
1727        }
1728      else
1729        {
1730          tempmacrovalue.push_back(c);
1731          c = my_ttnextchar (hereit, endit);
1732        }
1733    }
1734     
1735      // recursively replace this macro (if we need to)
1736      if (needsexpansion) {
1737    expandstring (*packagehere, tempmacrovalue, outputtext, recursiondepth+1);
1738      } else {
1739    outputtext += tempmacrovalue;
1740      }
1741
1742      return true;
1743    }
1744
1745  // GRB: deal with remaining code properly as it faults
1746  return false;
1747  outconvertclass text_t2ascii;
1748  if (logout != NULL) {
1749    (*logout) << text_t2ascii << "Warning: _" <<
1750      macropackage << ":" << macroname << "_ not found\n";
1751  }
1752 
1753  return false; // didn't find the macro
1754}
1755
1756
1757void displayclass::printdefaultmacros ()
1758{
1759  defpackagemap::const_iterator packagemapit;
1760  defmacromap::const_iterator macromapit;
1761  defparammap::const_iterator parammapit;
1762  const mvalue *mvalueptr;
1763  outconvertclass text_t2ascii;
1764
1765  text_t packagename, macroname, macroparam;
1766 
1767  for (packagemapit = defaultmacros->package_begin();
1768       packagemapit != defaultmacros->package_end();
1769       ++packagemapit)
1770    {
1771      packagename = (*packagemapit).first;
1772      //     cerr << "***package   : \"" << packagename << "\"\n";
1773   
1774      for (macromapit = (*packagemapit).second.begin();
1775       macromapit != (*packagemapit).second.end();
1776       ++macromapit)
1777    {
1778      macroname = (*macromapit).first;
1779      //       cerr << "***macroname : \"" << macroname << "\"\n";
1780
1781      for (parammapit = (*macromapit).second.begin();
1782           parammapit != (*macromapit).second.end();
1783           ++parammapit)
1784        {
1785          macroparam = (*parammapit).first;
1786          mvalueptr = &((*parammapit).second);
1787          cerr << text_t2ascii << "package   : \"" << packagename << "\"\n";
1788          cerr << text_t2ascii << "macroname : \"" << macroname << "\"\n";
1789          cerr << text_t2ascii << "parameters: [" << macroparam << "]\n";
1790          cerr << text_t2ascii << "filename  : \"" << mvalueptr->filename << "\"\n";
1791          cerr << text_t2ascii << "value     : {" << mvalueptr->value << "}\n\n";
1792        }
1793    }
1794    }
1795}
1796
1797void displayclass::printallparams ()
1798{
1799  text_tset::iterator text_tsetit;
1800  outconvertclass text_t2ascii;
1801
1802  cerr << text_t2ascii << "** allparams\n";
1803  for (text_tsetit=allparams.begin();
1804       text_tsetit!=allparams.end();
1805       ++text_tsetit) {
1806    cerr << text_t2ascii << (*text_tsetit) << "\n";
1807  }
1808  cerr << text_t2ascii << "\n";
1809}
1810
1811
1812
1813/////////////////////////////////////
1814// stuff to do tricky output
1815/////////////////////////////////////
1816
1817displayclass &operator<< (outconvertclass &theoutc, displayclass &display)
1818{
1819  display.setconvertclass (&theoutc);
1820  return display;
1821}
1822
1823displayclass &operator<< (displayclass &display, const text_t &t)
1824{
1825  outconvertclass *theoutc = display.getconvertclass();
1826
1827  if (theoutc == NULL) return display;
1828 
1829  text_t output;
1830  display.expandstring (t, output);
1831  (*theoutc) << output;
1832
1833  return display;
1834}
Note: See TracBrowser for help on using the browser.