source: main/tags/2.53/gsdl/lib/display.cpp@ 32727

Last change on this file since 32727 was 9025, checked in by davidb, 19 years ago

indentation tidy up

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